Search in sources :

Example 1 with Resolver

use of org.osgi.service.resolver.Resolver in project felix by apache.

the class FELIX_4914_Test method testResolution.

@Test
public void testResolution() throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/felix-4914.json")));
    Map<String, Object> resolution = (Map<String, Object>) JsonReader.read(reader);
    List<Resource> repository = readRepository(resolution.get("repository"));
    Map<String, CapabilitySet> capSets = new HashMap<String, CapabilitySet>();
    for (Resource resource : repository) {
        for (Capability cap : resource.getCapabilities(null)) {
            String ns = cap.getNamespace();
            CapabilitySet set = capSets.get(ns);
            if (set == null) {
                set = new CapabilitySet(Collections.singletonList(ns));
                capSets.put(ns, set);
            }
            set.addCapability(cap);
        }
    }
    Resource root = null;
    for (Resource resource : repository) {
        if ("root".equals(Util.getSymbolicName(resource))) {
            root = resource;
            break;
        }
    }
    List<Resource> mandatory = new ArrayList<Resource>();
    mandatory.add(root);
    Map<Requirement, List<Capability>> candidates = new HashMap<Requirement, List<Capability>>();
    for (Resource resource : repository) {
        for (Requirement requirement : resource.getRequirements(null)) {
            CapabilitySet set = capSets.get(requirement.getNamespace());
            if (set != null) {
                String filter = requirement.getDirectives().get(Constants.FILTER_DIRECTIVE);
                SimpleFilter sf = (filter != null) ? SimpleFilter.parse(filter) : SimpleFilter.convert(requirement.getAttributes());
                candidates.put(requirement, new ArrayList<Capability>(set.match(sf, true)));
            } else {
                candidates.put(requirement, Collections.<Capability>emptyList());
            }
        }
    }
    ResolveContextImpl rci = new ResolveContextImpl(Collections.<Resource, Wiring>emptyMap(), candidates, mandatory, Collections.EMPTY_LIST);
    Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG));
    Map<Resource, List<Wire>> wireMap = resolver.resolve(rci);
}
Also used : InputStreamReader(java.io.InputStreamReader) Capability(org.osgi.resource.Capability) Resolver(org.osgi.service.resolver.Resolver) HashMap(java.util.HashMap) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Logger(org.apache.felix.resolver.Logger) Requirement(org.osgi.resource.Requirement) SimpleFilter(org.apache.felix.resolver.test.util.SimpleFilter) BufferedReader(java.io.BufferedReader) CapabilitySet(org.apache.felix.resolver.test.util.CapabilitySet) ArrayList(java.util.ArrayList) List(java.util.List) ResolveContextImpl(org.apache.felix.resolver.test.util.ResolveContextImpl) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with Resolver

use of org.osgi.service.resolver.Resolver in project felix by apache.

the class ResolverTest method testScenario6.

@Test
public void testScenario6() throws Exception {
    Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG));
    Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
    Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
    List<Resource> mandatory = populateScenario6(wirings, candMap);
    ResolveContextImpl rci = new ResolveContextImpl(wirings, candMap, mandatory, Collections.<Resource>emptyList());
    Map<Resource, List<Wire>> wireMap = resolver.resolve(rci);
    int aResources = 0;
    for (Resource r : wireMap.keySet()) {
        if ("A".equals(getResourceName(r))) {
            aResources++;
            List<Wire> wires = wireMap.get(r);
            assertEquals(4, wires.size());
            List<String> providers = new ArrayList<String>();
            for (Wire w : wires) {
                providers.add(getResourceName(w.getProvider()));
            }
            Collections.sort(providers);
            assertEquals(Arrays.asList("B", "C", "D", "D"), providers);
        }
    }
    assertEquals("Should have found two resolved resources named 'A'", 2, aResources);
}
Also used : Resolver(org.osgi.service.resolver.Resolver) GenericCapability(org.apache.felix.resolver.test.util.GenericCapability) PackageCapability(org.apache.felix.resolver.test.util.PackageCapability) Capability(org.osgi.resource.Capability) BundleCapability(org.apache.felix.resolver.test.util.BundleCapability) HashMap(java.util.HashMap) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Logger(org.apache.felix.resolver.Logger) Wire(org.osgi.resource.Wire) Wiring(org.osgi.resource.Wiring) Requirement(org.osgi.resource.Requirement) BundleRequirement(org.apache.felix.resolver.test.util.BundleRequirement) GenericRequirement(org.apache.felix.resolver.test.util.GenericRequirement) PackageRequirement(org.apache.felix.resolver.test.util.PackageRequirement) ArrayList(java.util.ArrayList) List(java.util.List) ResolveContextImpl(org.apache.felix.resolver.test.util.ResolveContextImpl) Test(org.junit.Test)

Example 3 with Resolver

