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()));
}
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()));
}
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;
}
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);
}
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);
}
Aggregations