Search in sources :

Example 6 with BundleWire

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

the class FrameworkMBeanTest method testRefreshBundlesAndWait.

@Test
public void testRefreshBundlesAndWait() 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, bundleB.adapt(BundleWiring.class));
    FrameworkMBean framework = getMBean(FrameworkMBean.OBJECTNAME, FrameworkMBean.class);
    CompositeData result = framework.refreshBundlesAndWait(new long[] { bundleB.getBundleId() });
    assertTrue((Boolean) result.get(FrameworkMBean.SUCCESS));
    assertTrue(Arrays.equals(new Long[] { bundleB.getBundleId() }, (Long[]) result.get(FrameworkMBean.COMPLETED)));
    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) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.jmx.AbstractIntegrationTest)

Example 7 with BundleWire

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

the class FrameworkMBeanTest method testGetDependencyClosure.

@Test
public void testGetDependencyClosure() 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));
    Collection<Bundle> expectedDC = ((FrameworkWiring) context().getBundle(0).adapt(FrameworkWiring.class)).getDependencyClosure(Collections.singleton(bundleA));
    Set<Long> expectedClosure = new TreeSet<Long>();
    for (Bundle b : expectedDC) {
        expectedClosure.add(b.getBundleId());
    }
    long[] actualDC = framework.getDependencyClosure(new long[] { bundleA.getBundleId() });
    Set<Long> actualClosure = new TreeSet<Long>();
    for (long l : actualDC) {
        actualClosure.add(l);
    }
    assertEquals(expectedClosure, actualClosure);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) BundleWire(org.osgi.framework.wiring.BundleWire) Version(org.osgi.framework.Version) TreeSet(java.util.TreeSet) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.jmx.AbstractIntegrationTest)

Example 8 with BundleWire

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

the class BundleWiringStateMBeanTest method assertBundleWiring.