use of org.osgi.service.resolver.Resolver in project felix by apache.

the class ResolverTest method testScenario9.

@Test
public void testScenario9() throws Exception {
    Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG), 1);
    Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
    Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
    List<Resource> mandatory = populateScenario9(wirings, candMap);
    ResolveContextImpl rci = new ResolveContextImpl(wirings, candMap, mandatory, Collections.<Resource>emptyList());
    Map<Resource, List<Wire>> wireMap = resolver.resolve(rci);
    Resource resB = findResource("B", wireMap.keySet());
    Resource resA = findResource("A", wirings.keySet());
    Resource resC1 = findResource("C1", wirings.keySet());
    assertNotNull(resB);
    assertNotNull(resC1);
    assertEquals(1, wireMap.size());
    List<Wire> wiresB = wireMap.get(resB);
    assertEquals(2, wiresB.size());
    // should be wired to A and C1
    assertEquals(resA, wiresB.get(0).getProvider());
    assertEquals(resC1, wiresB.get(1).getProvider());
}
Also used : Resolver(org.osgi.service.resolver.Resolver) GenericCapability(org.apache.felix.resolver.test.util.GenericCapability) PackageCapability(org.apache.felix.resolver.test.util.PackageCapability) Capability(org.osgi.resource.Capability) BundleCapability(org.apache.felix.resolver.test.util.BundleCapability) HashMap(java.util.HashMap) Resource(org.osgi.resource.Resource) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Logger(org.apache.felix.resolver.Logger) Wire(org.osgi.resource.Wire) Wiring(org.osgi.resource.Wiring) Requirement(org.osgi.resource.Requirement) BundleRequirement(org.apache.felix.resolver.test.util.BundleRequirement) GenericRequirement(org.apache.felix.resolver.test.util.GenericRequirement) PackageRequirement(org.apache.felix.resolver.test.util.PackageRequirement) ArrayList(java.util.ArrayList) List(java.util.List) ResolveContextImpl(org.apache.felix.resolver.test.util.ResolveContextImpl) Test(org.junit.Test)

Example 4 with Resolver

use of org.osgi.service.resolver.Resolver in project felix by apache.

the class ResolverTest method testScenario7.

@Test
public void testScenario7() throws Exception {
    Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG));
    Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
    Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
    List<Resource> mandatory = populateScenario7(wirings, candMap);
    ResolveContextImpl rci = new ResolveContextImpl(wirings, candMap, mandatory, Collections.<Resource>emptyList());
    Map<Resource, List<Wire>> wireMap = resolver.resolve(rci);
    assertEquals(4, wireMap.size());
    Resource aRes = findResource("A", wireMap.keySet());
    List<Wire> aWires = wireMap.get(aRes);
    assertEquals(0, aWires.size());
    Resource f1Res = findResource("F1", wireMap.keySet());
    List<Wire> f1Wires = wireMap.get(f1Res);
    assertEquals(1, f1Wires.size());
    Wire f1Wire = f1Wires.get(0);
    assertEquals(f1Res, f1Wire.getRequirer());
    assertEquals(aRes, f1Wire.getProvider());
    Requirement req = f1Wire.getRequirement();
    assertEquals(HostNamespace.HOST_NAMESPACE, req.getNamespace());
    assertEquals(0, req.getAttributes().size());
    assertEquals(f1Res, req.getResource());
    assertEquals(1, req.getDirectives().size());
    assertEquals("(osgi.wiring.host=A)", req.getDirectives().get("filter"));
    Capability cap = f1Wire.getCapability();
    assertEquals(aRes, cap.getResource());
    assertEquals(HostNamespace.HOST_NAMESPACE, cap.getNamespace());
    assertEquals(0, cap.getDirectives().size());
    assertEquals(1, cap.getAttributes().size());
    assertEquals("A", cap.getAttributes().get(HostNamespace.HOST_NAMESPACE));
    Resource f2Res = findResource("F2", wireMap.keySet());
    List<Wire> f2Wires = wireMap.get(f2Res);
    assertEquals(1, f2Wires.size());
    Wire f2Wire = f2Wires.get(0);
    assertEquals(f2Res, f2Wire.getRequirer());
    assertEquals(aRes, f2Wire.getProvider());
    Requirement req2 = f2Wire.getRequirement();
    assertEquals(HostNamespace.HOST_NAMESPACE, req2.getNamespace());
    assertEquals(0, req2.getAttributes().size());
    assertEquals(f2Res, req2.getResource());
    assertEquals(1, req2.getDirectives().size());
    assertEquals("(osgi.wiring.host=A)", req2.getDirectives().get("filter"));
    Capability cap2 = f1Wire.getCapability();
    assertEquals(aRes, cap2.getResource());
    assertEquals(HostNamespace.HOST_NAMESPACE, cap2.getNamespace());
    assertEquals(0, cap2.getDirectives().size());
    assertEquals(1, cap2.getAttributes().size());
    assertEquals("A", cap2.getAttributes().get(HostNamespace.HOST_NAMESPACE));
    Resource bRes = findResource("B", wireMap.keySet());
    List<Wire> bWires = wireMap.get(bRes);
    assertEquals(1, bWires.size());
    Wire bWire = bWires.get(0);
    assertEquals(bRes, bWire.getRequirer());
    assertEquals(f2Res, bWire.getProvider());
    Requirement bReq = bWire.getRequirement();
    assertEquals(IdentityNamespace.IDENTITY_NAMESPACE, bReq.getNamespace());
    assertEquals(0, bReq.getAttributes().size());
    assertEquals(bRes, bReq.getResource());
    assertEquals(1, bReq.getDirectives().size());
    assertEquals("(osgi.identity=F2)", bReq.getDirectives().get("filter"));
}
Also used : Resolver(org.osgi.service.resolver.Resolver) GenericCapability(org.apache.felix.resolver.test.util.GenericCapability) PackageCapability(org.apache.felix.resolver.test.util.PackageCapability) Capability(org.osgi.resource.Capability) BundleCapability(org.apache.felix.resolver.test.util.BundleCapability) HashMap(java.util.HashMap) Resource(org.osgi.resource.Resource) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Logger(org.apache.felix.resolver.Logger) Wire(org.osgi.resource.Wire) Wiring(org.osgi.resource.Wiring) Requirement(org.osgi.resource.Requirement) BundleRequirement(org.apache.felix.resolver.test.util.BundleRequirement) GenericRequirement(org.apache.felix.resolver.test.util.GenericRequirement) PackageRequirement(org.apache.felix.resolver.test.util.PackageRequirement) ArrayList(java.util.ArrayList) List(java.util.List) ResolveContextImpl(org.apache.felix.resolver.test.util.ResolveContextImpl) Test(org.junit.Test)

