Search in sources :

Example 36 with CapReqBuilder

use of aQute.bnd.osgi.resource.CapReqBuilder 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)

Example 37 with CapReqBuilder

use of aQute.bnd.osgi.resource.CapReqBuilder 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 38 with CapReqBuilder

use of aQute.bnd.osgi.resource.CapReqBuilder in project bnd by bndtools.

the class BndEditModelTest method testVariableInRunRequirements.

public static void testVariableInRunRequirements() throws Exception {
    Workspace ws = new Workspace(new File("testresources/ws"));
    BndEditModel model = new BndEditModel(ws);
    File f = new File("testresources/ws/p7/reuse.bndrun");
    model.setBndResource(f);
    model.setBndResourceName("reuse.bndrun");
    model.loadFrom(f);
    // VERIFY
    Processor processor = model.getProperties();
    String runrequirements = processor.mergeProperties(Constants.RUNREQUIRES);
    String[] rrr = runrequirements.split(",");
    assertEquals(4, rrr.length);
    assertEquals("osgi.identity;filter:='(osgi.identity=variable)'", rrr[0]);
    assertEquals("osgi.identity;filter:='(osgi.identity=variable2)'", rrr[1]);
    assertEquals("osgi.identity;filter:='(osgi.identity=b)'", rrr[2]);
    assertEquals("osgi.identity;filter:='(osgi.identity=c)'", rrr[3]);
    // [cs] don't know how to update this.
    List<Requirement> r = model.getRunRequires();
    assertEquals(3, r.size());
    assertEquals(new CapReqBuilder("${var}").buildSyntheticRequirement(), r.get(0));
    assertEquals(new CapReqBuilder(IdentityNamespace.IDENTITY_NAMESPACE).addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, "(osgi.identity=b)").buildSyntheticRequirement(), r.get(1));
    assertEquals(new CapReqBuilder(IdentityNamespace.IDENTITY_NAMESPACE).addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, "(osgi.identity=c)").buildSyntheticRequirement(), r.get(2));
    // Test Set with variables
    List<Requirement> rr = new LinkedList<Requirement>();
    rr.add(new CapReqBuilder(IdentityNamespace.IDENTITY_NAMESPACE).addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, "(osgi.identity=b)").buildSyntheticRequirement());
    rr.add(new CapReqBuilder("${var}").buildSyntheticRequirement());
    model.setRunRequires(rr);
    // VERIFY
    processor = model.getProperties();
    runrequirements = processor.mergeProperties(Constants.RUNREQUIRES);
    rrr = runrequirements.split(",");
    assertEquals(3, rrr.length);
    assertEquals("osgi.identity;filter:='(osgi.identity=b)'", rrr[0]);
    assertEquals("osgi.identity;filter:='(osgi.identity=variable)'", rrr[1]);
    assertEquals("osgi.identity;filter:='(osgi.identity=variable2)'", rrr[2]);
    // Test SET
    rr = new LinkedList<Requirement>();
    rr.add(getReq("(osgi.identity=b)"));
    rr.add(getReq("(osgi.identity=c)"));
    model.setRunRequires(rr);
    // VERIFY
    processor = model.getProperties();
    runrequirements = processor.mergeProperties(Constants.RUNREQUIRES);
    rrr = runrequirements.split(",");
    assertEquals(2, rrr.length);
    assertEquals("osgi.identity;filter:='(osgi.identity=b)'", rrr[0]);
    assertEquals("osgi.identity;filter:='(osgi.identity=c)'", rrr[1]);
    r = model.getRunRequires();
    assertEquals(getReq("(osgi.identity=b)"), r.get(0));
    assertEquals(getReq("(osgi.identity=c)"), r.get(1));
    // TEST Saving changes and those changes persist...
    Document d = new Document("");
    model.saveChangesTo(d);
    processor = model.getProperties();
    runrequirements = processor.mergeProperties(Constants.RUNREQUIRES);
    rrr = runrequirements.split(",");
    assertEquals(2, rrr.length);
    assertEquals("	osgi.identity;filter:='(osgi.identity=b)'", rrr[0]);
    assertEquals("	osgi.identity;filter:='(osgi.identity=c)'", rrr[1]);
    assertEquals(getReq("(osgi.identity=b)"), r.get(0));
    assertEquals(getReq("(osgi.identity=c)"), r.get(1));
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Requirement(org.osgi.resource.Requirement) Processor(aQute.bnd.osgi.Processor) Document(aQute.bnd.properties.Document) File(java.io.File) BndEditModel(aQute.bnd.build.model.BndEditModel) LinkedList(java.util.LinkedList) Workspace(aQute.bnd.build.Workspace)

