Search in sources :

Example 51 with BundleWiring

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

the class BundleStateMBeanTest method getPackageVersion.

private Version getPackageVersion(String packageName) {
    Bundle systemBundle = context().getBundle(0);
    BundleWiring wiring = (BundleWiring) systemBundle.adapt(BundleWiring.class);
    List<BundleCapability> packages = wiring.getCapabilities(BundleRevision.PACKAGE_NAMESPACE);
    for (BundleCapability pkg : packages) {
        Map<String, Object> attrs = pkg.getAttributes();
        if (attrs.get(BundleRevision.PACKAGE_NAMESPACE).equals(packageName)) {
            return (Version) attrs.get(Constants.VERSION_ATTRIBUTE);
        }
    }
    throw new IllegalStateException("Package version not found for " + packageName);
}
Also used : Version(org.osgi.framework.Version) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 52 with BundleWiring

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

the class ProviderBundleTrackerCustomizer method getHeaderFromBundleOrFragment.

private String getHeaderFromBundleOrFragment(Bundle bundle, String headerName, String matchString) {
    Parameters headerParameters = new Parameters(bundle.getHeaders().get(headerName));
    if (matches(headerParameters.toString(), matchString) && !MERGE_HEADERS.contains(headerName)) {
        return headerParameters.isEmpty() ? null : headerParameters.toString();
    }
    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();
                Parameters fragmentParameters = new Parameters(fragment.getHeaders().get(headerName));
                if (MERGE_HEADERS.contains(headerName)) {
                    headerParameters.mergeWith(fragmentParameters, false);
                } else {
                    headerParameters = fragmentParameters;
                }
                if (matches(headerParameters.toString(), matchString)) {
                    return headerParameters.toString();
                }
            }
        }
    }
    return headerParameters.isEmpty() ? null : headerParameters.toString();
}
Also used : Parameters(aQute.bnd.header.Parameters) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleWire(org.osgi.framework.wiring.BundleWire)

Example 53 with BundleWiring

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

the class ClientWeavingHookGenericCapabilityTest method testHeadersFromFragment.

@Test
public void testHeadersFromFragment() throws Exception {
    // Register the bundle that provides the SPI implementation.
    Bundle providerBundle = mockProviderBundle("impl1", 1);
    activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle, new HashMap<String, Object>());
    Dictionary<String, String> fragmentConsumerHeaders = new Hashtable<String, String>();
    fragmentConsumerHeaders.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.CLIENT_REQUIREMENT);
    Bundle fragment = EasyMock.createMock(Bundle.class);
    EasyMock.expect(fragment.getHeaders()).andReturn(fragmentConsumerHeaders).anyTimes();
    EasyMock.replay(fragment);
    BundleRevision frev = EasyMock.createMock(BundleRevision.class);
    EasyMock.expect(frev.getBundle()).andReturn(fragment).anyTimes();
    EasyMock.replay(frev);
    BundleRequirement req = EasyMock.createMock(BundleRequirement.class);
    EasyMock.expect(req.getRevision()).andReturn(frev).anyTimes();
    EasyMock.replay(req);
    BundleWire wire = EasyMock.createMock(BundleWire.class);
    EasyMock.expect(wire.getRequirement()).andReturn(req).anyTimes();
    EasyMock.replay(wire);
    List<BundleWire> wires = Collections.singletonList(wire);
    BundleWiring wiring = EasyMock.createMock(BundleWiring.class);
    EasyMock.expect(wiring.getProvidedWires("osgi.wiring.host")).andReturn(wires).anyTimes();
    EasyMock.replay(wiring);
    BundleRevision rev = EasyMock.createMock(BundleRevision.class);
    EasyMock.expect(rev.getWiring()).andReturn(wiring).anyTimes();
    EasyMock.replay(rev);
    Bundle consumerBundle = mockConsumerBundle(new Hashtable<String, String>(), rev, providerBundle);
    activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.REQUIRE_CAPABILITY);
    Bundle spiFlyBundle = mockSpiFlyBundle("spifly", Version.parseVersion("1.9.4"), consumerBundle, providerBundle);
    WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator);
    // Weave the TestClient class.
    URL clsUrl = getClass().getResource("TestClient.class");
    Assert.assertNotNull("Precondition", clsUrl);
    String clientClassName = "org.apache.aries.spifly.dynamic.TestClient";
    WovenClass wc = new MyWovenClass(clsUrl, clientClassName, consumerBundle);
    Assert.assertEquals("Precondition", 0, wc.getDynamicImports().size());
    wh.weave(wc);
    Assert.assertEquals(1, wc.getDynamicImports().size());
    String di1 = "org.apache.aries.spifly";
    String di = wc.getDynamicImports().get(0);
    Assert.assertTrue("Weaving should have added a dynamic import", di1.equals(di));
    // Invoke the woven class and check that it properly sets the TCCL so that the
    // META-INF/services/org.apache.aries.mytest.MySPI file from impl1 is visible.
    Class<?> cls = wc.getDefinedClass();
    Method method = cls.getMethod("test", new Class[] { String.class });
    Object result = method.invoke(cls.getDeclaredConstructor().newInstance(), "hello");
    Assert.assertEquals(Collections.singleton("olleh"), result);
}
Also used : Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) BundleWiring(org.osgi.framework.wiring.BundleWiring) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) Method(java.lang.reflect.Method) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) URL(java.net.URL) BundleRevision(org.osgi.framework.wiring.BundleRevision) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) Test(org.junit.Test)

Example 54 with BundleWiring

use of org.osgi.framework.wiring.BundleWiring 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 55 with BundleWiring

use of org.osgi.framework.wiring.BundleWiring 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)

Aggregations

BundleWiring (org.osgi.framework.wiring.BundleWiring)110 Bundle (org.osgi.framework.Bundle)61 BundleWire (org.osgi.framework.wiring.BundleWire)39 ArrayList (java.util.ArrayList)23 BundleRevision (org.osgi.framework.wiring.BundleRevision)23 BundleCapability (org.osgi.framework.wiring.BundleCapability)19 Test (org.junit.Test)15 HashMap (java.util.HashMap)13 Hashtable (java.util.Hashtable)12 List (java.util.List)12 BundleContext (org.osgi.framework.BundleContext)12 URL (java.net.URL)10 Dictionary (java.util.Dictionary)10 HashSet (java.util.HashSet)9 LinkedHashMap (java.util.LinkedHashMap)8 Version (org.osgi.framework.Version)7 BundleRevisions (org.osgi.framework.wiring.BundleRevisions)7 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)6 IAnswer (org.easymock.IAnswer)6 ServiceReference (org.osgi.framework.ServiceReference)6