use of org.osgi.framework.startlevel.BundleStartLevel in project sling by apache.
the class BundlesInstaller method installBundle.
public void installBundle(BundleContext ctx, Artifact a, int startLevel) throws IOException, BundleException {
final String bundleUrl = "mvn:" + a.getGroupId() + "/" + a.getArtifactId() + "/" + a.getVersion() + "/" + a.getType();
final URL url = new URL(bundleUrl);
final InputStream bundleStream = url.openStream();
try {
final Bundle b = ctx.installBundle(bundleUrl, url.openStream());
if (startLevel > 0) {
final BundleStartLevel bsl = (BundleStartLevel) b.adapt(BundleStartLevel.class);
if (bsl == null) {
log.warn("Bundle does not adapt to BundleStartLevel, cannot set start level: {}", bundleUrl);
}
bsl.setStartLevel(startLevel);
}
log.info("bundle installed at start level {}: {}", startLevel, bundleUrl);
} finally {
bundleStream.close();
}
}
use of org.osgi.framework.startlevel.BundleStartLevel in project sling by apache.
the class BundleInstallTask method execute.
/**
* @see org.apache.sling.installer.api.tasks.InstallTask#execute(org.apache.sling.installer.api.tasks.InstallationContext)
*/
@Override
public void execute(final InstallationContext ctx) {
final int startLevel = this.getBundleStartLevel();
try {
final Bundle b = this.getBundleContext().installBundle(getResource().getURL(), getResource().getInputStream());
ctx.log("Installed bundle {} from resource {}", b, getResource());
// optionally set the start level
if (startLevel > 0) {
final BundleStartLevel startLevelService = b.adapt(BundleStartLevel.class);
startLevelService.setStartLevel(startLevel);
ctx.log("Set start level for bundle {} to {}", b, startLevel);
}
// fragment?
if (BundleUtil.isSystemBundleFragment(b)) {
// first install of a system fragment does not need a refresh of the host
// so we can just set the state and are done.
this.setFinishedState(ResourceState.INSTALLED);
} else {
final String fragmentHostHeader = BundleUtil.getFragmentHostHeader(b);
if (fragmentHostHeader != null) {
this.getLogger().debug("Need to do a refresh of the bundle's {} host", b);
for (final Bundle bundle : this.getBundleContext().getBundles()) {
if (fragmentHostHeader.equals(bundle.getSymbolicName())) {
this.getLogger().debug("Found host bundle for {} to refresh: {}", b, bundle);
RefreshBundlesTask.markBundleForRefresh(ctx, this.getTaskSupport(), bundle);
break;
}
}
this.setFinishedState(ResourceState.INSTALLED);
} else {
// mark this resource as to be started
BundleUtil.markBundleStart(getResource());
ctx.addTaskToCurrentCycle(new BundleStartTask(getResourceGroup(), b.getBundleId(), this.getTaskSupport()));
}
}
} catch (final Exception ex) {
// if something goes wrong we simply try it again
this.getLogger().info("Exception during install of bundle " + this.getResource() + " : " + ex.getMessage() + ". Retrying later.", ex);
}
}
Aggregations