Search in sources :

Example 61 with Capability

use of org.osgi.resource.Capability in project bndtools by bndtools.

the class BndBuilderCapReqLoader method loadCapabilities.

@Override
public Map<String, List<Capability>> loadCapabilities() throws Exception {
    Builder builder = getBuilder();
    if (builder == null)
        return Collections.emptyMap();
    Jar jar = builder.getJar();
    if (jar == null)
        return Collections.emptyMap();
    Manifest manifest = jar.getManifest();
    if (manifest == null)
        return Collections.emptyMap();
    Attributes attribs = manifest.getMainAttributes();
    Map<String, List<Capability>> capMap = new HashMap<String, List<Capability>>();
    // Load export packages
    String exportsPkgStr = attribs.getValue(Constants.EXPORT_PACKAGE);
    Parameters exportsMap = new Parameters(exportsPkgStr);
    for (Entry<String, Attrs> entry : exportsMap.entrySet()) {
        String pkg = Processor.removeDuplicateMarker(entry.getKey());
        org.osgi.framework.Version version = org.osgi.framework.Version.parseVersion(entry.getValue().getVersion());
        CapReqBuilder cb = new CapReqBuilder(PackageNamespace.PACKAGE_NAMESPACE).addAttribute(PackageNamespace.PACKAGE_NAMESPACE, pkg).addAttribute(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE, version);
        // TODO attributes and directives
        addCapability(capMap, cb.buildSyntheticCapability());
    }
    // Load identity/bundle/host
    String bsn = BundleUtils.getBundleSymbolicName(attribs);
    if (bsn != null) {
        // Ignore if not a bundle
        org.osgi.framework.Version version = org.osgi.framework.Version.parseVersion(attribs.getValue(Constants.BUNDLE_VERSION));
        // TODO attributes and directives
        addCapability(capMap, new CapReqBuilder(IdentityNamespace.IDENTITY_NAMESPACE).addAttribute(IdentityNamespace.IDENTITY_NAMESPACE, bsn).addAttribute(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE, version).buildSyntheticCapability());
        addCapability(capMap, new CapReqBuilder(BundleNamespace.BUNDLE_NAMESPACE).addAttribute(BundleNamespace.BUNDLE_NAMESPACE, bsn).addAttribute(BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE, version).buildSyntheticCapability());
        addCapability(capMap, new CapReqBuilder(HostNamespace.HOST_NAMESPACE).addAttribute(HostNamespace.HOST_NAMESPACE, bsn).addAttribute(HostNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE, version).buildSyntheticCapability());
    }
    // Generic capabilities
    String providesStr = attribs.getValue(Constants.PROVIDE_CAPABILITY);
    Parameters provides = new Parameters(providesStr);
    for (Entry<String, Attrs> entry : provides.entrySet()) {
        String ns = Processor.removeDuplicateMarker(entry.getKey());
        Attrs attrs = entry.getValue();
        CapReqBuilder cb = new CapReqBuilder(ns);
        for (String key : attrs.keySet()) {
            if (key.endsWith(":"))
                cb.addDirective(key.substring(0, key.length() - 1), attrs.get(key));
            else
                cb.addAttribute(key, attrs.getTyped(key));
        }
        addCapability(capMap, cb.buildSyntheticCapability());
    }
    return capMap;
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Parameters(aQute.bnd.header.Parameters) Capability(org.osgi.resource.Capability) HashMap(java.util.HashMap) CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Builder(aQute.bnd.osgi.Builder) Attributes(java.util.jar.Attributes) Attrs(aQute.bnd.header.Attrs) Manifest(java.util.jar.Manifest) Jar(aQute.bnd.osgi.Jar) LinkedList(java.util.LinkedList) List(java.util.List)

Example 62 with Capability

use of org.osgi.resource.Capability in project bnd by bndtools.

the class ResolveCommand method _query.

public void _query(QueryOptions options) throws Exception {
    List<String> args = options._arguments();
    String bsn = args.remove(0);
    String version = null;
    if (!args.isEmpty())
        version = args.remove(0);
    ProjectResolver pr = new ProjectResolver(bnd.getProject(options.project()));
    addClose(pr);
    IdentityCapability resource = pr.getResource(bsn, version);
    bnd.out.printf("%-30s %-20s %s\n", resource.osgi_identity(), resource.version(), resource.description(""));
    Resource r = resource.getResource();
    FilterParser p = new FilterParser();
    if (r != null) {
        List<Requirement> requirements = resource.getResource().getRequirements(null);
        if (!requirements.isEmpty()) {
            bnd.out.println("Requirements:");
            for (Requirement req : requirements) {
                Expression parse = p.parse(req);
                bnd.out.printf("  %-20s %s\n", req.getNamespace(), parse);
            }
        }
        List<Capability> capabilities = resource.getResource().getCapabilities(null);
        if (!capabilities.isEmpty()) {
            bnd.out.println("Capabilities:");
            for (Capability cap : capabilities) {
                Map<String, Object> attrs = new HashMap<String, Object>(cap.getAttributes());
                Object id = attrs.remove(cap.getNamespace());
                Object vv = attrs.remove("version");
                if (vv == null)
                    vv = attrs.remove("bundle-version");
                bnd.out.printf("  %-20s %-40s %-20s attrs=%s dirs=%s\n", cap.getNamespace(), id, vv, attrs, cap.getDirectives());
            }
        }
    }
}
Also used : Capability(org.osgi.resource.Capability) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) HashMap(java.util.HashMap) ProjectResolver(biz.aQute.resolve.ProjectResolver) Resource(org.osgi.resource.Resource) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Requirement(org.osgi.resource.Requirement) Expression(aQute.bnd.osgi.resource.FilterParser.Expression) FilterParser(aQute.bnd.osgi.resource.FilterParser)

