Search in sources :

Example 56 with Requirement

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

the class ResourceCapReqLoader method loadRequirements.

@Override
public Map<String, List<RequirementWrapper>> loadRequirements() throws Exception {
    Map<String, List<RequirementWrapper>> result = new HashMap<String, List<RequirementWrapper>>();
    List<Requirement> reqs = resource.getRequirements(null);
    for (Requirement req : reqs) {
        String ns = req.getNamespace();
        List<RequirementWrapper> listForNamespace = result.get(ns);
        if (listForNamespace == null) {
            listForNamespace = new LinkedList<RequirementWrapper>();
            result.put(ns, listForNamespace);
        }
        RequirementWrapper wrapper = new RequirementWrapper();
        wrapper.requirement = req;
        listForNamespace.add(wrapper);
    }
    return result;
}
Also used : Requirement(org.osgi.resource.Requirement) HashMap(java.util.HashMap) List(java.util.List) LinkedList(java.util.LinkedList) RequirementWrapper(bndtools.model.resolution.RequirementWrapper)

Example 57 with Requirement

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

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

the class ResolveCommand method _validate.

public void _validate(ValidateOptions options) throws Exception {
    ResourceBuilder system = new ResourceBuilder();
    system.addEE(options.ee(EE.JavaSE_1_8));
    if (options.core() != null)
        system.addManifest(options.core().getManifest());
    if (options.packages() != null)
        system.addExportPackages(options.packages());
    if (options.capabilities() != null)
        system.addProvideCapabilities(options.capabilities());
    if (options.system() != null) {
        File f = IO.getFile(options.system());
        if (!f.isFile()) {
            error("Specified system file but not found: %s", f);
            return;
        }
        Domain domain = Domain.domain(f);
        system.addManifest(domain);
    }
    List<String> args = options._arguments();
    File index = getFile(args.remove(0));
    logger.debug("validating {}", index);
    ResolverValidator validator = new ResolverValidator(bnd);
    validator.use(bnd);
    validator.addRepository(index.toURI());
    validator.setSystem(system.build());
    List<Resolution> result = validator.validate();
    Set<Requirement> done = new HashSet<>();
    for (Resolution res : result) {
        if (options.all()) {
            bnd.out.format("%s %-60s%n", res.succeeded ? "OK" : "**", res.resource, res.message == null ? "" : res.message);
        }
        if (!res.succeeded) {
            for (Requirement req : res.missing) {
                if (done.contains(req))
                    continue;
                bnd.out.format("    missing   %s%n", req);
                done.add(req);
            }
            if (options.all()) {
                for (Requirement req : res.repos) {
                    bnd.out.format("    repos     %s%n", req);
                }
                for (Requirement req : res.system) {
                    bnd.out.format("    system    %s%n", req);
                }
                for (Requirement req : res.optionals) {
                    bnd.out.format("    optional  %s%n", req);
                }
            }
        }
    }
    bnd.getInfo(validator);
}
Also used : Requirement(org.osgi.resource.Requirement) ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) ResolverValidator(biz.aQute.resolve.ResolverValidator) Domain(aQute.bnd.osgi.Domain) File(java.io.File) Resolution(biz.aQute.resolve.ResolverValidator.Resolution) HashSet(java.util.HashSet)

Example 59 with Requirement

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

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

the class ResourceImpl method setRequirements.

void setRequirements(List<Requirement> requirements) {
    Map<String, List<Requirement>> prepare = new HashMap<>();
    for (Requirement requirement : requirements) {
        List<Requirement> list = prepare.get(requirement.getNamespace());
        if (list == null) {
            list = new LinkedList<Requirement>();
            prepare.put(requirement.getNamespace(), list);
        }
        list.add(requirement);
    }
    for (Map.Entry<String, List<Requirement>> entry : prepare.entrySet()) {
        entry.setValue(unmodifiableList(new ArrayList<>(entry.getValue())));
    }
    allRequirements = unmodifiableList(new ArrayList<>(requirements));
    requirementMap = prepare;
}
Also used : Requirement(org.osgi.resource.Requirement) 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

Requirement (org.osgi.resource.Requirement)201 Capability (org.osgi.resource.Capability)103 Resource (org.osgi.resource.Resource)59 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)53 ArrayList (java.util.ArrayList)51 Collection (java.util.Collection)44 BndEditModel (aQute.bnd.build.model.BndEditModel)35 HashMap (java.util.HashMap)35 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)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)16 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