Search in sources :

Example 1 with CandidateComparator

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

InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 CandidateComparator (org.apache.felix.resolver.test.util.CandidateComparator)1 CapabilitySet (org.apache.felix.resolver.test.util.CapabilitySet)1 GenericCapability (org.apache.felix.resolver.test.util.GenericCapability)1 GenericRequirement (org.apache.felix.resolver.test.util.GenericRequirement)1 ResourceImpl (org.apache.felix.resolver.test.util.ResourceImpl)1 SimpleFilter (org.apache.felix.resolver.test.util.SimpleFilter)1 Capability (org.osgi.resource.Capability)1 Requirement (org.osgi.resource.Requirement)1 Resource (org.osgi.resource.Resource)1 Wiring (org.osgi.resource.Wiring)1 HostedCapability (org.osgi.service.resolver.HostedCapability)1 ResolveContext (org.osgi.service.resolver.ResolveContext)1