Search in sources :

Example 31 with Resource

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

the class SubsystemResolveContext method computeDistances.

private Map<Resource, Integer> computeDistances(Resource root) {
    Map<Resource, Integer> distance = new HashMap<>();
    Set<Resource> settledNodes = new HashSet<>();
    distance.put(root, 0);
    List<Resource> unSettledNodes = new ArrayList<>();
    unSettledNodes.add(root);
    while (!unSettledNodes.isEmpty()) {
        unSettledNodes.sort(Comparator.comparingInt(r -> distance.getOrDefault(r, Integer.MAX_VALUE)));
        Resource node = unSettledNodes.remove(0);
        if (settledNodes.add(node)) {
            Map<Resource, Integer> edge = computeEdges(node);
            for (Resource target : edge.keySet()) {
                int d = distance.getOrDefault(node, Integer.MAX_VALUE) + edge.get(target);
                distance.merge(target, d, Math::min);
                if (!settledNodes.contains(target)) {
                    unSettledNodes.add(target);
                }
            }
        }
    }
    return distance;
}
Also used : ResourceImpl(org.apache.karaf.features.internal.resolver.ResourceImpl) Requirement(org.osgi.resource.Requirement) HostedCapability(org.osgi.service.resolver.HostedCapability) BUNDLE_SYMBOLICNAME_ATTRIBUTE(org.osgi.framework.Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE) ResolverUtil(org.apache.karaf.features.internal.resolver.ResolverUtil) ResourceUtils.addIdentityRequirement(org.apache.karaf.features.internal.resolver.ResourceUtils.addIdentityRequirement) Region(org.eclipse.equinox.region.Region) HashMap(java.util.HashMap) FeaturesService(org.apache.karaf.features.FeaturesService) RequirementImpl(org.apache.karaf.features.internal.resolver.RequirementImpl) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Capability(org.osgi.resource.Capability) ResolveContext(org.osgi.service.resolver.ResolveContext) ResourceUtils.getUri(org.apache.karaf.features.internal.resolver.ResourceUtils.getUri) Map(java.util.Map) BaseRepository(org.apache.karaf.features.internal.repository.BaseRepository) RESOLUTION_OPTIONAL(org.osgi.framework.Constants.RESOLUTION_OPTIONAL) Downloader(org.apache.karaf.features.internal.download.Downloader) Repository(org.osgi.service.repository.Repository) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleException(org.osgi.framework.BundleException) MalformedURLException(java.net.MalformedURLException) CapabilityImpl(org.apache.karaf.features.internal.resolver.CapabilityImpl) Resource(org.osgi.resource.Resource) Collection(java.util.Collection) Set(java.util.Set) Wiring(org.osgi.resource.Wiring) RegionFilter(org.eclipse.equinox.region.RegionFilter) REQUIREMENT_RESOLUTION_DIRECTIVE(org.osgi.resource.Namespace.REQUIREMENT_RESOLUTION_DIRECTIVE) Objects(java.util.Objects) IDENTITY_NAMESPACE(org.osgi.framework.namespace.IdentityNamespace.IDENTITY_NAMESPACE) List(java.util.List) VISIBLE_BUNDLE_NAMESPACE(org.eclipse.equinox.region.RegionFilter.VISIBLE_BUNDLE_NAMESPACE) ServiceNamespace(org.osgi.namespace.service.ServiceNamespace) PackageNamespace(org.osgi.framework.namespace.PackageNamespace) RegionDigraph(org.eclipse.equinox.region.RegionDigraph) CAPABILITY_VERSION_ATTRIBUTE(org.osgi.framework.namespace.IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE) RESOLUTION_DIRECTIVE(org.osgi.framework.Constants.RESOLUTION_DIRECTIVE) BUNDLE_VERSION_ATTRIBUTE(org.osgi.framework.Constants.BUNDLE_VERSION_ATTRIBUTE) Comparator(java.util.Comparator) Collections(java.util.Collections) HashMap(java.util.HashMap) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 32 with Resource

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

the class SubsystemResolveContext method findProviders.

