Search in sources :

Example 1 with HostedCapability

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

the class IterativeResolver method resolve.

public Map<Resource, List<Wire>> resolve(final ResolveContext context) throws ResolutionException {
    final Map<Resource, List<Wire>> wires = new HashMap<Resource, List<Wire>>();
    final Map<Resource, List<Wire>> invertedWires = new HashMap<Resource, List<Wire>>();
    Collection<Resource> resources = context.getMandatoryResources();
    for (final Resource resource : resources) {
        // Build wiring
        invertedWires.clear();
        for (Resource res : wires.keySet()) {
            for (Wire wire : wires.get(res)) {
                List<Wire> w = invertedWires.get(wire.getProvider());
                if (w == null) {
                    w = new ArrayList<Wire>();
                    invertedWires.put(wire.getProvider(), w);
                }
                w.add(wire);
            }
        }
        final Map<Resource, Wiring> wiring = new HashMap<Resource, Wiring>();
        for (Resource res : wires.keySet()) {
            wiring.put(res, new SimpleWiring(res, wires, invertedWires));
        }
        // Resolve the new resource
        Map<Resource, List<Wire>> tempWires = resolver.resolve(new ResolveContext() {

            @Override
            public Collection<Resource> getMandatoryResources() {
                return Collections.singleton(resource);
            }

            @Override
            public Collection<Resource> getOptionalResources() {
                return context.getOptionalResources();
            }

            @Override
            public List<Capability> findProviders(Requirement requirement) {
                return context.findProviders(requirement);
            }

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

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

            @Override
            public Map<Resource, Wiring> getWirings() {
                return wiring;
            }
        });
        // Merge wiring
        wires.putAll(tempWires);
    }
    return wires;
}
Also used : ResolveContext(org.osgi.service.resolver.ResolveContext) HostedCapability(org.osgi.service.resolver.HostedCapability) Capability(org.osgi.resource.Capability) HashMap(java.util.HashMap) Resource(org.osgi.resource.Resource) Wire(org.osgi.resource.Wire) HostedCapability(org.osgi.service.resolver.HostedCapability) Wiring(org.osgi.resource.Wiring) Requirement(org.osgi.resource.Requirement) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with HostedCapability

use of org.osgi.service.resolver.HostedCapability 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)

Example 3 with HostedCapability

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

the class AbstractResolveContext method insertHostedCapability.

@Override
public int insertHostedCapability(List<Capability> caps, HostedCapability hc) {
    init();
    Integer prioObj = resourcePriorities.get(hc.getResource());
    int priority = prioObj != null ? prioObj.intValue() : Integer.MAX_VALUE;
    for (int i = 0; i < caps.size(); i++) {
        Capability c = caps.get(i);
        Integer otherPrioObj = resourcePriorities.get(c.getResource());
        int otherPriority = otherPrioObj != null ? otherPrioObj.intValue() : 0;
        if (otherPriority > priority) {
            caps.add(i, hc);
            return i;
        }
    }
    int newIndex = caps.size();
    // the List passed by Felix does not support the
    // single-arg version of add()... it throws
    // UnsupportedOperationException
    caps.add(newIndex, hc);
    return newIndex;
}
Also used : HostedCapability(org.osgi.service.resolver.HostedCapability) Capability(org.osgi.resource.Capability)

Example 4 with HostedCapability

use of org.osgi.service.resolver.HostedCapability in project karaf by apache.

the class OfflineResolver method resolve.

