Search in sources :

Example 26 with Capability

use of org.osgi.resource.Capability in project karaf by apache.

the class AggregateRepository method findProviders.

@Override
public Map<Requirement, Collection<Capability>> findProviders(Collection<? extends Requirement> requirements) {
    Map<Requirement, Collection<Capability>> result = new HashMap<>();
    for (Requirement requirement : requirements) {
        List<Capability> caps = new ArrayList<>();
        for (Repository repository : repositories) {
            Map<Requirement, Collection<Capability>> resMap = repository.findProviders(Collections.singleton(requirement));
            Collection<Capability> res = resMap != null ? resMap.get(requirement) : null;
            if (res != null) {
                caps.addAll(res);
            }
        }
        result.put(requirement, caps);
    }
    return result;
}
Also used : Requirement(org.osgi.resource.Requirement) Repository(org.osgi.service.repository.Repository) Capability(org.osgi.resource.Capability) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Collection(java.util.Collection)

Example 27 with Capability

use of org.osgi.resource.Capability 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 28 with Capability

use of org.osgi.resource.Capability in project karaf by apache.

the class XmlRepository method addResource.

@Override
protected void addResource(Resource resource) {
    List<Capability> identities = resource.getCapabilities(IDENTITY_NAMESPACE);
    if (identities.isEmpty()) {
        throw new IllegalStateException("Invalid resource: a capability with 'osgi.identity' namespace is required");
    } else if (identities.size() > 1) {
        throw new IllegalStateException("Invalid resource: multiple 'osgi.identity' capabilities found");
    }
    Capability identity = identities.get(0);
    Object name = identity.getAttributes().get(IDENTITY_NAMESPACE);
    Object type = identity.getAttributes().get(CAPABILITY_TYPE_ATTRIBUTE);
    Object vers = identity.getAttributes().get(CAPABILITY_VERSION_ATTRIBUTE);
    if (!String.class.isInstance(name) || !String.class.isInstance(type) || !Version.class.isInstance(vers)) {
        throw new IllegalStateException("Invalid osgi.identity capability: " + identity);
    }
    if (!hasResource((String) type, (String) name, (Version) vers)) {
        super.addResource(resource);
    }
}
Also used : Capability(org.osgi.resource.Capability) Version(org.osgi.framework.Version)

Example 29 with Capability

use of org.osgi.resource.Capability in project karaf by apache.

the class CapabilitySet method dump.

public void dump() {
    for (Entry<String, Map<Object, Set<Capability>>> entry : indices.entrySet()) {
        boolean header1 = false;
        for (Entry<Object, Set<Capability>> entry2 : entry.getValue().entrySet()) {
            boolean header2 = false;
            for (Capability cap : entry2.getValue()) {
                if (!header1) {
                    System.out.println(entry.getKey() + ":");
                    header1 = true;
                }
                if (!header2) {
                    System.out.println("   " + entry2.getKey());
                    header2 = true;
                }
                System.out.println("      " + cap);
            }
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Capability(org.osgi.resource.Capability) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 30 with Capability

use of org.osgi.resource.Capability in project karaf by apache.

the class SubsystemResolver method associateFragments.

private void associateFragments() {
    SimpleFilter sf = createFilter(IDENTITY_NAMESPACE, "*", CAPABILITY_TYPE_ATTRIBUTE, TYPE_SUBSYSTEM);
    for (Map.Entry<Resource, List<Wire>> entry : wiring.entrySet()) {
        final Resource resource = entry.getKey();
        final Requirement requirement = getSubsystemRequirement(resource);
        if (ResolverUtil.isFragment(resource) && requirement != null) {
            List<Wire> wires = entry.getValue();
            final Resource host = wires.get(0).getProvider();
            final Wire wire = findMatchingWire(sf, wiring.get(host));
            if (wire != null) {
                wires.add(new Wire() {

                    @Override
                    public Capability getCapability() {
                        return wire.getCapability();
                    }

                    @Override
                    public Requirement getRequirement() {
                        return requirement;
                    }

                    @Override
                    public Resource getProvider() {
                        return wire.getProvider();
                    }

                    @Override
                    public Resource getRequirer() {
                        return resource;
                    }
                });
            }
        }
    }
}
Also used : Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) SimpleFilter(org.apache.karaf.features.internal.resolver.SimpleFilter) Resource(org.osgi.resource.Resource) Wire(org.osgi.resource.Wire) DictionaryAsMap(org.apache.felix.utils.collections.DictionaryAsMap)

Aggregations

Capability (org.osgi.resource.Capability)189 Requirement (org.osgi.resource.Requirement)103 Resource (org.osgi.resource.Resource)60 ArrayList (java.util.ArrayList)38 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)37 HashMap (java.util.HashMap)37 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)35 Collection (java.util.Collection)32 MockRegistry (test.lib.MockRegistry)26 BndEditModel (aQute.bnd.build.model.BndEditModel)25 Version (org.osgi.framework.Version)19 List (java.util.List)18 Repository (org.osgi.service.repository.Repository)17 Test (org.junit.Test)16 File (java.io.File)14 HostedCapability (org.osgi.service.resolver.HostedCapability)12 HashSet (java.util.HashSet)11 LinkedList (java.util.LinkedList)11 Map (java.util.Map)11 RequirementBuilder (aQute.bnd.osgi.resource.RequirementBuilder)8