Example 5 with Resolver

use of org.osgi.service.resolver.Resolver in project bnd by bndtools.

the class DebugReporter method resource.

private void resource(Resource r) {
    IdentityCapability id = ResourceUtils.getIdentityCapability(r);
    String resolveError = null;
    if (!context.isSystemResource(r) && level >= 3 && id != null)
        try {
            String v = id.version() == null ? null : id.version().toString();
            Requirement req = CapReqBuilder.createBundleRequirement(id.osgi_identity(), v).buildSyntheticRequirement();
            context.setInputRequirements(req);
            Resolver resolver = new BndResolver(new ResolverLogger(4));
            Map<Resource, List<Wire>> resolved = resolver.resolve(context);
        } catch (Exception e) {
            resolveError = e.toString();
        }
    String s = resolveError == null ? " " : "!";
    if (id == null)
        out.printf("%s %s%n", s, r);
    else
        out.printf("%s %-50s %-20s %s%n", s, id.osgi_identity(), noNull(id.version()), id.description(""));
    if (level >= 2) {
        for (Capability c : r.getCapabilities(null)) {
            capability("   ", c);
        }
        for (Requirement rq : r.getRequirements(null)) {
            requirement("  ", rq);
        }
    }
}
Also used : Requirement(org.osgi.resource.Requirement) Resolver(org.osgi.service.resolver.Resolver) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Capability(org.osgi.resource.Capability) Wire(org.osgi.resource.Wire) HashMap(java.util.HashMap) Map(java.util.Map) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)

Aggregations

Resolver (org.osgi.service.resolver.Resolver)28 ArrayList (java.util.ArrayList)23 Resource (org.osgi.resource.Resource)23 Requirement (org.osgi.resource.Requirement)22 List (java.util.List)21 Capability (org.osgi.resource.Capability)16 HashMap (java.util.HashMap)14 ResolverImpl (org.apache.felix.resolver.ResolverImpl)14 Logger (org.apache.felix.resolver.Logger)12 ResolveContextImpl (org.apache.felix.resolver.test.util.ResolveContextImpl)11 Wire (org.osgi.resource.Wire)11 BundleCapability (org.apache.felix.resolver.test.util.BundleCapability)10 BundleRequirement (org.apache.felix.resolver.test.util.BundleRequirement)10 GenericCapability (org.apache.felix.resolver.test.util.GenericCapability)10 GenericRequirement (org.apache.felix.resolver.test.util.GenericRequirement)10 PackageCapability (org.apache.felix.resolver.test.util.PackageCapability)10 PackageRequirement (org.apache.felix.resolver.test.util.PackageRequirement)10 Test (org.junit.Test)10 Wiring (org.osgi.resource.Wiring)10 ResolutionException (org.osgi.service.resolver.ResolutionException)8