Example 63 with Capability

use of org.osgi.resource.Capability in project bnd by bndtools.

the class Project method getBundleByHash.

private Container getBundleByHash(String bsn, Map<String, String> attrs) throws Exception {
    String hashStr = attrs.get("hash");
    String algo = SHA_256;
    String hash = hashStr;
    int colonIndex = hashStr.indexOf(':');
    if (colonIndex > -1) {
        algo = hashStr.substring(0, colonIndex);
        int afterColon = colonIndex + 1;
        hash = (colonIndex < hashStr.length()) ? hashStr.substring(afterColon) : "";
    }
    for (RepositoryPlugin plugin : workspace.getRepositories()) {
        // The plugin *may* understand version=hash directly
        DownloadBlocker blocker = new DownloadBlocker(this);
        File result = plugin.get(bsn, Version.LOWEST, Collections.unmodifiableMap(attrs), blocker);
        // Service, use a capability search on the osgi.content namespace.
        if (result == null && plugin instanceof Repository) {
            Repository repo = (Repository) plugin;
            if (!SHA_256.equals(algo))
                // R5 repos only support SHA-256
                continue;
            Requirement contentReq = new CapReqBuilder(ContentNamespace.CONTENT_NAMESPACE).filter(String.format("(%s=%s)", ContentNamespace.CONTENT_NAMESPACE, hash)).buildSyntheticRequirement();
            Set<Requirement> reqs = Collections.singleton(contentReq);
            Map<Requirement, Collection<Capability>> providers = repo.findProviders(reqs);
            Collection<Capability> caps = providers != null ? providers.get(contentReq) : null;
            if (caps != null && !caps.isEmpty()) {
                Capability cap = caps.iterator().next();
                IdentityCapability idCap = ResourceUtils.getIdentityCapability(cap.getResource());
                Map<String, Object> idAttrs = idCap.getAttributes();
                String id = (String) idAttrs.get(IdentityNamespace.IDENTITY_NAMESPACE);
                Object version = idAttrs.get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
                Version bndVersion = version != null ? Version.parseVersion(version.toString()) : Version.LOWEST;
                if (!bsn.equals(id)) {
                    String error = String.format("Resource with requested hash does not match ID '%s' [hash: %s]", bsn, hashStr);
                    return new Container(this, bsn, "hash", Container.TYPE.ERROR, null, error, null, null);
                }
                result = plugin.get(id, bndVersion, null, blocker);
            }
        }
        if (result != null)
            return toContainer(bsn, "hash", attrs, result, blocker);
    }
    // If we reach this far, none of the repos found the resource.
    return new Container(this, bsn, "hash", Container.TYPE.ERROR, null, "Could not find resource by content hash " + hashStr, null, null);
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Capability(org.osgi.resource.Capability) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Requirement(org.osgi.resource.Requirement) Repository(org.osgi.service.repository.Repository) Version(aQute.bnd.version.Version) Collection(java.util.Collection) File(java.io.File)

Example 64 with Capability

use of org.osgi.resource.Capability in project bnd by bndtools.

the class ResourceImpl method toString.

@Override
public String toString() {
    final StringBuilder builder = new StringBuilder();
    List<Capability> identities = getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE);
    if (identities.size() == 1) {
        Capability idCap = identities.get(0);
        Object id = idCap.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE);
        Object version = idCap.getAttributes().get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
        builder.append(id).append(" version=").append(version);
    } else {
        // Generic toString
        builder.append("ResourceImpl [caps=");
        builder.append(allCapabilities);
        builder.append(", reqs=");
        builder.append(allRequirements);
        builder.append("]");
    }
    return builder.toString();
}
Also used : IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Capability(org.osgi.resource.Capability) ContentCapability(aQute.bnd.osgi.resource.ResourceUtils.ContentCapability)

Example 65 with Capability

use of org.osgi.resource.Capability in project bnd by bndtools.

the class ResourceImpl method setCapabilities.

void setCapabilities(List<Capability> capabilities) {
    Map<String, List<Capability>> prepare = new HashMap<>();
    for (Capability capability : capabilities) {
        List<Capability> list = prepare.get(capability.getNamespace());
        if (list == null) {
            list = new LinkedList<Capability>();
            prepare.put(capability.getNamespace(), list);
        }
        list.add(capability);
    }
    for (Map.Entry<String, List<Capability>> entry : prepare.entrySet()) {
        entry.setValue(unmodifiableList(new ArrayList<>(entry.getValue())));
    }
    allCapabilities = unmodifiableList(new ArrayList<>(capabilities));
    capabilityMap = prepare;
}
Also used : IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Capability(org.osgi.resource.Capability) ContentCapability(aQute.bnd.osgi.resource.ResourceUtils.ContentCapability) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Collections.unmodifiableList(java.util.Collections.unmodifiableList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) Map(java.util.Map)

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