public static void resolve(String resolutionFile) throws Exception {
    Map<String, Object> resolution;
    try (BufferedReader reader = Files.newBufferedReader(Paths.get(resolutionFile), StandardCharsets.UTF_8)) {
        resolution = (Map<String, Object>) JsonReader.read(reader);
    }
    final Repository globalRepository;
    if (resolution.containsKey("globalRepository")) {
        globalRepository = readRepository(resolution.get("globalRepository"));
    } else {
        globalRepository = null;
    }
    final Repository repository = readRepository(resolution.get("repository"));
    Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_ERROR));
    Map<Resource, List<Wire>> wiring = resolver.resolve(new ResolveContext() {

        private final CandidateComparator candidateComparator = new CandidateComparator(r -> 0);

        @Override
        public Collection<Resource> getMandatoryResources() {
            List<Resource> resources = new ArrayList<>();
            Requirement req = new RequirementImpl(null, IDENTITY_NAMESPACE, Collections.emptyMap(), Collections.emptyMap(), SimpleFilter.parse("(" + IDENTITY_NAMESPACE + "=root)"));
            Collection<Capability> identities = repository.findProviders(Collections.singleton(req)).get(req);
            for (Capability identity : identities) {
                resources.add(identity.getResource());
            }
            return resources;
        }

        @Override
        public List<Capability> findProviders(Requirement requirement) {
            List<Capability> caps = new ArrayList<>();
            Map<Requirement, Collection<Capability>> resMap = repository.findProviders(Collections.singleton(requirement));
            Collection<Capability> res = resMap != null ? resMap.get(requirement) : null;
            if (res != null && !res.isEmpty()) {
                caps.addAll(res);
            } else if (globalRepository != null) {
                // Only bring in external resources for non optional requirements
                if (!RESOLUTION_OPTIONAL.equals(requirement.getDirectives().get(RESOLUTION_DIRECTIVE))) {
                    resMap = globalRepository.findProviders(Collections.singleton(requirement));
                    res = resMap != null ? resMap.get(requirement) : null;
                    if (res != null && !res.isEmpty()) {
                        caps.addAll(res);
                    }
                }
            }
            // Sort caps
            Collections.sort(caps, candidateComparator);
            return caps;
        }

        @Override
        public int insertHostedCapability(List<Capability> capabilities, HostedCapability hostedCapability) {
            int idx = Collections.binarySearch(capabilities, hostedCapability, candidateComparator);
            if (idx < 0) {
                idx = Math.abs(idx + 1);
            }
            capabilities.add(idx, hostedCapability);
            return idx;
        }

        @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) RequirementImpl(org.apache.karaf.features.internal.resolver.RequirementImpl) Resolver(org.osgi.service.resolver.Resolver) HostedCapability(org.osgi.service.resolver.HostedCapability) Capability(org.osgi.resource.Capability) Resource(org.osgi.resource.Resource) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Logger(org.apache.felix.resolver.Logger) HostedCapability(org.osgi.service.resolver.HostedCapability) Requirement(org.osgi.resource.Requirement) BaseRepository(org.apache.karaf.features.internal.repository.BaseRepository) Repository(org.osgi.service.repository.Repository) BufferedReader(java.io.BufferedReader) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 5 with HostedCapability

use of org.osgi.service.resolver.HostedCapability in project aries by apache.

the class ResolveContextTest method testInsertHostedCapability.

@Test
public void testInsertHostedCapability() throws Exception {
    Activator prev = getActivator();
    try {
        Activator activator = createActivator();
        setActivator(activator);
        SubsystemResource res = new SubsystemResource(new File("."));
        ResolveContext rc = new ResolveContext(res);
        HostedCapability hc = EasyMock.createNiceMock(HostedCapability.class);
        List<Capability> caps = new ArrayList<Capability>() {

            // Must use add(idx, obj), get the other add() overloads to complain
            @Override
            public boolean add(Capability e) {
                throw new UnsupportedOperationException();
            }

            @Override
            public boolean addAll(Collection<? extends Capability> c) {
                throw new UnsupportedOperationException();
            }

            @Override
            public boolean addAll(int index, Collection<? extends Capability> c) {
                throw new UnsupportedOperationException();
            }
        };
        caps.add(0, EasyMock.createNiceMock(HostedCapability.class));
        assertEquals(1, rc.insertHostedCapability(caps, hc));
        assertSame(hc, caps.get(1));
    } finally {
        setActivator(prev);
    }
}
Also used : Capability(org.osgi.resource.Capability) HostedCapability(org.osgi.service.resolver.HostedCapability) ArrayList(java.util.ArrayList) Collection(java.util.Collection) File(java.io.File) HostedCapability(org.osgi.service.resolver.HostedCapability) Test(org.junit.Test)

Aggregations

HostedCapability (org.osgi.service.resolver.HostedCapability)7 Capability (org.osgi.resource.Capability)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 List (java.util.List)3 Requirement (org.osgi.resource.Requirement)3 Resource (org.osgi.resource.Resource)3 ResolveContext (org.osgi.service.resolver.ResolveContext)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Version (org.osgi.framework.Version)2 Wiring (org.osgi.resource.Wiring)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 InputStream (java.io.InputStream)1 Logger (org.apache.felix.resolver.Logger)1 ResolverImpl (org.apache.felix.resolver.ResolverImpl)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