Search in sources :

Example 86 with Capability

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

the class BndrunResolveContextTest method testPreferLeastRequirementsAndMostCapabilities.

public static void testPreferLeastRequirementsAndMostCapabilities() {
    MockRegistry registry = new MockRegistry();
    registry.addPlugin(createRepo(IO.getFile("testdata/repo4/index.xml")));
    BndEditModel runModel = new BndEditModel();
    runModel.setRunFw("org.apache.felix.framework");
    Requirement requirement = new CapReqBuilder("x").buildSyntheticRequirement();
    BndrunResolveContext context = new BndrunResolveContext(runModel, registry, log);
    List<Capability> providers = context.findProviders(requirement);
    assertEquals(3, providers.size());
    // x.3 has same requirements but more capabilities than x.2
    assertEquals(IO.getFile("testdata/repo4/x.3.jar").toURI(), findContentURI(providers.get(0).getResource()));
    // x.2 has same capabilities but fewer requirements than x.1
    assertEquals(IO.getFile("testdata/repo4/x.2.jar").toURI(), findContentURI(providers.get(1).getResource()));
    assertEquals(IO.getFile("testdata/repo4/x.1.jar").toURI(), findContentURI(providers.get(2).getResource()));
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) MockRegistry(test.lib.MockRegistry) BndEditModel(aQute.bnd.build.model.BndEditModel)

Example 87 with Capability

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

the class BndrunResolveContextTest method testSelfCapabilityPreferredOverRepository.

public static void testSelfCapabilityPreferredOverRepository() {
    MockRegistry registry = new MockRegistry();
    Repository repo = createRepo(IO.getFile("testdata/repo4.index.xml"));
    registry.addPlugin(repo);
    Requirement resourceReq = new CapReqBuilder("osgi.identity").addDirective("filter", "(osgi.identity=dummy-selfcap)").buildSyntheticRequirement();
    Resource resource = repo.findProviders(Collections.singleton(resourceReq)).get(resourceReq).iterator().next().getResource();
    Requirement packageReq = resource.getRequirements("osgi.wiring.package").get(0);
    BndEditModel runModel = new BndEditModel();
    runModel.setRunFw("org.apache.felix.framework");
    BndrunResolveContext context = new BndrunResolveContext(runModel, registry, log);
    List<Capability> providers = context.findProviders(packageReq);
    assertNotNull(providers);
    assertEquals(2, providers.size());
    assertEquals(IO.getFile("testdata/repo4/dummy.jar").toURI(), findContentURI(providers.get(0).getResource()));
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Requirement(org.osgi.resource.Requirement) Repository(org.osgi.service.repository.Repository) Capability(org.osgi.resource.Capability) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) MockRegistry(test.lib.MockRegistry) Resource(org.osgi.resource.Resource) BndEditModel(aQute.bnd.build.model.BndEditModel)

Example 88 with Capability

use of org.osgi.resource.Capability 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 89 with Capability

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

the class R5LabelFormatter method appendResourceLabel.

public static void appendResourceLabel(StyledString label, Resource resource) {
    Capability identity = ResourceUtils.getIdentityCapability(resource);
    String name = ResourceUtils.getIdentity(identity);
    if (name == null) {
        if (resource != null) {
            name = resource.toString();
        } else {
            name = "<unknown>";
        }
    }
    label.append(name, BoldStyler.INSTANCE_DEFAULT);
    Version version = ResourceUtils.getVersion(identity);
    if (version != null)
        label.append(" " + version, StyledString.COUNTER_STYLER);
}
Also used : Capability(org.osgi.resource.Capability) Version(org.osgi.framework.Version) StyledString(org.eclipse.jface.viewers.StyledString)

Example 90 with Capability

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

the class RequirementWithResourceLabelProvider method appendResourceLabel.

protected void appendResourceLabel(StyledString label, Resource resource) {
    Capability identity = ResourceUtils.getIdentityCapability(resource);
    String name = ResourceUtils.getIdentity(identity);
    if (name == null) {
        if (resource != null) {
            name = resource.toString();
        } else {
            name = "<unknown>";
        }
    }
    label.append(name);
    Version version = ResourceUtils.getVersion(identity);
    if (version != null)
        label.append(" " + version, StyledString.COUNTER_STYLER);
}
Also used : Capability(org.osgi.resource.Capability) Version(org.osgi.framework.Version) StyledString(org.eclipse.jface.viewers.StyledString)

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