Search in sources :

Example 51 with BundleRevision

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

the class BundleWiringStateMBeanTest method testGetRevisionsDeclaredRequirements.

@Test
public void testGetRevisionsDeclaredRequirements() throws Exception {
    BundleRevisions revisions = (BundleRevisions) bundleA.adapt(BundleRevisions.class);
    Assert.assertEquals("Precondition", 1, revisions.getRevisions().size());
    TabularData jmxRequirementsTable = brsMBean.getRevisionsDeclaredRequirements(bundleA.getBundleId(), BundleRevision.PACKAGE_NAMESPACE);
    Assert.assertEquals(1, jmxRequirementsTable.size());
    List<BundleRequirement> requirements = ((BundleRevision) revisions.getRevisions().iterator().next()).getDeclaredRequirements(BundleRevision.PACKAGE_NAMESPACE);
    CompositeData jmxRevRequirements = (CompositeData) jmxRequirementsTable.values().iterator().next();
    CompositeData[] jmxRequirements = (CompositeData[]) jmxRevRequirements.get(BundleWiringStateMBean.REQUIREMENTS);
    Map<Map<String, Object>, Map<String, String>> expectedRequirements = requirementsToMap(requirements);
    Map<Map<String, Object>, Map<String, String>> actualRequirements = jmxCapReqToMap(jmxRequirements);
    Assert.assertEquals(expectedRequirements, actualRequirements);
}
Also used : CompositeData(javax.management.openmbean.CompositeData) BundleRevision(org.osgi.framework.wiring.BundleRevision) HashMap(java.util.HashMap) Map(java.util.Map) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) BundleRevisions(org.osgi.framework.wiring.BundleRevisions) TabularData(javax.management.openmbean.TabularData) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.jmx.AbstractIntegrationTest)

Example 52 with BundleRevision

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

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

the class ClientWeavingHookOSGi43Test method mockProviderBundle.

private Bundle mockProviderBundle(String subdir, long id, Version version) throws Exception {
    URL url = getClass().getResource("/" + getClass().getName().replace('.', '/') + ".class");
    File classFile = new File(url.getFile());
    File baseDir = new File(classFile.getParentFile(), subdir);
    File directory = new File(baseDir, "/META-INF/services");
    final List<String> classNames = new ArrayList<String>();
    // Do a directory listing of the applicable META-INF/services directory
    List<String> resources = new ArrayList<String>();
    for (File f : directory.listFiles()) {
        String fileName = f.getName();
        if (fileName.startsWith(".") || fileName.endsWith("."))
            continue;
        classNames.addAll(getClassNames(f));
        // Needs to be something like: META-INF/services/org.apache.aries.mytest.MySPI
        String path = f.getAbsolutePath().substring(baseDir.getAbsolutePath().length());
        path = path.replace('\\', '/');
        if (path.startsWith("/")) {
            path = path.substring(1);
        }
        resources.add(path);
    }
    // Set up the classloader that will be used by the ASM-generated code as the TCCL.
    // It can load a META-INF/services file
    final ClassLoader cl = new TestProviderBundleClassLoader(subdir, resources.toArray(new String[] {}));
    final List<String> classResources = new ArrayList<String>();
    for (String className : classNames) {
        classResources.add("/" + className.replace('.', '/') + ".class");
    }
    Bundle systemBundle = EasyMock.createMock(Bundle.class);
    EasyMock.<Class<?>>expect(systemBundle.loadClass(EasyMock.anyObject(String.class))).andAnswer(new IAnswer<Class<?>>() {

        @Override
        public Class<?> answer() throws Throwable {
            String name = (String) EasyMock.getCurrentArguments()[0];
            return ClientWeavingHookOSGi43Test.class.getClassLoader().loadClass(name);
        }
    }).anyTimes();
    EasyMock.replay(systemBundle);
    BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
    EasyMock.expect(bc.getBundle(0)).andReturn(systemBundle).anyTimes();
    EasyMock.replay(bc);
    BundleWiring bundleWiring = EasyMock.createMock(BundleWiring.class);
    EasyMock.expect(bundleWiring.getClassLoader()).andReturn(cl).anyTimes();
    EasyMock.replay(bundleWiring);
    BundleRevision bundleRevision = EasyMock.createMock(BundleRevision.class);
    EasyMock.expect(bundleRevision.getWiring()).andReturn(bundleWiring).anyTimes();
    EasyMock.replay(bundleRevision);
    Bundle providerBundle = EasyMock.createMock(Bundle.class);
    String bsn = subdir;
    int idx = bsn.indexOf('_');
    if (idx > 0) {
        bsn = bsn.substring(0, idx);
    }
    EasyMock.expect(providerBundle.getSymbolicName()).andReturn(bsn).anyTimes();
    EasyMock.expect(providerBundle.getBundleId()).andReturn(id).anyTimes();
    EasyMock.expect(providerBundle.getBundleContext()).andReturn(bc).anyTimes();
    EasyMock.expect(providerBundle.getVersion()).andReturn(version).anyTimes();
    EasyMock.expect(providerBundle.adapt(BundleRevision.class)).andReturn(bundleRevision).anyTimes();
    EasyMock.<Class<?>>expect(providerBundle.loadClass(EasyMock.anyObject(String.class))).andAnswer(new IAnswer<Class<?>>() {

        @Override
        public Class<?> answer() throws Throwable {
            String name = (String) EasyMock.getCurrentArguments()[0];
            if (!classNames.contains(name)) {
                throw new ClassCastException(name);
            }
            return cl.loadClass(name);
        }
    }).anyTimes();
    EasyMock.replay(providerBundle);
    return providerBundle;
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) ArrayList(java.util.ArrayList) URL(java.net.URL) IAnswer(org.easymock.IAnswer) BundleRevision(org.osgi.framework.wiring.BundleRevision) URLClassLoader(java.net.URLClassLoader) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) File(java.io.File) BundleContext(org.osgi.framework.BundleContext)

