Search in sources :

Example 6 with BundleStartLevel

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();
    }
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) InputStream(java.io.InputStream) Bundle(org.osgi.framework.Bundle) URL(java.net.URL)

Example 7 with BundleStartLevel

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);
    }
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) Bundle(org.osgi.framework.Bundle)

Aggregations

BundleStartLevel (org.osgi.framework.startlevel.BundleStartLevel)7 Bundle (org.osgi.framework.Bundle)6 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 BundleException (org.osgi.framework.BundleException)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 Version (org.osgi.framework.Version)1 FrameworkStartLevel (org.osgi.framework.startlevel.FrameworkStartLevel)1 BundleRevision (org.osgi.framework.wiring.BundleRevision)1 Coordination (org.osgi.service.coordinator.Coordination)1 CoordinationException (org.osgi.service.coordinator.CoordinationException)1 Participant (org.osgi.service.coordinator.Participant)1 SubsystemException (org.osgi.service.subsystem.SubsystemException)1