use of org.osgi.resource.Capability in project bndtools by bndtools.
the class ResolutionWizard method resourceToRunBundle.
private static VersionedClause resourceToRunBundle(Resource resource) {
Capability idCap = ResourceUtils.getIdentityCapability(resource);
String identity = ResourceUtils.getIdentity(idCap);
// Map version range string, using "latest" for any workspace resources
Attrs attribs = new Attrs();
String versionRangeStr;
if (isWorkspace(resource)) {
versionRangeStr = VERSION_SNAPSHOT;
} else {
Version version = ResourceUtils.getVersion(idCap);
VersionRange versionRange = createVersionRange(version);
versionRangeStr = versionRange.toString();
}
attribs.put(Constants.VERSION_ATTRIBUTE, versionRangeStr);
return new VersionedClause(identity, attribs);
}
use of org.osgi.resource.Capability 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;
}
use of org.osgi.resource.Capability in project bndtools by bndtools.
the class CapabilityBasedTemplate method fetchBundle.
private synchronized File fetchBundle() throws IOException {
if (_bundleFile != null && _bundleFile.exists())
return _bundleFile;
Capability idCap = capability.getResource().getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE).get(0);
String id = (String) idCap.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE);
Capability contentCap = capability.getResource().getCapabilities(ContentNamespace.CONTENT_NAMESPACE).get(0);
URI location;
Object locationObj = contentCap.getAttributes().get("url");
if (locationObj instanceof URI)
location = (URI) locationObj;
else if (locationObj instanceof String)
location = URI.create((String) locationObj);
else
throw new IOException("Template repository entry is missing url attribute");
if ("file".equals(location.getScheme())) {
_bundleFile = IO.getFile(location.getPath());
return _bundleFile;
}
// Try to locate from the workspace and/or repositories if a BundleLocator was provide
if (locator != null) {
String hashStr = (String) contentCap.getAttributes().get(ContentNamespace.CONTENT_NAMESPACE);
try {
_bundleFile = locator.locate(id, hashStr, "SHA-256", location);
if (_bundleFile != null)
return _bundleFile;
} catch (Exception e) {
throw new IOException("Unable to fetch bundle for template: " + getName(), e);
}
}
throw new IOException("Unable to fetch bundle for template: " + getName());
}
use of org.osgi.resource.Capability in project bndtools by bndtools.
the class ResourceUtils method getContentCapability.
@SuppressWarnings("unused")
public static Capability getContentCapability(Resource resource) throws IllegalArgumentException {
List<Capability> caps = resource.getCapabilities(ContentNamespace.CONTENT_NAMESPACE);
if (caps == null)
throw new IllegalArgumentException("Resource has no content");
//A resource may have multiple capabilities and this is acceptable according to the specification
//to us, we should pick
//the first one with a URL that we understand, or the first URL if we understand none of them
Capability firstCap = null;
for (Capability c : caps) {
if (firstCap == null)
firstCap = c;
Object url = c.getAttributes().get("url");
if (url == null)
continue;
String urlString = String.valueOf(url);
try {
new URL(urlString);
return c;
} catch (MalformedURLException mue) {
// Oh well, just try the next one
}
}
if (firstCap == null)
throw new IllegalArgumentException("Resource has no content");
return firstCap;
}
use of org.osgi.resource.Capability in project bndtools by bndtools.
the class ResourceUtils method getIdentityCapability.
public static Capability getIdentityCapability(Resource resource) throws IllegalArgumentException {
List<Capability> caps = resource.getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE);
if (caps == null || caps.isEmpty())
throw new IllegalArgumentException("Resource has no identity");
if (caps.size() > 1) {
// Remove the alias identity "system.bundle"
List<Capability> filtered = new ArrayList<>(caps.size());
List<String> ids = new ArrayList<>(caps.size());
for (Capability cap : caps) {
String id = getIdentity(cap);
if (!Constants.SYSTEM_BUNDLE_SYMBOLICNAME.equals(id)) {
filtered.add(cap);
ids.add(id);
}
}
caps = filtered;
if (caps.size() > 1)
throw new IllegalArgumentException("Resource has multiple identity capabilities: " + ids);
}
return caps.get(0);
}
Aggregations