Search in sources :

Example 21 with BundleRequirement

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

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

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

the class BundleWiringState method getRevisionWiring.

private CompositeData getRevisionWiring(BundleRevision revision, int revisionID, String namespace, Map<BundleRevision, Integer> revisionIDMap) {
    BundleWiring wiring = revision.getWiring();
    List<BundleCapability> capabilities = wiring.getCapabilities(namespace);
    List<BundleRequirement> requirements = wiring.getRequirements(namespace);
    List<BundleWire> providedWires = wiring.getProvidedWires(namespace);
    List<BundleWire> requiredWires = wiring.getRequiredWires(namespace);
    BundleWiringData data = new BundleWiringData(wiring.getBundle().getBundleId(), revisionID, capabilities, requirements, providedWires, requiredWires, revisionIDMap);
    return data.toCompositeData();
}
Also used : BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleWiringData(org.apache.aries.jmx.codec.BundleWiringData) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Example 24 with BundleRequirement

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

the class Aries1383Test method test11.

/*
	 * (11) Subsystem with apache-aries-provision-dependencies:=resolve undergoes 
	 * the following state transitions when starting fails due to a runtime 
	 * resolution failure: INSTALLING -> INSTALLED -> RESOLVING -> INSTALLED.
	 */
@Test
public void test11() throws Exception {
    Subsystem root = getRootSubsystem();
    subsystemEvents.clear();
    Subsystem subsystem = root.install(APPLICATION_DEPENDENCY_IN_ARCHIVE, applicationDependencyInArchive());
    ServiceRegistration<ResolverHookFactory> registration = bundleContext.registerService(ResolverHookFactory.class, new ResolverHookFactory() {

        @Override
        public ResolverHook begin(Collection<BundleRevision> triggers) {
            return new ResolverHook() {

                @Override
                public void filterResolvable(Collection<BundleRevision> candidates) {
                    for (Iterator<BundleRevision> i = candidates.iterator(); i.hasNext(); ) {
                        BundleRevision revision = i.next();
                        if (revision.getSymbolicName().equals(BUNDLE_B)) {
                            i.remove();
                        }
                    }
                }

                @Override
                public void filterSingletonCollisions(BundleCapability singleton, Collection<BundleCapability> collisionCandidates) {
                // Nothing.
                }

                @Override
                public void filterMatches(BundleRequirement requirement, Collection<BundleCapability> candidates) {
                // Nothing.
                }

                @Override
                public void end() {
                // Nothing.
                }
            };
        }
    }, null);
    try {
        subsystem.start();
        stopSubsystemSilently(subsystem);
        fail("Subsystem should not have started");
    } catch (SubsystemException e) {
        e.printStackTrace();
        long id = lastSubsystemId();
        assertEvent(id, APPLICATION_DEPENDENCY_IN_ARCHIVE, Version.emptyVersion, SubsystemConstants.SUBSYSTEM_TYPE_APPLICATION, State.INSTALLING, subsystemEvents.poll(id, 5000), ServiceEvent.REGISTERED);
        assertEvent(id, APPLICATION_DEPENDENCY_IN_ARCHIVE, Version.emptyVersion, SubsystemConstants.SUBSYSTEM_TYPE_APPLICATION, State.INSTALLED, subsystemEvents.poll(id, 5000), ServiceEvent.MODIFIED);
        assertEvent(id, APPLICATION_DEPENDENCY_IN_ARCHIVE, Version.emptyVersion, SubsystemConstants.SUBSYSTEM_TYPE_APPLICATION, State.RESOLVING, subsystemEvents.poll(id, 5000), ServiceEvent.MODIFIED);
        assertEvent(id, APPLICATION_DEPENDENCY_IN_ARCHIVE, Version.emptyVersion, SubsystemConstants.SUBSYSTEM_TYPE_APPLICATION, State.INSTALLED, subsystemEvents.poll(id, 5000), ServiceEvent.MODIFIED);
    } finally {
        registration.unregister();
        uninstallSubsystemSilently(subsystem);
    }
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) AriesSubsystem(org.apache.aries.subsystem.AriesSubsystem) Subsystem(org.osgi.service.subsystem.Subsystem) SubsystemException(org.osgi.service.subsystem.SubsystemException) BundleRevision(org.osgi.framework.wiring.BundleRevision) Iterator(java.util.Iterator) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) SubsystemTest(org.apache.aries.subsystem.itests.SubsystemTest) Test(org.junit.Test)

Example 25 with BundleRequirement

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

the class BundleWiringData method getRequirements.

private static CompositeData[] getRequirements(List<BundleRequirement> requirementList) throws OpenDataException {
    CompositeData[] reqData = new CompositeData[requirementList.size()];
    for (int i = 0; i < requirementList.size(); i++) {
        BundleRequirement requirement = requirementList.get(i);
        reqData[i] = getCapReqCompositeData(BundleWiringStateMBean.BUNDLE_REQUIREMENT_TYPE, requirement.getNamespace(), requirement.getAttributes().entrySet(), requirement.getDirectives().entrySet());
    }
    return reqData;
}
Also used : CompositeData(javax.management.openmbean.CompositeData) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Aggregations

BundleRequirement (org.osgi.framework.wiring.BundleRequirement)55 BundleCapability (org.osgi.framework.wiring.BundleCapability)23 ArrayList (java.util.ArrayList)20 BundleWire (org.osgi.framework.wiring.BundleWire)18 BundleRevision (org.osgi.framework.wiring.BundleRevision)17 HashMap (java.util.HashMap)15 Bundle (org.osgi.framework.Bundle)15 Test (org.junit.Test)14 BundleException (org.osgi.framework.BundleException)11 ResolverHook (org.osgi.framework.hooks.resolver.ResolverHook)10 BundleWiring (org.osgi.framework.wiring.BundleWiring)10 List (java.util.List)9 Collection (java.util.Collection)8 BundleRequirementImpl (org.apache.felix.framework.wiring.BundleRequirementImpl)8 ResolverHookFactory (org.osgi.framework.hooks.resolver.ResolverHookFactory)8 CompositeData (javax.management.openmbean.CompositeData)6 Module (org.eclipse.osgi.container.Module)6 ModuleContainer (org.eclipse.osgi.container.ModuleContainer)6 DummyContainerAdaptor (org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor)6 SimpleFilter (org.apache.felix.framework.capabilityset.SimpleFilter)5