Search in sources :

Example 21 with BundleWire

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

the class Phase_Extension method findExtensionDependencies.

List<ExtensionDependency> findExtensionDependencies(BundleWiring bundleWiring) {
    List<ExtensionDependency> extensionDependencies = new CopyOnWriteArrayList<>();
    List<BundleWire> requiredWires = bundleWiring.getRequiredWires(CdiConstants.CDI_EXTENSION_NAMESPACE);
    for (BundleWire wire : requiredWires) {
        Map<String, Object> attributes = wire.getCapability().getAttributes();
        String extension = (String) attributes.get(CdiConstants.CDI_EXTENSION_NAMESPACE);
        if (extension != null) {
            ExtensionDependency extensionDependency = new ExtensionDependency(_bundleContext, wire.getProvider().getBundle().getBundleId(), extension);
            extensionDependencies.add(extensionDependency);
        }
    }
    return extensionDependencies;
}
Also used : BundleWire(org.osgi.framework.wiring.BundleWire) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 22 with BundleWire

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

the class BundleWiringData method getWiresCompositeData.

private CompositeData[] getWiresCompositeData(List<BundleWire> wires) throws OpenDataException {
    CompositeData[] reqWiresData = new CompositeData[wires.size()];
    for (int i = 0; i < wires.size(); i++) {
        BundleWire requiredWire = wires.get(i);
        Map<String, Object> wireItems = new HashMap<String, Object>();
        BundleCapability capability = requiredWire.getCapability();
        wireItems.put(BundleWiringStateMBean.PROVIDER_BUNDLE_ID, capability.getRevision().getBundle().getBundleId());
        wireItems.put(BundleWiringStateMBean.PROVIDER_BUNDLE_REVISION_ID, revisionIDMap.get(capability.getRevision()));
        wireItems.put(BundleWiringStateMBean.BUNDLE_CAPABILITY, getCapReqCompositeData(BundleWiringStateMBean.BUNDLE_CAPABILITY_TYPE, capability.getNamespace(), capability.getAttributes().entrySet(), capability.getDirectives().entrySet()));
        BundleRequirement requirement = requiredWire.getRequirement();
        wireItems.put(BundleWiringStateMBean.REQUIRER_BUNDLE_ID, requirement.getRevision().getBundle().getBundleId());
        wireItems.put(BundleWiringStateMBean.REQUIRER_BUNDLE_REVISION_ID, revisionIDMap.get(requirement.getRevision()));
        wireItems.put(BundleWiringStateMBean.BUNDLE_REQUIREMENT, getCapReqCompositeData(BundleWiringStateMBean.BUNDLE_REQUIREMENT_TYPE, requirement.getNamespace(), requirement.getAttributes().entrySet(), requirement.getDirectives().entrySet()));
        CompositeData wireData = new CompositeDataSupport(BundleWiringStateMBean.BUNDLE_WIRE_TYPE, wireItems);
        reqWiresData[i] = wireData;
    }
    return reqWiresData;
}
Also used : HashMap(java.util.HashMap) CompositeData(javax.management.openmbean.CompositeData) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Example 23 with BundleWire

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

the class BundleWiringState method populateTransitiveRevisions.

private void populateTransitiveRevisions(String namespace, BundleRevision rootRevision, Map<BundleRevision, Integer> allRevisions) {
    allRevisions.put(rootRevision, System.identityHashCode(rootRevision));
    BundleWiring wiring = rootRevision.getWiring();
    if (wiring == null)
        return;
    List<BundleWire> requiredWires = wiring.getRequiredWires(namespace);
    for (BundleWire wire : requiredWires) {
        BundleRevision revision = wire.getCapability().getRevision();
        if (!allRevisions.containsKey(revision)) {
            populateTransitiveRevisions(namespace, revision, allRevisions);
        }
    }
    List<BundleWire> providedWires = wiring.getProvidedWires(namespace);
    for (BundleWire wire : providedWires) {
        BundleRevision revision = wire.getRequirement().getRevision();
        if (!allRevisions.containsKey(revision)) {
            populateTransitiveRevisions(namespace, revision, allRevisions);
        }
    }
}
Also used : BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleWire(org.osgi.framework.wiring.BundleWire)

Example 24 with BundleWire

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

the class FrameworkMBeanTest method testRefreshBundlesAndWait2.

