Search in sources :

Example 26 with BundleWire

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

the class ProviderBundleTrackerCustomizerGenericCapabilityTest method testCapReqHeadersInFragment.

@Test
public void testCapReqHeadersInFragment() throws Exception {
    Bundle mediatorBundle = EasyMock.createMock(Bundle.class);
    EasyMock.expect(mediatorBundle.getBundleId()).andReturn(42l).anyTimes();
    EasyMock.replay(mediatorBundle);
    BaseActivator activator = new BaseActivator() {

        @Override
        public void start(BundleContext context) throws Exception {
        }
    };
    ProviderBundleTrackerCustomizer customizer = new ProviderBundleTrackerCustomizer(activator, mediatorBundle);
    ServiceRegistration<?> sreg = EasyMock.createNiceMock(ServiceRegistration.class);
    EasyMock.replay(sreg);
    BundleContext implBC = mockSPIBundleContext(sreg);
    Dictionary<String, String> headers = new Hashtable<String, String>();
    // A typical requirement that is not for us...
    headers.put(SpiFlyConstants.REQUIRE_CAPABILITY, "osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.6))\"");
    List<BundleWire> wires = new ArrayList<BundleWire>();
    BundleWire wire = EasyMock.createMock(BundleWire.class);
    Bundle fragment = EasyMock.createMock(Bundle.class);
    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);
    EasyMock.expect(wire.getRequirement()).andReturn(req).anyTimes();
    EasyMock.replay(wire);
    wires.add(wire);
    BundleWiring bw = EasyMock.createMock(BundleWiring.class);
    EasyMock.expect(bw.getProvidedWires("osgi.wiring.host")).andReturn(wires).anyTimes();
    EasyMock.replay(bw);
    BundleRevision rev = EasyMock.createMock(BundleRevision.class);
    EasyMock.expect(rev.getWiring()).andReturn(bw).anyTimes();
    EasyMock.replay(rev);
    Bundle implBundle = mockSPIBundle(implBC, headers, rev);
    Dictionary<String, String> fheaders = new Hashtable<String, String>();
    fheaders.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.PROVIDER_REQUIREMENT);
    fheaders.put(SpiFlyConstants.PROVIDE_CAPABILITY, SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "; " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "=org.apache.aries.mytest.MySPI");
    EasyMock.expect(fragment.getHeaders()).andReturn(fheaders).anyTimes();
    EasyMock.replay(fragment);
    assertEquals("Precondition", 0, activator.findProviderBundles("org.apache.aries.mytest.MySPI").size());
    customizer.addingBundle(implBundle, null);
    Collection<Bundle> bundles = activator.findProviderBundles("org.apache.aries.mytest.MySPI");
    assertEquals(1, bundles.size());
    assertSame(implBundle, bundles.iterator().next());
}
Also used : Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) BundleWiring(org.osgi.framework.wiring.BundleWiring) ArrayList(java.util.ArrayList) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 27 with BundleWire

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

the class SubsystemDependencyTestBase method verifySinglePackageWiring.

/**
	 * Check that wiredBundleName in subsystem s is wired to a single package, 
	 * expectedPackage, from expectedProvidingBundle
	 * @param s
	 * @param wiredBundleName
	 * @param expectedPackage
	 * @param expectedProvidingBundle
	 */
protected void verifySinglePackageWiring(Subsystem s, String wiredBundleName, String expectedPackage, String expectedProvidingBundle) {
    Bundle wiredBundle = context(s).getBundleByName(wiredBundleName);
    assertNotNull("Bundle not found", wiredBundleName);
    BundleWiring wiring = (BundleWiring) wiredBundle.adapt(BundleWiring.class);
    List<BundleWire> wiredPackages = wiring.getRequiredWires(PACKAGE_NAMESPACE);
    assertEquals("Only one package expected", 1, wiredPackages.size());
    String packageName = (String) wiredPackages.get(0).getCapability().getAttributes().get(PACKAGE_NAMESPACE);
    assertEquals("Wrong package found", expectedPackage, packageName);
    String providingBundle = wiredPackages.get(0).getProvider().getSymbolicName();
    assertEquals("Package provided by wrong bundle", expectedProvidingBundle, providingBundle);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleWire(org.osgi.framework.wiring.BundleWire)

Example 28 with BundleWire

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

the class SubsystemDependencyTestBase method verifyCapabilityWiring.

/**
	 * Verify that a bundle with wiredBundleName imports a single capability in namespace
	 * from expectedProvidingBundleName
	 * @param s
	 * @param wiredBundleName
	 * @param namespace
	 * @param expectedProvidingBundleName
	 */
protected void verifyCapabilityWiring(Subsystem s, String wiredBundleName, String namespace, String expectedProvidingBundleName) {
    Bundle wiredBundle = context(s).getBundleByName(wiredBundleName);
    assertNotNull("Targt bundle " + wiredBundleName + " not found", wiredBundleName);
    BundleWiring wiring = (BundleWiring) wiredBundle.adapt(BundleWiring.class);
    List<BundleWire> wiredProviders = wiring.getRequiredWires(namespace);
    assertEquals("Only one wire for capability namespace " + namespace + " expected", 1, wiredProviders.size());
    String capabilityNamespace = (String) wiredProviders.get(0).getCapability().getNamespace();
    assertEquals("Wrong namespace", namespace, capabilityNamespace);
    String providingBundle = wiredProviders.get(0).getProvider().getSymbolicName();
    assertEquals("Wrong bundle provider", expectedProvidingBundleName, providingBundle);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleWire(org.osgi.framework.wiring.BundleWire)

Example 29 with BundleWire

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

the class BundleWiresTest method testFromBundle.

@Test
public void testFromBundle() throws IOException {
    BundleWire wire = packageWire(packageFilter, bundleCap(targetBundleId, targetBundleVersion));
    Bundle bundle = wiredBundle(Arrays.asList(wire));
    c.replay();
    BundleWires bwires = new BundleWires(bundle);
    bwires.save(BASE_PATH);
    c.verify();
    Iterator<String> lines = Files.lines(new File("target/bundles/1").toPath()).iterator();
    Assert.assertEquals(PackageNamespace.PACKAGE_NAMESPACE + "; " + packageFilter, lines.next());
    Assert.assertEquals(targetBundleId + "; version=" + targetBundleVersion, lines.next());
    bwires.delete(BASE_PATH);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWire(org.osgi.framework.wiring.BundleWire) File(java.io.File) Test(org.junit.Test)

Example 30 with BundleWire

use of org.osgi.framework.wiring.BundleWire in project logging-log4j2 by apache.

the class Activator method start.

@Override
public void start(final BundleContext context) throws Exception {
    ProviderUtil.STARTUP_LOCK.lock();
    lockingProviderUtil = true;
    final BundleWiring self = context.getBundle().adapt(BundleWiring.class);
    List<BundleWire> required = self.getRequiredWires(LoggerContextFactory.class.getName());
    for (final BundleWire wire : required) {
        loadProvider(wire.getProviderWiring());
    }
    context.addBundleListener(this);
    final Bundle[] bundles = context.getBundles();
    for (final Bundle bundle : bundles) {
        loadProvider(bundle);
    }
    unlockIfReady();
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) LoggerContextFactory(org.apache.logging.log4j.spi.LoggerContextFactory) 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