Search in sources :

Example 46 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project karaf by apache.

the class BundleWiresTest method bundleCap.

private BundleCapability bundleCap(long bundleId, String version) {
    BundleRevision rev = c.createMock(BundleRevision.class);
    Bundle bundle = c.createMock(Bundle.class);
    expect(bundle.getBundleId()).andReturn(bundleId);
    expect(rev.getBundle()).andReturn(bundle);
    BundleCapability cap = c.createMock(BundleCapability.class);
    expect(cap.getRevision()).andReturn(rev);
    Map<String, Object> attrs = new HashMap<>();
    attrs.put(Constants.VERSION_ATTRIBUTE, version);
    expect(cap.getAttributes()).andReturn(attrs);
    return cap;
}
Also used : HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 47 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project aries by apache.

the class JndiExtensionTests method testDisableExtensionAndCDIContainerWaits.

public void testDisableExtensionAndCDIContainerWaits() throws Exception {
    BundleTracker<Bundle> bt = new BundleTracker<>(bundle.getBundleContext(), Bundle.RESOLVED | Bundle.ACTIVE, new BundleTrackerCustomizer<Bundle>() {

        @Override
        public Bundle addingBundle(Bundle bundle, BundleEvent arg1) {
            List<BundleCapability> capabilities = bundle.adapt(BundleWiring.class).getCapabilities(CdiConstants.CDI_EXTENSION_NAMESPACE);
            if (capabilities.isEmpty()) {
                return null;
            }
            for (Capability capability : capabilities) {
                if (capability.getAttributes().containsValue("jndi")) {
                    return bundle;
                }
            }
            return null;
        }

        @Override
        public void modifiedBundle(Bundle bundle, BundleEvent arg1, Bundle arg2) {
        }

        @Override
        public void removedBundle(Bundle bundle, BundleEvent arg1, Bundle arg2) {
        }
    });
    bt.open();
    assertFalse(bt.isEmpty());
    Bundle extensionBundle = bt.getBundles()[0];
    Collection<ServiceReference<CdiContainer>> serviceReferences = bundleContext.getServiceReferences(CdiContainer.class, "(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + cdiBundle.getBundleId() + "))");
    assertNotNull(serviceReferences);
    assertFalse(serviceReferences.isEmpty());
    ServiceReference<CdiContainer> serviceReference = serviceReferences.iterator().next();
    CdiEvent.Type state = (CdiEvent.Type) serviceReference.getProperty(CdiConstants.CDI_CONTAINER_STATE);
    assertEquals(CdiEvent.Type.CREATED, state);
    extensionBundle.stop();
    state = (CdiEvent.Type) serviceReference.getProperty(CdiConstants.CDI_CONTAINER_STATE);
    assertEquals(CdiEvent.Type.WAITING_FOR_EXTENSIONS, state);
    extensionBundle.start();
    state = (CdiEvent.Type) serviceReference.getProperty(CdiConstants.CDI_CONTAINER_STATE);
    assertEquals(CdiEvent.Type.CREATED, state);
}
Also used : BundleCapability(org.osgi.framework.wiring.BundleCapability) Capability(org.osgi.resource.Capability) Bundle(org.osgi.framework.Bundle) BundleTracker(org.osgi.util.tracker.BundleTracker) ServiceReference(org.osgi.framework.ServiceReference) BundleEvent(org.osgi.framework.BundleEvent) List(java.util.List) CdiEvent(org.osgi.service.cdi.CdiEvent) CdiContainer(org.osgi.service.cdi.CdiContainer)

Example 48 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project geronimo-xbean by apache.

the class DelegatingBundle method buildPackageBundleMap.