@Override
public List<Capability> findProviders(Requirement requirement) {
    List<Capability> caps = new ArrayList<>();
    Region requirerRegion = getRegion(requirement.getResource());
    if (requirerRegion != null) {
        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);
                }
            }
        }
        // Use the digraph to prune non visible capabilities
        Visitor visitor = new Visitor(caps);
        requirerRegion.visitSubgraph(visitor);
        Collection<Capability> allowed = visitor.getAllowed();
        caps.retainAll(allowed);
        // the parent one
        if (caps.size() > 1) {
            Set<Resource> providers = new HashSet<>();
            for (Capability cap : caps) {
                Resource resource = cap.getResource();
                String id = ResolverUtil.getSymbolicName(resource) + "|" + ResolverUtil.getVersion(resource);
                if (!providers.contains(resource)) {
                    Set<Resource> oldRes = new HashSet<>(providers);
                    providers.clear();
                    String r1 = getRegion(resource).getName();
                    boolean superceded = false;
                    for (Resource r : oldRes) {
                        String id2 = ResolverUtil.getSymbolicName(r) + "|" + ResolverUtil.getVersion(r);
                        if (id.equals(id2)) {
                            String r2 = getRegion(r).getName();
                            if (r1.equals(r2)) {
                                if (r instanceof BundleRevision) {
                                    providers.add(r);
                                    superceded = true;
                                } else if (resource instanceof BundleRevision) {
                                    providers.add(resource);
                                } else {
                                    throw new InternalError();
                                }
                            } else if (r1.startsWith(r2)) {
                                providers.add(r);
                                superceded = true;
                            } else if (r2.startsWith(r1)) {
                                providers.add(resource);
                            } else {
                                providers.add(r);
                            }
                        } else {
                            providers.add(r);
                        }
                    }
                    if (!superceded) {
                        providers.add(resource);
                    }
                }
            }
            caps.removeIf(cap -> !providers.contains(cap.getResource()));
        }
        // Sort caps
        if (distance != null) {
            caps.sort(candidateComparator);
        }
    }
    return caps;
}
Also used : HostedCapability(org.osgi.service.resolver.HostedCapability) Capability(org.osgi.resource.Capability) ArrayList(java.util.ArrayList) Resource(org.osgi.resource.Resource) Requirement(org.osgi.resource.Requirement) ResourceUtils.addIdentityRequirement(org.apache.karaf.features.internal.resolver.ResourceUtils.addIdentityRequirement) BundleRevision(org.osgi.framework.wiring.BundleRevision) Region(org.eclipse.equinox.region.Region) Collection(java.util.Collection) HashSet(java.util.HashSet)

Example 33 with Resource

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

the class SubsystemResolver method toJson.

private Object toJson(Map<Resource, List<Wire>> wiring) {
    Map<String, List<Map<String, Object>>> wires = new HashMap<>();
    for (Map.Entry<Resource, List<Wire>> reswiring : wiring.entrySet()) {
        Resource resource = reswiring.getKey();
        String id = toString(resource);
        List<Map<String, Object>> reswires = new ArrayList<>();
        for (Wire w : reswiring.getValue()) {
            Map<String, Object> rw = new LinkedHashMap<>();
            rw.put("requirement", toString(w.getRequirement()));
            rw.put("capability", toString(w.getCapability()));
            rw.put("requirer", toString(w.getRequirer()));
            rw.put("provider", toString(w.getProvider()));
            reswires.add(rw);
        }
        wires.put(id, reswires);
    }
    return wires;
}
Also used : Resource(org.osgi.resource.Resource) Wire(org.osgi.resource.Wire) DictionaryAsMap(org.apache.felix.utils.collections.DictionaryAsMap)

Example 34 with Resource

use of org.osgi.resource.Resource 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 35 with Resource

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

the class OfflineResolver method readRepository.

private static Repository readRepository(Object repository) throws BundleException {
    List<Resource> resources = new ArrayList<>();
    Collection<Map<String, List<String>>> metadatas;
    if (repository instanceof Map) {
        metadatas = ((Map<String, Map<String, List<String>>>) repository).values();
    } else {
        metadatas = (Collection<Map<String, List<String>>>) repository;
    }
    for (Map<String, List<String>> metadata : metadatas) {
        ResourceImpl res = new ResourceImpl();
        for (String cap : metadata.get("capabilities")) {
            res.addCapabilities(ResourceBuilder.parseCapability(res, cap));
        }
        if (metadata.containsKey("requirements")) {
            for (String req : metadata.get("requirements")) {
                res.addRequirements(ResourceBuilder.parseRequirement(res, req));
            }
        }
        resources.add(res);
    }
    return new BaseRepository(resources);
}
Also used : ResourceImpl(org.apache.karaf.features.internal.resolver.ResourceImpl) ArrayList(java.util.ArrayList) Resource(org.osgi.resource.Resource) BaseRepository(org.apache.karaf.features.internal.repository.BaseRepository) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Aggregations

Resource (org.osgi.resource.Resource)201 Capability (org.osgi.resource.Capability)62 Requirement (org.osgi.resource.Requirement)62 ArrayList (java.util.ArrayList)57 List (java.util.List)39 HashMap (java.util.HashMap)32 File (java.io.File)27 Collection (java.util.Collection)24 Wire (org.osgi.resource.Wire)24 ResourceBuilder (aQute.bnd.osgi.resource.ResourceBuilder)23 Map (java.util.Map)22 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)18 URI (java.net.URI)18 HashSet (java.util.HashSet)18 BndEditModel (aQute.bnd.build.model.BndEditModel)15 MockRegistry (test.lib.MockRegistry)15 Version (org.osgi.framework.Version)14 BundleRevision (org.osgi.framework.wiring.BundleRevision)14 Repository (org.osgi.service.repository.Repository)14 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)13