Search in sources :

Example 26 with Requirement

use of org.osgi.resource.Requirement 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 Requirement

use of org.osgi.resource.Requirement 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 Requirement

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

the class Subsystem method doBuild.

private void doBuild(Collection<Feature> features, boolean mandatory) throws Exception {
    for (Subsystem child : children) {
        child.doBuild(features, true);
    }
    if (feature != null) {
        for (Dependency dep : feature.getDependencies()) {
            Subsystem ss = this;
            while (!ss.isAcceptDependencies()) {
                ss = ss.getParent();
            }
            ss.requireFeature(dep.getName(), dep.getVersion(), false);
        }
        for (Conditional cond : feature.getConditional()) {
            Feature fcond = cond.asFeature();
            String ssName = this.name + "#" + (fcond.hasVersion() ? fcond.getName() + "-" + fcond.getVersion() : fcond.getName());
            Subsystem fs = getChild(ssName);
            if (fs == null) {
                fs = new Subsystem(ssName, fcond, this, true);
                fs.doBuild(features, false);
                installable.add(fs);
                children.add(fs);
            }
        }
    }
    List<Requirement> processed = new ArrayList<>();
    while (true) {
        List<Requirement> requirements = getRequirements(IDENTITY_NAMESPACE);
        requirements.addAll(dependentFeatures);
        requirements.removeAll(processed);
        if (requirements.isEmpty()) {
            break;
        }
        for (Requirement requirement : requirements) {
            String name = (String) requirement.getAttributes().get(IDENTITY_NAMESPACE);
            String type = (String) requirement.getAttributes().get(CAPABILITY_TYPE_ATTRIBUTE);
            VersionRange range = (VersionRange) requirement.getAttributes().get(CAPABILITY_VERSION_ATTRIBUTE);
            if (TYPE_FEATURE.equals(type)) {
                for (Feature feature : features) {
                    if (feature.getName().equals(name) && (range == null || range.contains(VersionTable.getVersion(feature.getVersion())))) {
                        if (feature != this.feature) {
                            String ssName = this.name + "#" + (feature.hasVersion() ? feature.getName() + "-" + feature.getVersion() : feature.getName());
                            Subsystem fs = getChild(ssName);
                            if (fs == null) {
                                fs = new Subsystem(ssName, feature, this, mandatory && !SubsystemResolveContext.isOptional(requirement));
                                fs.build(features);
                                installable.add(fs);
                                children.add(fs);
                            }
                        }
                    }
                }
            }
            processed.add(requirement);
        }
    }
}
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) ArrayList(java.util.ArrayList) Conditional(org.apache.karaf.features.Conditional) VersionRange(org.apache.felix.utils.version.VersionRange) Dependency(org.apache.karaf.features.Dependency) Feature(org.apache.karaf.features.Feature)

Example 29 with Requirement

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

the class FeatureResource method build.

public static FeatureResource build(Feature feature, Conditional conditional, String featureRange, Map<String, ? extends Resource> locToRes) throws BundleException {
    Feature fcond = conditional.asFeature();
    FeatureResource resource = build(fcond, featureRange, locToRes);
    for (String cond : conditional.getCondition()) {
        if (cond.startsWith("req:")) {
            cond = cond.substring("req:".length());
            List<Requirement> reqs = ResourceBuilder.parseRequirement(resource, cond);
            resource.addRequirements(reqs);
        } else {
            org.apache.karaf.features.internal.model.Dependency dep = new org.apache.karaf.features.internal.model.Dependency();
            String[] p = cond.split("/");
            dep.setName(p[0]);
            if (p.length > 1) {
                dep.setVersion(p[1]);
            }
            addDependency(resource, dep, featureRange, true);
        }
    }
    org.apache.karaf.features.internal.model.Dependency dep = new org.apache.karaf.features.internal.model.Dependency();
    dep.setName(feature.getName());
    dep.setVersion(feature.getVersion());
    addDependency(resource, dep, featureRange, true);
    return resource;
}
Also used : Dependency(org.apache.karaf.features.Dependency) Feature(org.apache.karaf.features.Feature) Requirement(org.osgi.resource.Requirement) ResourceUtils.addIdentityRequirement(org.apache.karaf.features.internal.resolver.ResourceUtils.addIdentityRequirement)

Example 30 with Requirement

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

the class ResourceBuilder method convertImportService.

private static List<Requirement> convertImportService(List<ParsedHeaderClause> clauses, Resource resource) throws BundleException {
    try {
        List<Requirement> reqList = new ArrayList<>();
        for (ParsedHeaderClause clause : clauses) {
            for (String path : clause.paths) {
                String multiple = clause.dirs.get("multiple");
                String avail = clause.dirs.get("availability");
                String filter = (String) clause.attrs.get("filter");
                Map<String, String> dirs = new LinkedHashMap<>();
                dirs.put(ServiceNamespace.REQUIREMENT_EFFECTIVE_DIRECTIVE, ServiceNamespace.EFFECTIVE_ACTIVE);
                if ("optional".equals(avail)) {
                    dirs.put(ServiceNamespace.REQUIREMENT_RESOLUTION_DIRECTIVE, ServiceNamespace.RESOLUTION_OPTIONAL);
                }
                if ("true".equals(multiple)) {
                    dirs.put(ServiceNamespace.REQUIREMENT_CARDINALITY_DIRECTIVE, ServiceNamespace.CARDINALITY_MULTIPLE);
                }
                if (filter == null) {
                    filter = "(" + Constants.OBJECTCLASS + "=" + path + ")";
                } else if (!filter.startsWith("(") && !filter.endsWith(")")) {
                    filter = "(&(" + Constants.OBJECTCLASS + "=" + path + ")(" + filter + "))";
                } else {
                    filter = "(&(" + Constants.OBJECTCLASS + "=" + path + ")" + filter + ")";
                }
                dirs.put(ServiceNamespace.REQUIREMENT_FILTER_DIRECTIVE, filter);
                reqList.add(new RequirementImpl(resource, ServiceNamespace.SERVICE_NAMESPACE, dirs, Collections.emptyMap(), SimpleFilter.parse(filter)));
            }
        }
        return reqList;
    } catch (Exception ex) {
        throw new BundleException("Error creating requirement: " + ex, ex);
    }
}
Also used : Requirement(org.osgi.resource.Requirement) ArrayList(java.util.ArrayList) BundleException(org.osgi.framework.BundleException) BundleException(org.osgi.framework.BundleException) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Requirement (org.osgi.resource.Requirement)193 Capability (org.osgi.resource.Capability)102 Resource (org.osgi.resource.Resource)59 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)53 ArrayList (java.util.ArrayList)46 Collection (java.util.Collection)44 BndEditModel (aQute.bnd.build.model.BndEditModel)35 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)34 HashMap (java.util.HashMap)34 MockRegistry (test.lib.MockRegistry)32 List (java.util.List)19 Repository (org.osgi.service.repository.Repository)19 Test (org.junit.Test)18 File (java.io.File)14 Map (java.util.Map)12 Resolver (org.osgi.service.resolver.Resolver)11 LinkedHashSet (java.util.LinkedHashSet)10 LinkedList (java.util.LinkedList)10 Wire (org.osgi.resource.Wire)10 Parameters (aQute.bnd.header.Parameters)9