use of org.osgi.resource.Capability in project bndtools by bndtools.
the class BndBuilderCapReqLoader method loadCapabilities.
@Override
public Map<String, List<Capability>> loadCapabilities() throws Exception {
Builder builder = getBuilder();
if (builder == null)
return Collections.emptyMap();
Jar jar = builder.getJar();
if (jar == null)
return Collections.emptyMap();
Manifest manifest = jar.getManifest();
if (manifest == null)
return Collections.emptyMap();
Attributes attribs = manifest.getMainAttributes();
Map<String, List<Capability>> capMap = new HashMap<String, List<Capability>>();
// Load export packages
String exportsPkgStr = attribs.getValue(Constants.EXPORT_PACKAGE);
Parameters exportsMap = new Parameters(exportsPkgStr);
for (Entry<String, Attrs> entry : exportsMap.entrySet()) {
String pkg = Processor.removeDuplicateMarker(entry.getKey());
org.osgi.framework.Version version = org.osgi.framework.Version.parseVersion(entry.getValue().getVersion());
CapReqBuilder cb = new CapReqBuilder(PackageNamespace.PACKAGE_NAMESPACE).addAttribute(PackageNamespace.PACKAGE_NAMESPACE, pkg).addAttribute(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE, version);
// TODO attributes and directives
addCapability(capMap, cb.buildSyntheticCapability());
}
// Load identity/bundle/host
String bsn = BundleUtils.getBundleSymbolicName(attribs);
if (bsn != null) {
// Ignore if not a bundle
org.osgi.framework.Version version = org.osgi.framework.Version.parseVersion(attribs.getValue(Constants.BUNDLE_VERSION));
// TODO attributes and directives
addCapability(capMap, new CapReqBuilder(IdentityNamespace.IDENTITY_NAMESPACE).addAttribute(IdentityNamespace.IDENTITY_NAMESPACE, bsn).addAttribute(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE, version).buildSyntheticCapability());
addCapability(capMap, new CapReqBuilder(BundleNamespace.BUNDLE_NAMESPACE).addAttribute(BundleNamespace.BUNDLE_NAMESPACE, bsn).addAttribute(BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE, version).buildSyntheticCapability());
addCapability(capMap, new CapReqBuilder(HostNamespace.HOST_NAMESPACE).addAttribute(HostNamespace.HOST_NAMESPACE, bsn).addAttribute(HostNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE, version).buildSyntheticCapability());
}
// Generic capabilities
String providesStr = attribs.getValue(Constants.PROVIDE_CAPABILITY);
Parameters provides = new Parameters(providesStr);
for (Entry<String, Attrs> entry : provides.entrySet()) {
String ns = Processor.removeDuplicateMarker(entry.getKey());
Attrs attrs = entry.getValue();
CapReqBuilder cb = new CapReqBuilder(ns);
for (String key : attrs.keySet()) {
if (key.endsWith(":"))
cb.addDirective(key.substring(0, key.length() - 1), attrs.get(key));
else
cb.addAttribute(key, attrs.getTyped(key));
}
addCapability(capMap, cb.buildSyntheticCapability());
}
return capMap;
}
use of org.osgi.resource.Capability 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());
}
}
}
}
use of org.osgi.resource.Capability 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);
}
use of org.osgi.resource.Capability in project bnd by bndtools.
the class ResourceImpl method toString.
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
List<Capability> identities = getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE);
if (identities.size() == 1) {
Capability idCap = identities.get(0);
Object id = idCap.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE);
Object version = idCap.getAttributes().get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
builder.append(id).append(" version=").append(version);
} else {
// Generic toString
builder.append("ResourceImpl [caps=");
builder.append(allCapabilities);
builder.append(", reqs=");
builder.append(allRequirements);
builder.append("]");
}
return builder.toString();
}
use of org.osgi.resource.Capability in project bnd by bndtools.
the class ResourceImpl method setCapabilities.
void setCapabilities(List<Capability> capabilities) {
Map<String, List<Capability>> prepare = new HashMap<>();
for (Capability capability : capabilities) {
List<Capability> list = prepare.get(capability.getNamespace());
if (list == null) {
list = new LinkedList<Capability>();
prepare.put(capability.getNamespace(), list);
}
list.add(capability);
}
for (Map.Entry<String, List<Capability>> entry : prepare.entrySet()) {
entry.setValue(unmodifiableList(new ArrayList<>(entry.getValue())));
}
allCapabilities = unmodifiableList(new ArrayList<>(capabilities));
capabilityMap = prepare;
}
Aggregations