use of org.osgi.framework.wiring.BundleRevision in project karaf by apache.
the class Deployer method getBundlesToStart.
protected List<Bundle> getBundlesToStart(Collection<Bundle> bundles, Bundle serviceBundle) {
// Restart the features service last, regardless of any other consideration
// so that we don't end up with the service trying to do stuff before we're done
boolean restart = false;
SortedMap<Integer, Set<Bundle>> bundlesPerStartLevel = new TreeMap<>();
for (Bundle bundle : bundles) {
if (bundle == serviceBundle) {
restart = true;
} else {
int sl = bundle.adapt(BundleStartLevel.class).getStartLevel();
addToMapSet(bundlesPerStartLevel, sl, bundle);
}
}
if (bundlesPerStartLevel.isEmpty()) {
bundles = Collections.emptyList();
} else {
bundles = bundlesPerStartLevel.remove(bundlesPerStartLevel.firstKey());
}
// We hit FELIX-2949 if we don't use the correct order as Felix resolver isn't greedy.
// In order to minimize that, we make sure we resolve the bundles in the order they
// are given back by the resolution, meaning that all root bundles (i.e. those that were
// not flagged as dependencies in features) are started before the others. This should
// make sure those important bundles are started first and minimize the problem.
List<BundleRevision> revs = new ArrayList<>();
for (Bundle bundle : bundles) {
revs.add(bundle.adapt(BundleRevision.class));
}
List<Bundle> sorted = new ArrayList<>();
for (BundleRevision rev : RequirementSort.sort(revs)) {
sorted.add(rev.getBundle());
}
if (sorted.isEmpty() && restart) {
sorted.add(serviceBundle);
}
return sorted;
}
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 aries by apache.
the class BundleWiringStateMBeanTest method testGetRevisionsDeclaredCapabilities.
@Test
public void testGetRevisionsDeclaredCapabilities() throws Exception {
BundleRevisions revisions = (BundleRevisions) bundleA.adapt(BundleRevisions.class);
Assert.assertEquals("Precondition", 1, revisions.getRevisions().size());
TabularData jmxCapabilitiesTable = brsMBean.getRevisionsDeclaredCapabilities(bundleA.getBundleId(), BundleRevision.PACKAGE_NAMESPACE);
Assert.assertEquals(1, jmxCapabilitiesTable.size());
List<BundleCapability> capabilities = ((BundleRevision) revisions.getRevisions().iterator().next()).getDeclaredCapabilities(BundleRevision.PACKAGE_NAMESPACE);
CompositeData jmxRevCapabilities = (CompositeData) jmxCapabilitiesTable.values().iterator().next();
CompositeData[] jmxCapabilities = (CompositeData[]) jmxRevCapabilities.get(BundleWiringStateMBean.CAPABILITIES);
Map<Map<String, Object>, Map<String, String>> expectedCapabilities = capabilitiesToMap(capabilities);
Map<Map<String, Object>, Map<String, String>> actualCapabilities = jmxCapReqToMap(jmxCapabilities);
Assert.assertEquals(expectedCapabilities, actualCapabilities);
}
use of org.osgi.framework.wiring.BundleRevision in project aries by apache.
the class BundleWiringStateMBeanTest method testGetCurrentRevisionDeclaredCapabilities.
@Test
public void testGetCurrentRevisionDeclaredCapabilities() throws Exception {
BundleRevision br = (BundleRevision) bundleA.adapt(BundleRevision.class);
List<BundleCapability> capabilities = br.getDeclaredCapabilities(BundleRevision.PACKAGE_NAMESPACE);
CompositeData[] jmxCapabilities = brsMBean.getCurrentRevisionDeclaredCapabilities(bundleA.getBundleId(), BundleRevision.PACKAGE_NAMESPACE);
Assert.assertEquals(capabilities.size(), jmxCapabilities.length);
Map<Map<String, Object>, Map<String, String>> expectedCapabilities = capabilitiesToMap(capabilities);
Map<Map<String, Object>, Map<String, String>> actualCapabilities = jmxCapReqToMap(jmxCapabilities);
Assert.assertEquals(expectedCapabilities, actualCapabilities);
}
Aggregations