@Test
public void testRefreshBundlesAndWait2() throws Exception {
    Bundle bundleA = getBundleByName("org.apache.aries.jmx.test.bundlea");
    Bundle bundleB = getBundleByName("org.apache.aries.jmx.test.bundleb");
    BundleWiring bw = (BundleWiring) bundleB.adapt(BundleWiring.class);
    List<BundleWire> initialRequiredWires = bw.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
    assertEquals(1, initialRequiredWires.size());
    BundleWire wire = initialRequiredWires.get(0);
    Map<String, Object> capabilityAttributes = wire.getCapability().getAttributes();
    assertEquals("Precondition", bundleA.getSymbolicName(), capabilityAttributes.get(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE));
    assertEquals("Precondition", new Version("1.0"), capabilityAttributes.get(Constants.BUNDLE_VERSION_ATTRIBUTE));
    assertEquals("Precondition", "org.apache.aries.jmx.test.bundlea.api", capabilityAttributes.get(BundleRevision.PACKAGE_NAMESPACE));
    // Create an updated version of Bundle A, which an extra export and version 1.1
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
    manifest.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, "org.apache.aries.jmx.test.bundlea");
    manifest.getMainAttributes().putValue(Constants.BUNDLE_VERSION, "1.1");
    manifest.getMainAttributes().putValue(Constants.EXPORT_PACKAGE, "org.apache.aries.jmx.test.bundlea.api,org.apache.aries.jmx.test.bundlea.impl");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JarOutputStream jos = new JarOutputStream(baos, manifest);
    addResourceToJar("org/apache/aries/jmx/test/bundlea/api/InterfaceA.class", jos, bundleA);
    addResourceToJar("org/apache/aries/jmx/test/bundlea/impl/A2.class", jos, bundleA);
    jos.close();
    assertEquals("Precondition", 1, ((BundleRevisions) bundleA.adapt(BundleRevisions.class)).getRevisions().size());
    bundleA.update(new ByteArrayInputStream(baos.toByteArray()));
    assertEquals("There should be 2 revisions now", 2, ((BundleRevisions) bundleA.adapt(BundleRevisions.class)).getRevisions().size());
    assertEquals("No refresh called, the bundle wiring for B should still be the old one", bw, ((BundleWiring) bundleB.adapt(BundleWiring.class)));
    FrameworkMBean framework = getMBean(FrameworkMBean.OBJECTNAME, FrameworkMBean.class);
    CompositeData result = framework.refreshBundlesAndWait(null);
    Set<Long> completed = new HashSet<Long>(Arrays.asList((Long[]) result.get(FrameworkMBean.COMPLETED)));
    assertTrue(completed.contains(bundleA.getBundleId()));
    assertTrue(completed.contains(bundleB.getBundleId()));
    List<BundleWire> requiredWires = ((BundleWiring) bundleB.adapt(BundleWiring.class)).getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
    assertEquals(2, requiredWires.size());
    List<String> imported = new ArrayList<String>();
    for (BundleWire w : requiredWires) {
        Map<String, Object> ca = w.getCapability().getAttributes();
        assertEquals(bundleA.getSymbolicName(), ca.get(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE));
        imported.add(ca.get(BundleRevision.PACKAGE_NAMESPACE).toString());
        if ("org.apache.aries.jmx.test.bundlea.impl".equals(ca.get(BundleRevision.PACKAGE_NAMESPACE))) {
            // Came across an issue where equinox was reporting the other package as still coming from from the 1.0 bundle
            // not sure if this is a bug or not...
            assertEquals(new Version("1.1"), ca.get(Constants.BUNDLE_VERSION_ATTRIBUTE));
        }
    }
    assertEquals(Arrays.asList("org.apache.aries.jmx.test.bundlea.api", "org.apache.aries.jmx.test.bundlea.impl"), imported);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest) FrameworkMBean(org.osgi.jmx.framework.FrameworkMBean) BundleWire(org.osgi.framework.wiring.BundleWire) Version(org.osgi.framework.Version) ByteArrayInputStream(java.io.ByteArrayInputStream) BundleRevisions(org.osgi.framework.wiring.BundleRevisions) HashSet(java.util.HashSet) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.jmx.AbstractIntegrationTest)

Example 25 with BundleWire

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

the class FrameworkMBeanTest method testRefreshBundleAndWait.