Example 54 with BundleRevision

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

the class BundleEventHook method handleInstalledEvent.

private void handleInstalledEvent(BundleEvent event) {
    Bundle origin = event.getOrigin();
    BundleRevision originRevision = origin.adapt(BundleRevision.class);
    Bundle bundle = event.getBundle();
    BundleRevision bundleRevision = bundle.adapt(BundleRevision.class);
    if (bundleRevision == null) {
        // bundle has been uninstalled. Nothing we can do.
        return;
    }
    bundleToRevision.put(bundle, bundleRevision);
    // BundleContext or using RegionDigraph.
    if (ThreadLocalSubsystem.get() != null || // Region context bundles must be treated as explicit installations.
    bundleRevision.getSymbolicName().startsWith(Constants.RegionContextBundleSymbolicNamePrefix)) {
        return;
    }
    // Indicate that a bundle is being explicitly installed on this thread.
    // This protects against attempts to resolve the bundle as part of
    // processing the explicit installation.
    ThreadLocalBundleRevision.set(bundleRevision);
    try {
        if ("org.eclipse.equinox.region".equals(origin.getSymbolicName())) {
            // The bundle was installed using RegionDigraph.
            handleExplicitlyInstalledBundleRegionDigraph(origin, bundleRevision);
        } else {
            // The bundle was installed using some other bundle's BundleContext.
            handleExplicitlyInstalledBundleBundleContext(originRevision, bundleRevision);
        }
    } finally {
        // Always remove the bundle so that it can be resolved no matter
        // what happens here.
        ThreadLocalBundleRevision.remove();
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleRevision(org.osgi.framework.wiring.BundleRevision)

Example 55 with BundleRevision

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

the class BundleEventHook method handleExplicitlyInstalledBundleBundleContext.

/*
	 * This method guards against an uninstalled origin bundle. Guards against a
	 * null bundle revision are done elsewhere. It is assumed the bundle
	 * revision is never null once we get here.
	 */
private void handleExplicitlyInstalledBundleBundleContext(BundleRevision originRevision, BundleRevision bundleRevision) {
    /* 
		 * The newly installed bundle must become a constituent of all the Subsystems of which the bundle
		 * whose context was used to perform the install is a constituent (OSGI.enterprise spec. 134.10.1.1).
		 */
    Collection<BasicSubsystem> subsystems = getSubsystems().getSubsystemsReferencing(originRevision);
    boolean bundleRevisionInstalled = false;
    for (BasicSubsystem s : subsystems) {
        for (Resource constituent : s.getConstituents()) {
            if (constituent instanceof BundleConstituent) {
                BundleRevision rev = ((BundleConstituent) constituent).getRevision();
                if (originRevision.equals(rev)) {
                    Utils.installResource(bundleRevision, s);
                    bundleRevisionInstalled = true;
                }
            }
        }
    }
    /* if the bundle is not made constituent of any subsystem then make it constituent of root */
    if (!bundleRevisionInstalled) {
        Utils.installResource(bundleRevision, getSubsystems().getRootSubsystem());
    }
}
Also used : BundleConstituent(org.apache.aries.subsystem.core.internal.BundleResourceInstaller.BundleConstituent) Resource(org.osgi.resource.Resource) BundleRevision(org.osgi.framework.wiring.BundleRevision)

Aggregations

BundleRevision (org.osgi.framework.wiring.BundleRevision)79 Bundle (org.osgi.framework.Bundle)42 ArrayList (java.util.ArrayList)24 HashMap (java.util.HashMap)18 BundleWiring (org.osgi.framework.wiring.BundleWiring)14 BundleCapability (org.osgi.framework.wiring.BundleCapability)13 Test (org.junit.Test)12 Map (java.util.Map)11 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)11 Resource (org.osgi.resource.Resource)11 HashSet (java.util.HashSet)9 BundleRevisions (org.osgi.framework.wiring.BundleRevisions)9 BundleContext (org.osgi.framework.BundleContext)8 BundleException (org.osgi.framework.BundleException)8 List (java.util.List)7 BundleConstituent (org.apache.aries.subsystem.core.internal.BundleResourceInstaller.BundleConstituent)7 BundleWire (org.osgi.framework.wiring.BundleWire)7 Requirement (org.osgi.resource.Requirement)7 File (java.io.File)6 Collection (java.util.Collection)6