Search in sources :

Example 91 with Capability

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

the class ResolutionWizard method resourceToRunBundle.

private static VersionedClause resourceToRunBundle(Resource resource) {
    Capability idCap = ResourceUtils.getIdentityCapability(resource);
    String identity = ResourceUtils.getIdentity(idCap);
    // Map version range string, using "latest" for any workspace resources
    Attrs attribs = new Attrs();
    String versionRangeStr;
    if (isWorkspace(resource)) {
        versionRangeStr = VERSION_SNAPSHOT;
    } else {
        Version version = ResourceUtils.getVersion(idCap);
        VersionRange versionRange = createVersionRange(version);
        versionRangeStr = versionRange.toString();
    }
    attribs.put(Constants.VERSION_ATTRIBUTE, versionRangeStr);
    return new VersionedClause(identity, attribs);
}
Also used : Capability(org.osgi.resource.Capability) Version(org.osgi.framework.Version) VersionedClause(aQute.bnd.build.model.clauses.VersionedClause) Attrs(aQute.bnd.header.Attrs) VersionRange(aQute.bnd.version.VersionRange)

Example 92 with Capability

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

the class ResolutionSuccessPanel method resourceToRequirement.

private static Requirement resourceToRequirement(Resource resource) {
    Capability identity = ResourceUtils.getIdentityCapability(resource);
    String id = ResourceUtils.getIdentity(identity);
    Version version = ResourceUtils.getVersion(identity);
    Version dropQualifier = new Version(version.getMajor(), version.getMinor(), version.getMicro());
    AndFilter filter = new AndFilter();
    filter.addChild(new SimpleFilter(IdentityNamespace.IDENTITY_NAMESPACE, id));
    filter.addChild(new LiteralFilter(Filters.fromVersionRange(dropQualifier.toString())));
    Requirement req = new CapReqBuilder(IdentityNamespace.IDENTITY_NAMESPACE).addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filter.toString()).buildSyntheticRequirement();
    return req;
}
Also used : LiteralFilter(aQute.libg.filters.LiteralFilter) CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) AndFilter(aQute.libg.filters.AndFilter) Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) Version(org.osgi.framework.Version) SimpleFilter(aQute.libg.filters.SimpleFilter)

Example 93 with Capability

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

the class CapabilityBasedTemplate method fetchBundle.

private synchronized File fetchBundle() throws IOException {
    if (_bundleFile != null && _bundleFile.exists())
        return _bundleFile;
    Capability idCap = capability.getResource().getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE).get(0);
    String id = (String) idCap.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE);
    Capability contentCap = capability.getResource().getCapabilities(ContentNamespace.CONTENT_NAMESPACE).get(0);
    URI location;
    Object locationObj = contentCap.getAttributes().get("url");
    if (locationObj instanceof URI)
        location = (URI) locationObj;
    else if (locationObj instanceof String)
        location = URI.create((String) locationObj);
    else
        throw new IOException("Template repository entry is missing url attribute");
    if ("file".equals(location.getScheme())) {
        _bundleFile = IO.getFile(location.getPath());
        return _bundleFile;
    }
    // Try to locate from the workspace and/or repositories if a BundleLocator was provide
    if (locator != null) {
        String hashStr = (String) contentCap.getAttributes().get(ContentNamespace.CONTENT_NAMESPACE);
        try {
            _bundleFile = locator.locate(id, hashStr, "SHA-256", location);
            if (_bundleFile != null)
                return _bundleFile;
        } catch (Exception e) {
            throw new IOException("Unable to fetch bundle for template: " + getName(), e);
        }
    }
    throw new IOException("Unable to fetch bundle for template: " + getName());
}
Also used : Capability(org.osgi.resource.Capability) IOException(java.io.IOException) URI(java.net.URI) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException)

Example 94 with Capability

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

the class ResourceUtils method getContentCapability.

@SuppressWarnings("unused")
public static Capability getContentCapability(Resource resource) throws IllegalArgumentException {
    List<Capability> caps = resource.getCapabilities(ContentNamespace.CONTENT_NAMESPACE);
    if (caps == null)
        throw new IllegalArgumentException("Resource has no content");
    //A resource may have multiple capabilities and this is acceptable according to the specification
    //to us, we should pick
    //the first one with a URL that we understand, or the first URL if we understand none of them
    Capability firstCap = null;
    for (Capability c : caps) {
        if (firstCap == null)
            firstCap = c;
        Object url = c.getAttributes().get("url");
        if (url == null)
            continue;
        String urlString = String.valueOf(url);
        try {
            new URL(urlString);
            return c;
        } catch (MalformedURLException mue) {
        // Oh well, just try the next one
        }
    }
    if (firstCap == null)
        throw new IllegalArgumentException("Resource has no content");
    return firstCap;
}
Also used : MalformedURLException(java.net.MalformedURLException) Capability(org.osgi.resource.Capability) URL(java.net.URL)

Example 95 with Capability

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

the class ResourceUtils method getIdentityCapability.

public static Capability getIdentityCapability(Resource resource) throws IllegalArgumentException {
    List<Capability> caps = resource.getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE);
    if (caps == null || caps.isEmpty())
        throw new IllegalArgumentException("Resource has no identity");
    if (caps.size() > 1) {
        // Remove the alias identity "system.bundle"
        List<Capability> filtered = new ArrayList<>(caps.size());
        List<String> ids = new ArrayList<>(caps.size());
        for (Capability cap : caps) {
            String id = getIdentity(cap);
            if (!Constants.SYSTEM_BUNDLE_SYMBOLICNAME.equals(id)) {
                filtered.add(cap);
                ids.add(id);
            }
        }
        caps = filtered;
        if (caps.size() > 1)
            throw new IllegalArgumentException("Resource has multiple identity capabilities: " + ids);
    }
    return caps.get(0);
}
Also used : Capability(org.osgi.resource.Capability) ArrayList(java.util.ArrayList)

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