@Test
public void testRefreshBundleAndWait() throws Exception {
    FrameworkWiring frameworkWiring = (FrameworkWiring) context().getBundle(0).adapt(FrameworkWiring.class);
    Bundle bundleA = getBundleByName("org.apache.aries.jmx.test.bundlea");
    Bundle bundleB = getBundleByName("org.apache.aries.jmx.test.bundleb");
    BundleWiring bw = (BundleWiring) bundleB.adapt(BundleWiring.class);
    List<BundleWire> initialRequiredWires = bw.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
    assertEquals(1, initialRequiredWires.size());
    BundleWire wire = initialRequiredWires.get(0);
    Map<String, Object> capabilityAttributes = wire.getCapability().getAttributes();
    assertEquals("Precondition", bundleA.getSymbolicName(), capabilityAttributes.get(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE));
    assertEquals("Precondition", new Version("1.0"), capabilityAttributes.get(Constants.BUNDLE_VERSION_ATTRIBUTE));
    assertEquals("Precondition", "org.apache.aries.jmx.test.bundlea.api", capabilityAttributes.get(BundleRevision.PACKAGE_NAMESPACE));
    // Create an updated version of Bundle A, which an extra export and version 1.1
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
    manifest.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, "org.apache.aries.jmx.test.bundlea");
    manifest.getMainAttributes().putValue(Constants.BUNDLE_VERSION, "1.1");
    manifest.getMainAttributes().putValue(Constants.EXPORT_PACKAGE, "org.apache.aries.jmx.test.bundlea.api,org.apache.aries.jmx.test.bundlea.impl");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JarOutputStream jos = new JarOutputStream(baos, manifest);
    addResourceToJar("org/apache/aries/jmx/test/bundlea/api/InterfaceA.class", jos, bundleA);
    addResourceToJar("org/apache/aries/jmx/test/bundlea/impl/A2.class", jos, bundleA);
    jos.close();
    assertEquals("Precondition", 0, frameworkWiring.getRemovalPendingBundles().size());
    assertEquals(0, framework.getRemovalPendingBundles().length);
    assertEquals("Precondition", 1, ((BundleRevisions) bundleA.adapt(BundleRevisions.class)).getRevisions().size());
    bundleA.update(new ByteArrayInputStream(baos.toByteArray()));
    assertEquals("There should be 2 revisions now", 2, ((BundleRevisions) bundleA.adapt(BundleRevisions.class)).getRevisions().size());
    assertEquals("No refresh called, the bundle wiring for B should still be the old one", bw, bundleB.adapt(BundleWiring.class));
    assertEquals("Precondition", 1, frameworkWiring.getRemovalPendingBundles().size());
    assertEquals(1, framework.getRemovalPendingBundles().length);
    assertEquals(((Bundle) frameworkWiring.getRemovalPendingBundles().iterator().next()).getBundleId(), framework.getRemovalPendingBundles()[0]);
    assertTrue(framework.refreshBundleAndWait(bundleB.getBundleId()));
    List<BundleWire> requiredWires = ((BundleWiring) bundleB.adapt(BundleWiring.class)).getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
    assertEquals(2, requiredWires.size());
    List<String> imported = new ArrayList<String>();
    for (BundleWire w : requiredWires) {
        Map<String, Object> ca = w.getCapability().getAttributes();
        assertEquals(bundleA.getSymbolicName(), ca.get(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE));
        imported.add(ca.get(BundleRevision.PACKAGE_NAMESPACE).toString());
        if ("org.apache.aries.jmx.test.bundlea.impl".equals(ca.get(BundleRevision.PACKAGE_NAMESPACE))) {
            // Came across an issue where equinox was reporting the other package as still coming from from the 1.0 bundle
            // not sure if this is a bug or not...
            assertEquals(new Version("1.1"), ca.get(Constants.BUNDLE_VERSION_ATTRIBUTE));
        }
    }
    assertEquals(Arrays.asList("org.apache.aries.jmx.test.bundlea.api", "org.apache.aries.jmx.test.bundlea.impl"), imported);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) ArrayList(java.util.ArrayList) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest) BundleWire(org.osgi.framework.wiring.BundleWire) Version(org.osgi.framework.Version) ByteArrayInputStream(java.io.ByteArrayInputStream) BundleRevisions(org.osgi.framework.wiring.BundleRevisions) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.jmx.AbstractIntegrationTest)

Aggregations

BundleWire (org.osgi.framework.wiring.BundleWire)30 BundleWiring (org.osgi.framework.wiring.BundleWiring)22 Bundle (org.osgi.framework.Bundle)19 ArrayList (java.util.ArrayList)9 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)8 Test (org.junit.Test)7 BundleCapability (org.osgi.framework.wiring.BundleCapability)7 BundleRevision (org.osgi.framework.wiring.BundleRevision)7 List (java.util.List)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 CompositeData (javax.management.openmbean.CompositeData)4 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)4 Version (org.osgi.framework.Version)4 BundleRevisions (org.osgi.framework.wiring.BundleRevisions)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Map (java.util.Map)3 JarOutputStream (java.util.jar.JarOutputStream)3 Manifest (java.util.jar.Manifest)3