private Map<String, Bundle> buildPackageBundleMap() {
    Map<String, Bundle> map = new HashMap<String, Bundle>();
    Iterator<Bundle> iterator = bundles.iterator();
    // skip first bundle
    iterator.next();
    // attempt to load the class from the remaining bundles
    while (iterator.hasNext()) {
        Bundle bundle = iterator.next();
        BundleWiring wiring = bundle.adapt(BundleWiring.class);
        if (wiring != null) {
            List<BundleCapability> capabilities = wiring.getCapabilities(BundleRevision.PACKAGE_NAMESPACE);
            if (capabilities != null && !capabilities.isEmpty()) {
                for (BundleCapability capability : capabilities) {
                    Map<String, Object> attributes = capability.getAttributes();
                    if (attributes != null) {
                        String packageName = String.valueOf(attributes.get(BundleRevision.PACKAGE_NAMESPACE));
                        if (!map.containsKey(packageName)) {
                            map.put(packageName, bundle);
                        }
                    }
                }
            }
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 49 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project felix by apache.

the class ExtensionManager method convertCapabilitiesToHeaders.

private String convertCapabilitiesToHeaders(Map headers) {
    StringBuffer exportSB = new StringBuffer("");
    Set<String> exportNames = new HashSet<String>();
    List<BundleCapability> caps = m_capabilities;
    for (BundleCapability cap : caps) {
        if (cap.getNamespace().equals(BundleRevision.PACKAGE_NAMESPACE)) {
            // Add a comma separate if there is an existing package.
            if (exportSB.length() > 0) {
                exportSB.append(", ");
            }
            // Append exported package information.
            exportSB.append(cap.getAttributes().get(BundleRevision.PACKAGE_NAMESPACE));
            for (Entry<String, String> entry : cap.getDirectives().entrySet()) {
                exportSB.append("; ");
                exportSB.append(entry.getKey());
                exportSB.append(":=\"");
                exportSB.append(entry.getValue());
                exportSB.append("\"");
            }
            for (Entry<String, Object> entry : cap.getAttributes().entrySet()) {
                if (!entry.getKey().equals(BundleRevision.PACKAGE_NAMESPACE) && !entry.getKey().equals(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE) && !entry.getKey().equals(Constants.BUNDLE_VERSION_ATTRIBUTE)) {
                    exportSB.append("; ");
                    exportSB.append(entry.getKey());
                    exportSB.append("=\"");
                    exportSB.append(entry.getValue());
                    exportSB.append("\"");
                }
            }
            // Remember exported packages.
            exportNames.add((String) cap.getAttributes().get(BundleRevision.PACKAGE_NAMESPACE));
        }
    }
    m_exportNames = exportNames;
    return exportSB.toString();
}
Also used : BundleCapability(org.osgi.framework.wiring.BundleCapability) HashSet(java.util.HashSet)

Example 50 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project felix by apache.

the class ExtensionManager method removeExtensionBundles.

public void removeExtensionBundles(Felix felix) {
    m_resolvedExtensions.clear();
    m_unresolvedExtensions.clear();
    m_failedExtensions.clear();
    m_headerMap.clear();
    m_headerMap.putAll(m_originalHeaderMap);
    try {
        ManifestParser mp = new ManifestParser(m_logger, m_configMap, m_systemBundleRevision, m_headerMap);
        List<BundleCapability> caps = ManifestParser.aliasSymbolicName(mp.getCapabilities(), m_systemBundleRevision);
        caps.add(buildNativeCapabilites());
        appendCapabilities(caps);
    } catch (Exception ex) {
        m_capabilities = Collections.EMPTY_LIST;
        m_logger.log(Logger.LOG_ERROR, "Error parsing system bundle export statement", ex);
    }
}
Also used : ManifestParser(org.apache.felix.framework.util.manifestparser.ManifestParser) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleException(org.osgi.framework.BundleException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException)

Aggregations

BundleCapability (org.osgi.framework.wiring.BundleCapability)83 ArrayList (java.util.ArrayList)30 BundleRevision (org.osgi.framework.wiring.BundleRevision)26 HashMap (java.util.HashMap)24 Bundle (org.osgi.framework.Bundle)24 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)21 BundleWiring (org.osgi.framework.wiring.BundleWiring)19 BundleWire (org.osgi.framework.wiring.BundleWire)18 List (java.util.List)15 Version (org.osgi.framework.Version)12 BundleException (org.osgi.framework.BundleException)11 ResolverHook (org.osgi.framework.hooks.resolver.ResolverHook)11 Collection (java.util.Collection)10 Test (org.junit.Test)10 HashSet (java.util.HashSet)9 Set (java.util.Set)8 ResolverHookFactory (org.osgi.framework.hooks.resolver.ResolverHookFactory)8 LinkedHashMap (java.util.LinkedHashMap)6 Module (org.eclipse.osgi.container.Module)6 CompositeData (javax.management.openmbean.CompositeData)5