Search in sources :

Example 1 with RequirementImpl

use of org.apache.karaf.features.internal.resolver.RequirementImpl 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 2 with RequirementImpl

use of org.apache.karaf.features.internal.resolver.RequirementImpl in project karaf by apache.

the class StaxParser method parseRequirement.

private static RequirementImpl parseRequirement(XMLStreamReader reader, ResourceImpl resource) throws XMLStreamException {
    String[] namespace = new String[1];
    Map<String, String> directives = new HashMap<>();
    Map<String, Object> attributes = new HashMap<>();
    parseClause(reader, namespace, directives, attributes);
    sanityCheckEndElement(reader, reader.getEventType(), REQUIREMENT);
    String filterStr = directives.get(Constants.FILTER_DIRECTIVE);
    SimpleFilter sf = (filterStr != null) ? SimpleFilter.parse(filterStr) : SimpleFilter.convert(attributes);
    return new RequirementImpl(resource, namespace[0], directives, attributes, sf);
}
Also used : RequirementImpl(org.apache.karaf.features.internal.resolver.RequirementImpl) HashMap(java.util.HashMap) SimpleFilter(org.apache.karaf.features.internal.resolver.SimpleFilter)

Example 3 with RequirementImpl

use of org.apache.karaf.features.internal.resolver.RequirementImpl in project karaf by apache.

the class BaseRepository method findProviders.

@Override
public Map<Requirement, Collection<Capability>> findProviders(Collection<? extends Requirement> requirements) {
    Map<Requirement, Collection<Capability>> result = new HashMap<>();
    for (Requirement requirement : requirements) {
        CapabilitySet set = capSets.get(requirement.getNamespace());
        if (set != null) {
            SimpleFilter sf;
            if (requirement instanceof RequirementImpl) {
                sf = ((RequirementImpl) requirement).getFilter();
            } else {
                String filter = requirement.getDirectives().get(Constants.FILTER_DIRECTIVE);
                sf = (filter != null) ? SimpleFilter.parse(filter) : new SimpleFilter(null, null, SimpleFilter.MATCH_ALL);
            }
            result.put(requirement, set.match(sf, true));
        } else {
            result.put(requirement, Collections.emptyList());
        }
    }
    return result;
}
Also used : Requirement(org.osgi.resource.Requirement) RequirementImpl(org.apache.karaf.features.internal.resolver.RequirementImpl) HashMap(java.util.HashMap) SimpleFilter(org.apache.karaf.features.internal.resolver.SimpleFilter) Collection(java.util.Collection) CapabilitySet(org.apache.karaf.features.internal.resolver.CapabilitySet)

Example 4 with RequirementImpl

use of org.apache.karaf.features.internal.resolver.RequirementImpl in project karaf by apache.

the class SubsystemResolver method toJson.

private Object toJson(Repository repository) {
    Requirement req = new RequirementImpl(null, IDENTITY_NAMESPACE, Collections.emptyMap(), Collections.emptyMap(), new SimpleFilter(null, null, SimpleFilter.MATCH_ALL));
    Collection<Capability> identities = repository.findProviders(Collections.singleton(req)).get(req);
    List<Object> resources = new ArrayList<>();
    for (Capability identity : identities) {
        String id = BaseClause.toString(null, identity.getNamespace(), identity.getAttributes(), identity.getDirectives());
        resources.add(toJson(identity.getResource()));
    }
    return resources;
}
Also used : Requirement(org.osgi.resource.Requirement) RequirementImpl(org.apache.karaf.features.internal.resolver.RequirementImpl) Capability(org.osgi.resource.Capability) SimpleFilter(org.apache.karaf.features.internal.resolver.SimpleFilter)

Example 5 with RequirementImpl

use of org.apache.karaf.features.internal.resolver.RequirementImpl in project karaf by apache.

the class Subsystem method cloneResource.

ResourceImpl cloneResource(Resource resource) {
    ResourceImpl res = new ResourceImpl();
    for (Capability cap : resource.getCapabilities(null)) {
        res.addCapability(new CapabilityImpl(res, cap.getNamespace(), new HashMap<>(cap.getDirectives()), new HashMap<>(cap.getAttributes())));
    }
    for (Requirement req : resource.getRequirements(null)) {
        SimpleFilter sf;
        if (req instanceof RequirementImpl) {
            sf = ((RequirementImpl) req).getFilter();
        } else if (req.getDirectives().containsKey(REQUIREMENT_FILTER_DIRECTIVE)) {
            sf = SimpleFilter.parse(req.getDirectives().get(REQUIREMENT_FILTER_DIRECTIVE));
        } else {
            sf = SimpleFilter.convert(req.getAttributes());
        }
        res.addRequirement(new RequirementImpl(res, req.getNamespace(), new HashMap<>(req.getDirectives()), new HashMap<>(req.getAttributes()), sf));
    }
    return res;
}
Also used : ResourceUtils.addIdentityRequirement(org.apache.karaf.features.internal.resolver.ResourceUtils.addIdentityRequirement) ResourceUtils.toFeatureRequirement(org.apache.karaf.features.internal.resolver.ResourceUtils.toFeatureRequirement) Requirement(org.osgi.resource.Requirement) ResourceImpl(org.apache.karaf.features.internal.resolver.ResourceImpl) CapabilityImpl(org.apache.karaf.features.internal.resolver.CapabilityImpl) RequirementImpl(org.apache.karaf.features.internal.resolver.RequirementImpl) Capability(org.osgi.resource.Capability) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SimpleFilter(org.apache.karaf.features.internal.resolver.SimpleFilter)

Aggregations

RequirementImpl (org.apache.karaf.features.internal.resolver.RequirementImpl)5 SimpleFilter (org.apache.karaf.features.internal.resolver.SimpleFilter)4 Requirement (org.osgi.resource.Requirement)4 HashMap (java.util.HashMap)3 Capability (org.osgi.resource.Capability)3 Collection (java.util.Collection)2 BufferedReader (java.io.BufferedReader)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Logger (org.apache.felix.resolver.Logger)1 ResolverImpl (org.apache.felix.resolver.ResolverImpl)1 BaseRepository (org.apache.karaf.features.internal.repository.BaseRepository)1 CapabilityImpl (org.apache.karaf.features.internal.resolver.CapabilityImpl)1 CapabilitySet (org.apache.karaf.features.internal.resolver.CapabilitySet)1 ResourceImpl (org.apache.karaf.features.internal.resolver.ResourceImpl)1 ResourceUtils.addIdentityRequirement (org.apache.karaf.features.internal.resolver.ResourceUtils.addIdentityRequirement)1 ResourceUtils.toFeatureRequirement (org.apache.karaf.features.internal.resolver.ResourceUtils.toFeatureRequirement)1 Resource (org.osgi.resource.Resource)1