Search in sources :

Example 1 with CapabilitySet

use of org.apache.felix.resolver.test.util.CapabilitySet 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 CapabilitySet

use of org.apache.felix.resolver.test.util.CapabilitySet in project felix by apache.

the class BigResolutionTest method buildResolutionContext.

private ResolveContext buildResolutionContext() throws IOException, BundleException {
    Object resolution;
    InputStream is = getClass().getClassLoader().getResourceAsStream("resolution.json");
    try {
        resolution = JsonReader.read(is);
    } finally {
        is.close();
    }
    List<Resource> resources = new ArrayList<Resource>();
    ResourceImpl system = new ResourceImpl("system-bundle");
    parseCapability(system, "osgi.ee; osgi.ee=JavaSE; version=1.5");
    parseCapability(system, "osgi.ee; osgi.ee=JavaSE; version=1.6");
    parseCapability(system, "osgi.ee; osgi.ee=JavaSE; version=1.7");
    resources.add(system);
    for (Object r : (Collection) ((Map) resolution).get("resources")) {
        resources.add(parseResource(r));
    }
    final List<Resource> mandatory = new ArrayList<Resource>();
    for (Object r : (Collection) ((Map) resolution).get("mandatory")) {
        mandatory.add(parseResource(r));
    }
    final Map<String, CapabilitySet> capSets = new HashMap<String, CapabilitySet>();
    CapabilitySet svcSet = new CapabilitySet(Collections.singletonList("objectClass"));
    capSets.put("osgi.service", svcSet);
    for (Resource resource : resources) {
        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);
        }
    }
    return new ResolveContext() {

        @Override
        public Collection<Resource> getMandatoryResources() {
            return mandatory;
        }

        @Override
        public List<Capability> findProviders(Requirement requirement) {
            SimpleFilter sf;
            if (requirement.getDirectives().containsKey("filter")) {
                sf = SimpleFilter.parse(requirement.getDirectives().get("filter"));
            } else {
                sf = SimpleFilter.convert(requirement.getAttributes());
            }
            CapabilitySet set = capSets.get(requirement.getNamespace());
            List<Capability> caps = new ArrayList<Capability>(set.match(sf, true));
            Collections.sort(caps, new CandidateComparator());
            return caps;
        }

        @Override
        public int insertHostedCapability(List<Capability> capabilities, HostedCapability hostedCapability) {
            capabilities.add(hostedCapability);
            return capabilities.size() - 1;
        }

        @Override
        public boolean isEffective(Requirement requirement) {
            return true;
        }

        @Override
        public Map<Resource, Wiring> getWirings() {
            return Collections.emptyMap();
        }
    };
}
Also used : ResolveContext(org.osgi.service.resolver.ResolveContext) HostedCapability(org.osgi.service.resolver.HostedCapability) GenericCapability(org.apache.felix.resolver.test.util.GenericCapability) Capability(org.osgi.resource.Capability) HashMap(java.util.HashMap) InputStream(java.io.InputStream) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) HostedCapability(org.osgi.service.resolver.HostedCapability) Wiring(org.osgi.resource.Wiring) Requirement(org.osgi.resource.Requirement) GenericRequirement(org.apache.felix.resolver.test.util.GenericRequirement) ResourceImpl(org.apache.felix.resolver.test.util.ResourceImpl) CandidateComparator(org.apache.felix.resolver.test.util.CandidateComparator) SimpleFilter(org.apache.felix.resolver.test.util.SimpleFilter) Collection(java.util.Collection) CapabilitySet(org.apache.felix.resolver.test.util.CapabilitySet) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 CapabilitySet (org.apache.felix.resolver.test.util.CapabilitySet)2 SimpleFilter (org.apache.felix.resolver.test.util.SimpleFilter)2 Capability (org.osgi.resource.Capability)2 Requirement (org.osgi.resource.Requirement)2 Resource (org.osgi.resource.Resource)2 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Collection (java.util.Collection)1 Map (java.util.Map)1 Logger (org.apache.felix.resolver.Logger)1 ResolverImpl (org.apache.felix.resolver.ResolverImpl)1 CandidateComparator (org.apache.felix.resolver.test.util.CandidateComparator)1 GenericCapability (org.apache.felix.resolver.test.util.GenericCapability)1 GenericRequirement (org.apache.felix.resolver.test.util.GenericRequirement)1 ResolveContextImpl (org.apache.felix.resolver.test.util.ResolveContextImpl)1 ResourceImpl (org.apache.felix.resolver.test.util.ResourceImpl)1