Search in sources :

Example 46 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement 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.expect(rev.getTypes()).andReturn(0).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 47 with BundleRequirement

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

the class BundleWiringData method getWiresCompositeData.

private CompositeData[] getWiresCompositeData(List<BundleWire> wires) throws OpenDataException {
    CompositeData[] reqWiresData = new CompositeData[wires.size()];
    for (int i = 0; i < wires.size(); i++) {
        BundleWire requiredWire = wires.get(i);
        Map<String, Object> wireItems = new HashMap<String, Object>();
        BundleCapability capability = requiredWire.getCapability();
        wireItems.put(BundleWiringStateMBean.PROVIDER_BUNDLE_ID, capability.getRevision().getBundle().getBundleId());
        wireItems.put(BundleWiringStateMBean.PROVIDER_BUNDLE_REVISION_ID, revisionIDMap.get(capability.getRevision()));
        wireItems.put(BundleWiringStateMBean.BUNDLE_CAPABILITY, getCapReqCompositeData(BundleWiringStateMBean.BUNDLE_CAPABILITY_TYPE, capability.getNamespace(), capability.getAttributes().entrySet(), capability.getDirectives().entrySet()));
        BundleRequirement requirement = requiredWire.getRequirement();
        wireItems.put(BundleWiringStateMBean.REQUIRER_BUNDLE_ID, requirement.getRevision().getBundle().getBundleId());
        wireItems.put(BundleWiringStateMBean.REQUIRER_BUNDLE_REVISION_ID, revisionIDMap.get(requirement.getRevision()));
        wireItems.put(BundleWiringStateMBean.BUNDLE_REQUIREMENT, getCapReqCompositeData(BundleWiringStateMBean.BUNDLE_REQUIREMENT_TYPE, requirement.getNamespace(), requirement.getAttributes().entrySet(), requirement.getDirectives().entrySet()));
        CompositeData wireData = new CompositeDataSupport(BundleWiringStateMBean.BUNDLE_WIRE_TYPE, wireItems);
        reqWiresData[i] = wireData;
    }
    return reqWiresData;
}
Also used : HashMap(java.util.HashMap) CompositeData(javax.management.openmbean.CompositeData) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Example 48 with BundleRequirement

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

the class BundleWiringData method getRequirementsCompositeData.

public static CompositeData[] getRequirementsCompositeData(List<BundleRequirement> bundleRequirements) {
    try {
        CompositeData[] data = new CompositeData[bundleRequirements.size()];
        for (int i = 0; i < bundleRequirements.size(); i++) {
            BundleRequirement requirement = bundleRequirements.get(i);
            CompositeData cd = BundleWiringData.getCapReqCompositeData(BundleWiringStateMBean.BUNDLE_REQUIREMENT_TYPE, requirement.getNamespace(), requirement.getAttributes().entrySet(), requirement.getDirectives().entrySet());
            data[i] = cd;
        }
        return data;
    } catch (OpenDataException e) {
        throw new IllegalStateException("Can't create CompositeData", e);
    }
}
Also used : OpenDataException(javax.management.openmbean.OpenDataException) CompositeData(javax.management.openmbean.CompositeData) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Example 49 with BundleRequirement

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

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

the class PackageServiceImpl method getImports.

@Override
public List<PackageRequirement> getImports() {
    Bundle[] bundles = bundleContext.getBundles();
    SortedMap<String, PackageRequirement> requirements = new TreeMap<>();
    for (Bundle bundle : bundles) {
        BundleRevision rev = bundle.adapt(BundleRevision.class);
        if (rev != null) {
            List<BundleRequirement> reqs = rev.getDeclaredRequirements(BundleRevision.PACKAGE_NAMESPACE);
            for (BundleRequirement req : reqs) {
                PackageRequirement preq = create(req, bundle);
                requirements.put(preq.getPackageName() + "|" + preq.getFilter() + "|" + preq.getBundle().getBundleId(), preq);
            }
        }
    }
    return new ArrayList<>(requirements.values());
}
Also used : Bundle(org.osgi.framework.Bundle) PackageRequirement(org.apache.karaf.packages.core.PackageRequirement) BundleRevision(org.osgi.framework.wiring.BundleRevision) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) 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