Search in sources :

Example 21 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 22 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 23 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 24 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)

Example 25 with Capability

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

the class SubsystemResource method findContent.

private Resource findContent(Requirement requirement) throws BundleException, IOException, InvalidSyntaxException, URISyntaxException {
    Map<Requirement, Collection<Capability>> map;
    // the case of a persisted subsystem.
    if (isUnscoped()) {
        map = Activator.getInstance().getSystemRepository().findProviders(Collections.singleton(requirement));
        if (map.containsKey(requirement)) {
            Collection<Capability> capabilities = map.get(requirement);
            for (Capability capability : capabilities) {
                Resource provider = capability.getResource();
                if (provider instanceof BundleRevision) {
                    if (getRegion().contains(((BundleRevision) provider).getBundle())) {
                        return provider;
                    }
                } else if (provider instanceof BasicSubsystem) {
                    if (getRegion().equals(((BasicSubsystem) provider).getRegion())) {
                        return provider;
                    }
                }
            }
        }
    }
    // First search the local repository.
    map = resource.getLocalRepository().findProviders(Collections.singleton(requirement));
    Collection<Capability> capabilities = map.get(requirement);
    if (capabilities.isEmpty()) {
        // Nothing found in the local repository so search the repository services.
        capabilities = new RepositoryServiceRepository().findProviders(requirement);
    }
    if (capabilities.isEmpty()) {
        // Nothing found period.
        return null;
    }
    for (Capability capability : capabilities) {
        if (!IdentityNamespace.TYPE_FRAGMENT.equals(capability.getAttributes().get(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE))) {
            // See ARIES-1425.
            return capability.getResource();
        }
    }
    // Nothing here but fragment bundles. Return the first one.
    return capabilities.iterator().next().getResource();
}
Also used : RequireBundleRequirement(org.apache.aries.subsystem.core.archive.RequireBundleRequirement) Requirement(org.osgi.resource.Requirement) RequireCapabilityRequirement(org.apache.aries.subsystem.core.archive.RequireCapabilityRequirement) ImportPackageRequirement(org.apache.aries.subsystem.core.archive.ImportPackageRequirement) SubsystemImportServiceRequirement(org.apache.aries.subsystem.core.archive.SubsystemImportServiceRequirement) Capability(org.osgi.resource.Capability) Resource(org.osgi.resource.Resource) BundleRevision(org.osgi.framework.wiring.BundleRevision) Collection(java.util.Collection)

Aggregations

Capability (org.osgi.resource.Capability)197 Requirement (org.osgi.resource.Requirement)104 Resource (org.osgi.resource.Resource)61 ArrayList (java.util.ArrayList)40 HashMap (java.util.HashMap)39 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)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)20 List (java.util.List)19 Repository (org.osgi.service.repository.Repository)17 Test (org.junit.Test)16 File (java.io.File)15 HostedCapability (org.osgi.service.resolver.HostedCapability)13 HashSet (java.util.HashSet)12 Map (java.util.Map)12 LinkedList (java.util.LinkedList)11 RequirementBuilder (aQute.bnd.osgi.resource.RequirementBuilder)8