private void assertBundleWiring(BundleWiring bundleWiring, CompositeData jmxWiring) {
    CompositeData[] jmxCapabilities = (CompositeData[]) jmxWiring.get(BundleWiringStateMBean.CAPABILITIES);
    List<BundleCapability> capabilities = bundleWiring.getCapabilities(BundleRevision.PACKAGE_NAMESPACE);
    Assert.assertEquals(capabilities.size(), jmxCapabilities.length);
    Map<Map<String, Object>, Map<String, String>> expectedCapabilities = capabilitiesToMap(capabilities);
    Map<Map<String, Object>, Map<String, String>> actualCapabilities = jmxCapReqToMap(jmxCapabilities);
    Assert.assertEquals(expectedCapabilities, actualCapabilities);
    CompositeData[] jmxRequirements = (CompositeData[]) jmxWiring.get(BundleWiringStateMBean.REQUIREMENTS);
    List<BundleRequirement> requirements = bundleWiring.getRequirements(BundleRevision.PACKAGE_NAMESPACE);
    Assert.assertEquals(requirements.size(), jmxRequirements.length);
    Map<Map<String, Object>, Map<String, String>> expectedRequirements = requirementsToMap(requirements);
    Map<Map<String, Object>, Map<String, String>> actualRequirements = jmxCapReqToMap(jmxRequirements);
    Assert.assertEquals(expectedRequirements, actualRequirements);
    List<BundleWire> requiredWires = bundleWiring.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);
    CompositeData[] jmxRequiredWires = (CompositeData[]) jmxWiring.get(BundleWiringStateMBean.REQUIRED_WIRES);
    Assert.assertEquals(requiredWires.size(), jmxRequiredWires.length);
    Set<List<Object>> expectedRequiredWires = new HashSet<List<Object>>();
    for (BundleWire wire : requiredWires) {
        List<Object> data = new ArrayList<Object>();
        data.add(wire.getCapability().getRevision().getBundle().getBundleId());
        data.add(wire.getCapability().getAttributes());
        data.add(wire.getCapability().getDirectives());
        data.add(wire.getRequirement().getRevision().getBundle().getBundleId());
        data.add(wire.getRequirement().getAttributes());
        data.add(wire.getRequirement().getDirectives());
        expectedRequiredWires.add(data);
    }
    Set<List<Object>> actualRequiredWires = new HashSet<List<Object>>();
    for (CompositeData wire : jmxRequiredWires) {
        List<Object> data = new ArrayList<Object>();
        data.add(wire.get(BundleWiringStateMBean.PROVIDER_BUNDLE_ID));
        // TODO bundle revision id
        data.add(getJmxAttributes((CompositeData) wire.get(BundleWiringStateMBean.BUNDLE_CAPABILITY)));
        data.add(getJmxDirectives((CompositeData) wire.get(BundleWiringStateMBean.BUNDLE_CAPABILITY)));
        data.add(wire.get(BundleWiringStateMBean.REQUIRER_BUNDLE_ID));
        data.add(getJmxAttributes((CompositeData) wire.get(BundleWiringStateMBean.BUNDLE_REQUIREMENT)));
        data.add(getJmxDirectives((CompositeData) wire.get(BundleWiringStateMBean.BUNDLE_REQUIREMENT)));
        actualRequiredWires.add(data);
    }
    Assert.assertEquals(expectedRequiredWires, actualRequiredWires);
    List<BundleWire> providedWires = bundleWiring.getProvidedWires(BundleRevision.PACKAGE_NAMESPACE);
    CompositeData[] jmxProvidedWires = (CompositeData[]) jmxWiring.get(BundleWiringStateMBean.PROVIDED_WIRES);
    Assert.assertEquals(providedWires.size(), jmxProvidedWires.length);
    HashSet<List<Object>> expectedProvidedWires = new HashSet<List<Object>>();
    for (BundleWire wire : providedWires) {
        List<Object> data = new ArrayList<Object>();
        data.add(wire.getCapability().getRevision().getBundle().getBundleId());
        data.add(wire.getCapability().getAttributes());
        data.add(wire.getCapability().getDirectives());
        data.add(wire.getRequirement().getRevision().getBundle().getBundleId());
        data.add(wire.getRequirement().getAttributes());
        data.add(wire.getRequirement().getDirectives());
        expectedProvidedWires.add(data);
    }
    Set<List<Object>> actualProvidedWires = new HashSet<List<Object>>();
    for (CompositeData wire : jmxProvidedWires) {
        List<Object> data = new ArrayList<Object>();
        data.add(wire.get(BundleWiringStateMBean.PROVIDER_BUNDLE_ID));
        data.add(getJmxAttributes((CompositeData) wire.get(BundleWiringStateMBean.BUNDLE_CAPABILITY)));
        data.add(getJmxDirectives((CompositeData) wire.get(BundleWiringStateMBean.BUNDLE_CAPABILITY)));
        data.add(wire.get(BundleWiringStateMBean.REQUIRER_BUNDLE_ID));
        data.add(getJmxAttributes((CompositeData) wire.get(BundleWiringStateMBean.BUNDLE_REQUIREMENT)));
        data.add(getJmxDirectives((CompositeData) wire.get(BundleWiringStateMBean.BUNDLE_REQUIREMENT)));
        actualProvidedWires.add(data);
    }
    Assert.assertEquals(expectedProvidedWires, actualProvidedWires);
}
Also used : CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) ArrayList(java.util.ArrayList) List(java.util.List) BundleCapability(org.osgi.framework.wiring.BundleCapability) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 9 with BundleWire

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

the class ProviderBundleTrackerCustomizer method getHeaderFromBundleOrFragment.

private String getHeaderFromBundleOrFragment(Bundle bundle, String headerName, String matchString) {
    String val = bundle.getHeaders().get(headerName);
    if (matches(val, matchString))
        return val;
    BundleRevision rev = bundle.adapt(BundleRevision.class);
    if (rev != null) {
        BundleWiring wiring = rev.getWiring();
        if (wiring != null) {
            for (BundleWire wire : wiring.getProvidedWires("osgi.wiring.host")) {
                Bundle fragment = wire.getRequirement().getRevision().getBundle();
                val = fragment.getHeaders().get(headerName);
                if (matches(val, matchString)) {
                    return val;
                }
            }
        }
    }
    return null;
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleWire(org.osgi.framework.wiring.BundleWire)

Example 10 with BundleWire

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

the class BaseActivator method getAllHeaders.

private List<String> getAllHeaders(String headerName, Bundle bundle) {
    List<Bundle> bundlesFragments = new ArrayList<Bundle>();
    bundlesFragments.add(bundle);
    BundleRevision rev = bundle.adapt(BundleRevision.class);
    if (rev != null) {
        BundleWiring wiring = rev.getWiring();
        if (wiring != null) {
            for (BundleWire wire : wiring.getProvidedWires("osgi.wiring.host")) {
                bundlesFragments.add(wire.getRequirement().getRevision().getBundle());
            }
        }
    }
    List<String> l = new ArrayList<String>();
    for (Bundle bf : bundlesFragments) {
        String header = bf.getHeaders().get(headerName);
        if (header != null) {
            l.add(header);
        }
    }
    return l;
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleWire(org.osgi.framework.wiring.BundleWire)

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