use of org.osgi.framework.wiring.BundleRevision in project karaf by apache.
the class SubsystemResolver method prepare.
public void prepare(Collection<Feature> allFeatures, Map<String, Set<String>> requirements, Map<String, Set<BundleRevision>> system) throws Exception {
// Build subsystems on the fly
for (Map.Entry<String, Set<String>> entry : requirements.entrySet()) {
String[] parts = entry.getKey().split("/");
if (root == null) {
root = new Subsystem(parts[0]);
} else if (!root.getName().equals(parts[0])) {
throw new IllegalArgumentException("Can not use multiple roots: " + root.getName() + ", " + parts[0]);
}
Subsystem ss = root;
for (int i = 1; i < parts.length; i++) {
ss = getOrCreateChild(ss, parts[i]);
}
for (String requirement : entry.getValue()) {
ss.require(requirement);
}
}
if (root == null) {
return;
}
// Pre-resolve
root.build(allFeatures);
// Add system resources
BundleRevision sysBundleRev = null;
boolean hasEeCap = false;
for (Map.Entry<String, Set<BundleRevision>> entry : system.entrySet()) {
Subsystem ss = null;
String[] parts = entry.getKey().split("/");
String path = parts[0];
if (path.equals(root.getName())) {
ss = root;
}
for (int i = 1; ss != null && i < parts.length; i++) {
path += "/" + parts[i];
ss = ss.getChild(path);
}
if (ss != null) {
ResourceImpl dummy = new ResourceImpl("dummy", "dummy", Version.emptyVersion);
for (BundleRevision res : entry.getValue()) {
// We need to explicitely provide service capabilities for bundles
// We use both actual services and services declared from the headers
// TODO: use actual services
Map<String, String> headers = new DictionaryAsMap<>(res.getBundle().getHeaders());
Resource tmp = ResourceBuilder.build(res.getBundle().getLocation(), headers);
for (Capability cap : tmp.getCapabilities(ServiceNamespace.SERVICE_NAMESPACE)) {
dummy.addCapability(new CapabilityImpl(dummy, cap.getNamespace(), cap.getDirectives(), cap.getAttributes()));
}
ss.addSystemResource(res);
for (Capability cap : res.getCapabilities(null)) {
hasEeCap |= cap.getNamespace().equals(EXECUTION_ENVIRONMENT_NAMESPACE);
}
if (res.getBundle().getBundleId() == 0) {
sysBundleRev = res;
}
}
ss.addSystemResource(dummy);
}
}
// Under Equinox, the osgi.ee capabilities are not provided by the system bundle
if (!hasEeCap && sysBundleRev != null) {
String provideCaps = sysBundleRev.getBundle().getHeaders().get(PROVIDE_CAPABILITY);
environmentResource = new ResourceImpl("environment", "karaf.environment", Version.emptyVersion);
environmentResource.addCapabilities(ResourceBuilder.parseCapability(environmentResource, provideCaps));
root.addSystemResource(environmentResource);
}
}
use of org.osgi.framework.wiring.BundleRevision in project karaf by apache.
the class AssemblyDeployCallback method installBundle.
@Override
public Bundle installBundle(String region, String uri, InputStream is) throws BundleException {
// Check blacklist
if (Blacklist.isBundleBlacklisted(builder.getBlacklistedBundles(), uri)) {
if (builder.getBlacklistPolicy() == Builder.BlacklistPolicy.Fail) {
throw new RuntimeException("Bundle " + uri + " is blacklisted");
}
}
// Install
LOGGER.info(" adding maven artifact: " + uri);
try {
String regUri;
String path;
if (uri.startsWith("mvn:")) {
regUri = uri;
path = Parser.pathFromMaven(uri);
} else {
uri = uri.replaceAll("[^0-9a-zA-Z.\\-_]+", "_");
if (uri.length() > 256) {
//to avoid the File name too long exception
uri = uri.substring(0, 255);
}
path = "generated/" + uri;
regUri = "file:" + path;
}
final Path bundleSystemFile = systemDirectory.resolve(path);
Files.createDirectories(bundleSystemFile.getParent());
Files.copy(is, bundleSystemFile, StandardCopyOption.REPLACE_EXISTING);
Hashtable<String, String> headers = new Hashtable<>();
try (JarFile jar = new JarFile(bundleSystemFile.toFile())) {
Attributes attributes = jar.getManifest().getMainAttributes();
for (Map.Entry<Object, Object> attr : attributes.entrySet()) {
headers.put(attr.getKey().toString(), attr.getValue().toString());
}
}
BundleRevision revision = new FakeBundleRevision(headers, uri, nextBundleId.incrementAndGet());
Bundle bundle = revision.getBundle();
MapUtils.addToMapSet(dstate.bundlesPerRegion, region, bundle.getBundleId());
dstate.bundles.put(bundle.getBundleId(), bundle);
bundles.put(regUri, bundle);
return bundle;
} catch (IOException e) {
throw new BundleException("Unable to install bundle", e);
}
}
use of org.osgi.framework.wiring.BundleRevision in project karaf by apache.
the class BundleServiceImpl method getUnsatisfiedRequirements.
@Override
public List<BundleRequirement> getUnsatisfiedRequirements(Bundle bundle, String namespace) {
List<BundleRequirement> result = new ArrayList<>();
BundleRevision rev = bundle.adapt(BundleRevision.class);
if (rev != null) {
List<BundleRequirement> reqs = rev.getDeclaredRequirements(namespace);
for (BundleRequirement req : reqs) {
if (!canBeSatisfied(req)) {
result.add(req);
}
}
}
return result;
}
use of org.osgi.framework.wiring.BundleRevision in project karaf by apache.
the class BundleInfoImpl method populateRevisions.
private String populateRevisions(Bundle bundle) {
String ret = "";
BundleRevisions revisions = bundle.adapt(BundleRevisions.class);
if (revisions == null) {
return ret;
}
for (BundleRevision revision : revisions.getRevisions()) {
ret = ret + "[" + revision + "]" + " ";
}
return ret;
}
use of org.osgi.framework.wiring.BundleRevision in project karaf by apache.
the class PackageServiceImpl method getImports.
@Override
public List<PackageRequirement> getImports() {
Bundle[] bundles = bundleContext.getBundles();
SortedMap<String, PackageRequirement> requirements = new TreeMap<>();
for (Bundle bundle : bundles) {
BundleRevision rev = bundle.adapt(BundleRevision.class);
if (rev != null) {
List<BundleRequirement> reqs = rev.getDeclaredRequirements(BundleRevision.PACKAGE_NAMESPACE);
for (BundleRequirement req : reqs) {
PackageRequirement preq = create(req, bundle);
requirements.put(preq.getPackageName() + "|" + preq.getFilter() + "|" + preq.getBundle().getBundleId(), preq);
}
}
}
return new ArrayList<>(requirements.values());
}
Aggregations