Example 39 with CapReqBuilder

use of aQute.bnd.osgi.resource.CapReqBuilder in project bnd by bndtools.

the class OBRFragment method parse.

public static Reporter parse(File file, ResourceBuilder resource, String base) throws Exception {
    try (Jar jar = new Jar(file)) {
        Reporter reporter = parse(jar, resource);
        if (!reporter.isOk())
            return reporter;
        CapReqBuilder content = new CapReqBuilder(ContentNamespace.CONTENT_NAMESPACE);
        String sha = SHA1.digest(file).asHex();
        content.addAttribute(ContentNamespace.CONTENT_NAMESPACE, sha);
        content.addAttribute(ContentNamespace.CAPABILITY_SIZE_ATTRIBUTE, file.length());
        content.addAttribute(ContentNamespace.CAPABILITY_MIME_ATTRIBUTE, MIME_TYPE_OSGI_BUNDLE);
        if (base != null) {
            String path = file.getAbsolutePath();
            if (base.startsWith(path)) {
                content.addAttribute(ContentNamespace.CAPABILITY_URL_ATTRIBUTE, path.substring(base.length()).replace(File.separatorChar, '/'));
            } else {
                reporter.error("Base path %s is not parent of file path: %s", base, file.getAbsolutePath());
            }
        }
        resource.addCapability(content);
        return reporter;
    }
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Reporter(aQute.service.reporter.Reporter) Jar(aQute.bnd.osgi.Jar)

Example 40 with CapReqBuilder

use of aQute.bnd.osgi.resource.CapReqBuilder in project bnd by bndtools.

the class OBRFragment method parse.

@SuppressWarnings("deprecation")
public static Reporter parse(Jar jar, ResourceBuilder resource) throws Exception {
    Manifest m = jar.getManifest();
    if (m == null)
        return null;
    Domain d = Domain.domain(m);
    d.setTranslation(jar);
    Entry<String, Attrs> bundleSymbolicName = d.getBundleSymbolicName();
    if (bundleSymbolicName == null)
        return null;
    boolean singleton = "true".equals(bundleSymbolicName.getValue().get(Constants.SINGLETON_DIRECTIVE + ":"));
    boolean isFragment = d.get(Constants.FRAGMENT_HOST) != null;
    Version version = d.getBundleVersion() == null ? Version.emptyVersion : new Version(d.getBundleVersion());
    CapReqBuilder identity = new CapReqBuilder(IdentityNamespace.IDENTITY_NAMESPACE);
    identity.addAttribute(IdentityNamespace.IDENTITY_NAMESPACE, bundleSymbolicName.getKey());
    identity.addAttribute(IdentityNamespace.CAPABILITY_COPYRIGHT_ATTRIBUTE, d.translate(Constants.BUNDLE_COPYRIGHT));
    identity.addAttribute(IdentityNamespace.CAPABILITY_DESCRIPTION_ATTRIBUTE, d.translate(Constants.BUNDLE_DESCRIPTION));
    identity.addAttribute(IdentityNamespace.CAPABILITY_DOCUMENTATION_ATTRIBUTE, d.translate(Constants.BUNDLE_DOCURL));
    identity.addAttribute(IdentityNamespace.CAPABILITY_LICENSE_ATTRIBUTE, d.translate(aQute.bnd.osgi.Constants.BUNDLE_LICENSE));
    if (singleton)
        identity.addAttribute(IdentityNamespace.CAPABILITY_SINGLETON_DIRECTIVE, "true");
    identity.addAttribute(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE, isFragment ? IdentityNamespace.TYPE_FRAGMENT : IdentityNamespace.TYPE_BUNDLE);
    identity.addAttribute(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE, new Version(d.getBundleVersion()));
    resource.addCapability(identity);
    if (isFragment) {
        //
        // Fragment-Host
        //
        Entry<String, Attrs> fragmentHost = d.getFragmentHost();
        CapReqBuilder fragment = new CapReqBuilder(HostNamespace.HOST_NAMESPACE);
        String v = fragmentHost.getValue().get("version");
        if (v == null)
            v = "0";
        Version fragmentVersion = new Version(v);
        String filter = filter(PackageNamespace.PACKAGE_NAMESPACE, fragmentHost.getKey(), fragmentHost.getValue());
        fragment.addDirective(HostNamespace.REQUIREMENT_FILTER_DIRECTIVE, filter);
        resource.addRequirement(fragment);
    } else {
        //
        // Bundle-SymbolicName
        //
        CapReqBuilder bundle = new CapReqBuilder(BundleNamespace.BUNDLE_NAMESPACE);
        CapReqBuilder host = new CapReqBuilder(HostNamespace.HOST_NAMESPACE);
        bundle.addAttribute("version", version);
        host.addAttribute("version", version);
        for (Entry<String, String> e : bundleSymbolicName.getValue().entrySet()) {
            String key = e.getKey();
            if (key.endsWith(":")) {
                String directive = key.substring(0, key.length() - 1);
                if (Constants.FRAGMENT_ATTACHMENT_DIRECTIVE.equalsIgnoreCase(directive)) {
                    if (Constants.FRAGMENT_ATTACHMENT_NEVER.equalsIgnoreCase(e.getValue()))
                        host = null;
                } else if (!Constants.SINGLETON_DIRECTIVE.equalsIgnoreCase(directive)) {
                    bundle.addDirective(directive, e.getValue());
                }
                if (host != null)
                    host.addDirective(directive, e.getValue());
                bundle.addDirective(directive, e.getValue());
            } else {
                if (host != null)
                    host.addAttribute(key, e.getValue());
                bundle.addAttribute(key, e.getValue());
            }
        }
        if (host != null)
            resource.addCapability(host);
        resource.addCapability(bundle);
    }
    //
    // Export-Package
    //
    Parameters exports = d.getExportPackage();
    for (Entry<String, Attrs> entry : exports.entrySet()) {
        CapReqBuilder exported = new CapReqBuilder(PackageNamespace.PACKAGE_NAMESPACE);
        String pkgName = Processor.removeDuplicateMarker(entry.getKey());
        exported.addAttribute(PackageNamespace.PACKAGE_NAMESPACE, pkgName);
        String versionStr = entry.getValue().get(Constants.VERSION_ATTRIBUTE);
        Version v = Version.parseVersion(entry.getValue().get("version"));
        exported.addAttribute(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE, version);
        for (Entry<String, String> attribEntry : entry.getValue().entrySet()) {
            String key = attribEntry.getKey();
            if (key.endsWith(":")) {
                String directive = key.substring(0, key.length() - 1);
                exported.addDirective(directive, attribEntry.getValue());
            } else {
                if (key.equals("specification-version") || key.equals("version"))
                    exported.addAttribute("version", Version.parseVersion(attribEntry.getValue()));
                else
                    exported.addAttribute(key, attribEntry.getValue());
            }
        }
        exported.addAttribute(PackageNamespace.CAPABILITY_BUNDLE_SYMBOLICNAME_ATTRIBUTE, bundleSymbolicName.getKey());
        exported.addAttribute(PackageNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE, version);
        resource.addCapability(exported);
    }
    //
    // Import-Package
    //
    Parameters imports = d.getImportPackage();
    for (Entry<String, Attrs> entry : imports.entrySet()) {
        CapReqBuilder imported = new CapReqBuilder(PackageNamespace.PACKAGE_NAMESPACE);
        String name = Processor.removeDuplicateMarker(entry.getKey());
        String filter = filter(PackageNamespace.PACKAGE_NAMESPACE, Processor.removeDuplicateMarker(entry.getKey()), entry.getValue());
        imported.addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filter);
        resource.addRequirement(imported);
    }
    //
    // Require-Bundle
    //
    Parameters requires = d.getRequireBundle();
    for (Entry<String, Attrs> entry : requires.entrySet()) {
        CapReqBuilder req = new CapReqBuilder(BundleNamespace.BUNDLE_NAMESPACE);
        String bsn = Processor.removeDuplicateMarker(entry.getKey());
        String filter = filter(BundleNamespace.BUNDLE_NAMESPACE, bsn, entry.getValue());
        req.addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filter);
        resource.addRequirement(req);
    }
    //
    // Bundle-RequiredExecutionEnvironment
    //
    Parameters brees = d.getBundleRequiredExecutionEnvironment();
    try (Formatter formatter = new Formatter()) {
        formatter.format("(|");
        for (Entry<String, Attrs> bree : brees.entrySet()) {
            String name = Processor.removeDuplicateMarker(bree.getKey());
            Matcher matcher = EE_PATTERN.matcher(name);
            if (matcher.matches()) {
                name = matcher.group(1);
                Version v = Version.parseVersion(matcher.group(2));
                formatter.format("%s", filter(ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE, name, MAP.$("version", v.toString())));
            }
        }
        formatter.format(")");
        CapReqBuilder breeReq = new CapReqBuilder(ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE);
        breeReq.addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, formatter.toString());
        for (Entry<String, Attrs> export : d.getParameters(Constants.EXPORT_SERVICE).entrySet()) {
            CapReqBuilder exportedService = new CapReqBuilder(ServiceNamespace.SERVICE_NAMESPACE);
            String service = Processor.removeDuplicateMarker(export.getKey());
            exportedService.addAttribute(ServiceNamespace.SERVICE_NAMESPACE, service);
            exportedService.addAttribute(ServiceNamespace.CAPABILITY_OBJECTCLASS_ATTRIBUTE, export.getValue().get("objectclass"));
            resource.addCapability(exportedService);
        }
        for (Entry<String, Attrs> imported : d.getParameters(Constants.IMPORT_SERVICE).entrySet()) {
            CapReqBuilder importedService = new CapReqBuilder(ServiceNamespace.SERVICE_NAMESPACE);
            String service = Processor.removeDuplicateMarker(imported.getKey());
            importedService.addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filter(ServiceNamespace.SERVICE_NAMESPACE, service, imported.getValue()));
            resource.addRequirement(importedService);
        }
        for (Entry<String, Attrs> rc : d.getProvideCapability().entrySet()) {
            resource.addCapability(toCapability(rc.getKey(), rc.getValue()));
        }
        for (Entry<String, Attrs> rc : d.getRequireCapability().entrySet()) {
            resource.addCapability(toRequirement(rc.getKey(), rc.getValue()));
        }
    }
    return null;
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Parameters(aQute.bnd.header.Parameters) Version(org.osgi.framework.Version) Matcher(java.util.regex.Matcher) Formatter(java.util.Formatter) Attrs(aQute.bnd.header.Attrs) Manifest(java.util.jar.Manifest) Domain(aQute.bnd.osgi.Domain)

Aggregations

CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)69 Requirement (org.osgi.resource.Requirement)53 Capability (org.osgi.resource.Capability)35 BndEditModel (aQute.bnd.build.model.BndEditModel)32 MockRegistry (test.lib.MockRegistry)28 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)23 Resource (org.osgi.resource.Resource)18 ArrayList (java.util.ArrayList)11 Collection (java.util.Collection)10 Repository (org.osgi.service.repository.Repository)9 ResourceBuilder (aQute.bnd.osgi.resource.ResourceBuilder)8 HashMap (java.util.HashMap)8 File (java.io.File)7 List (java.util.List)7 AndFilter (aQute.libg.filters.AndFilter)6 LiteralFilter (aQute.libg.filters.LiteralFilter)6 SimpleFilter (aQute.libg.filters.SimpleFilter)6 Parameters (aQute.bnd.header.Parameters)5 InfoRepository (aQute.bnd.service.repository.InfoRepository)5 Version (aQute.bnd.version.Version)5