Search in sources :

Example 1 with IdentityCapability

use of aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability 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 2 with IdentityCapability

use of aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability 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 3 with IdentityCapability

use of aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability in project bnd by bndtools.

the class ResourceImpl method compareTo.

@Override
public int compareTo(Resource o) {
    IdentityCapability me = ResourceUtils.getIdentityCapability(this);
    IdentityCapability them = ResourceUtils.getIdentityCapability(o);
    String myName = me.osgi_identity();
    String theirName = them.osgi_identity();
    if (myName == theirName)
        return 0;
    if (myName == null)
        return -1;
    if (theirName == null)
        return 1;
    int n = myName.compareTo(theirName);
    if (n != 0)
        return n;
    Version myVersion = me.version();
    Version theirVersion = them.version();
    if (myVersion == theirVersion)
        return 0;
    if (myVersion == null)
        return -1;
    if (theirVersion == null)
        return 1;
    return myVersion.compareTo(theirVersion);
}
Also used : Version(aQute.bnd.version.Version) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)

Example 4 with IdentityCapability

use of aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability in project bnd by bndtools.

the class BndrunResolveContextTest method testInputRequirementsAsMandatoryResource.

public static void testInputRequirementsAsMandatoryResource() {
    MockRegistry registry = new MockRegistry();
    registry.addPlugin(createRepo(IO.getFile("testdata/repo3.index.xml")));
    BndEditModel runModel = new BndEditModel();
    runModel.setRunFw("org.apache.felix.framework");
    Requirement req = new CapReqBuilder("osgi.identity").addDirective("filter", "(osgi.identity=org.apache.felix.gogo.command)").buildSyntheticRequirement();
    runModel.setRunRequires(Collections.singletonList(req));
    BndrunResolveContext context = new BndrunResolveContext(runModel, registry, log);
    context.init();
    assertEquals(IO.getFile("testdata/repo3/org.apache.felix.framework-4.0.2.jar").toURI(), findContentURI(context.getFramework()));
    Collection<Resource> mandRes = context.getMandatoryResources();
    assertEquals(1, mandRes.size());
    Resource resource = mandRes.iterator().next();
    assertNotNull(resource);
    IdentityCapability ic = ResourceUtils.getIdentityCapability(resource);
    assertNotNull(ic);
    assertEquals("<<INITIAL>>", ic.osgi_identity());
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Requirement(org.osgi.resource.Requirement) MockRegistry(test.lib.MockRegistry) Resource(org.osgi.resource.Resource) BndEditModel(aQute.bnd.build.model.BndEditModel) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)

Example 5 with IdentityCapability

use of aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability in project bndtools by bndtools.

the class ReposTemplateLoader method findTemplates.

@Override
public Promise<List<Template>> findTemplates(String templateType, final Reporter reporter) {
    String filterStr = String.format("(%s=%s)", NS_TEMPLATE, templateType);
    final Requirement requirement = new CapReqBuilder(NS_TEMPLATE).addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filterStr).buildSyntheticRequirement();
    // Try to get the repositories and BundleLocator from the workspace
    List<Repository> workspaceRepos;
    BundleLocator tmpLocator;
    try {
        if (workspace == null)
            workspace = Central.getWorkspace();
        workspaceRepos = workspace.getPlugins(Repository.class);
        tmpLocator = new RepoPluginsBundleLocator(workspace.getRepositories());
    } catch (Exception e) {
        workspaceRepos = Collections.emptyList();
        tmpLocator = new DirectDownloadBundleLocator();
    }
    final BundleLocator locator = tmpLocator;
    // Setup the repos
    List<Repository> repos = new ArrayList<>(workspaceRepos.size() + 1);
    repos.addAll(workspaceRepos);
    addPreferenceConfiguredRepos(repos, reporter);
    // Generate a Promise<List<Template>> for each repository and add to an accumulator
    Promise<List<Template>> accumulator = Promises.resolved((List<Template>) new LinkedList<Template>());
    for (final Repository repo : repos) {
        final Deferred<List<Template>> deferred = new Deferred<>();
        final Promise<List<Template>> current = deferred.getPromise();
        accumulator = accumulator.then(new Success<List<Template>, List<Template>>() {

            @Override
            public Promise<List<Template>> call(Promise<List<Template>> resolved) throws Exception {
                final List<Template> prefix = resolved.getValue();
                return current.map(new Function<List<Template>, List<Template>>() {

                    @Override
                    public List<Template> apply(List<Template> t) {
                        return CollectionUtils.append(prefix, t);
                    }
                });
            }
        });
        executor.submit(new Runnable() {

            @Override
            public void run() {
                List<Template> templates = new LinkedList<>();
                Map<Requirement, Collection<Capability>> providerMap = repo.findProviders(Collections.singleton(requirement));
                if (providerMap != null) {
                    Collection<Capability> candidates = providerMap.get(requirement);
                    if (candidates != null) {
                        for (Capability cap : candidates) {
                            IdentityCapability idcap = ResourceUtils.getIdentityCapability(cap.getResource());
                            Object id = idcap.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE);
                            Object ver = idcap.getAttributes().get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
                            try {
                                String engineName = (String) cap.getAttributes().get("engine");
                                if (engineName == null)
                                    engineName = "stringtemplate";
                                TemplateEngine engine = engines.get(engineName);
                                if (engine != null)
                                    templates.add(new CapabilityBasedTemplate(cap, locator, engine));
                                else
                                    reporter.error("Error loading template from resource '%s' version %s: no Template Engine available matching '%s'", id, ver, engineName);
                            } catch (Exception e) {
                                reporter.error("Error loading template from resource '%s' version %s: %s", id, ver, e.getMessage());
                            }
                        }
                    }
                }
                deferred.resolve(templates);
            }
        });
    }
    return accumulator;
}
Also used : Deferred(org.osgi.util.promise.Deferred) ArrayList(java.util.ArrayList) Success(org.osgi.util.promise.Success) Template(org.bndtools.templating.Template) TemplateEngine(org.bndtools.templating.TemplateEngine) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Capability(org.osgi.resource.Capability) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Requirement(org.osgi.resource.Requirement) Promise(org.osgi.util.promise.Promise) Repository(org.osgi.service.repository.Repository) Collection(java.util.Collection) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)11 Capability (org.osgi.resource.Capability)7 Requirement (org.osgi.resource.Requirement)7 Resource (org.osgi.resource.Resource)5 HashMap (java.util.HashMap)4 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)3 Version (aQute.bnd.version.Version)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 IOException (java.io.IOException)2 Collection (java.util.Collection)2 Map (java.util.Map)2 Repository (org.osgi.service.repository.Repository)2 Promise (org.osgi.util.promise.Promise)2 Container (aQute.bnd.build.Container)1 BndEditModel (aQute.bnd.build.model.BndEditModel)1 ResourcesRepository (aQute.bnd.osgi.repository.ResourcesRepository)1 CapabilityBuilder (aQute.bnd.osgi.resource.CapabilityBuilder)1 FilterParser (aQute.bnd.osgi.resource.FilterParser)1