Search in sources :

Example 1 with BundleStartLevel

use of org.osgi.framework.startlevel.BundleStartLevel in project karaf by apache.

the class StartLevel method doExecute.

protected Object doExecute(Bundle bundle) throws Exception {
    // Get package instance service.
    BundleStartLevel bsl = bundle.adapt(BundleStartLevel.class);
    if (level == null) {
        System.out.println("Level " + bsl.getStartLevel());
    } else {
        int sbsl = bundleService.getSystemBundleThreshold();
        if ((level < sbsl) && (bsl.getStartLevel() >= sbsl)) {
            if (!JaasHelper.currentUserHasRole(BundleService.SYSTEM_BUNDLES_ROLE)) {
                throw new IllegalArgumentException("Insufficient priviledges");
            }
        }
        bsl.setStartLevel(level);
    }
    return null;
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel)

Example 2 with BundleStartLevel

use of org.osgi.framework.startlevel.BundleStartLevel in project karaf by apache.

the class TestBase method createDummyBundle.

public Bundle createDummyBundle(long id, final String symbolicName, Dictionary<String, String> headers) {
    Bundle bundle = EasyMock.createNiceMock(Bundle.class);
    // Be aware that this means all bundles are treated as different
    expect(bundle.compareTo(EasyMock.anyObject())).andReturn(1).anyTimes();
    expect(bundle.getBundleId()).andReturn(id).anyTimes();
    expect(bundle.getSymbolicName()).andReturn(symbolicName).anyTimes();
    expect(bundle.getHeaders()).andReturn(headers).anyTimes();
    BundleStartLevel sl = EasyMock.createMock(BundleStartLevel.class);
    expect(sl.isPersistentlyStarted()).andReturn(true).anyTimes();
    expect(bundle.adapt(BundleStartLevel.class)).andReturn(sl).anyTimes();
    replay(bundle, sl);
    return bundle;
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) Bundle(org.osgi.framework.Bundle)

Example 3 with BundleStartLevel

use of org.osgi.framework.startlevel.BundleStartLevel in project sling by apache.

the class BundleUpdateTask 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 String symbolicName = (String) getResource().getAttribute(Constants.BUNDLE_SYMBOLICNAME);
    final Bundle b = BundleInfo.getMatchingBundle(this.getBundleContext(), symbolicName, null);
    if (b == null) {
        String message = MessageFormat.format("Bundle to update ({0}) not found", symbolicName);
        this.getLogger().debug(message);
        this.setFinishedState(ResourceState.IGNORED, null, message);
        return;
    }
    final Version newVersion = new Version((String) getResource().getAttribute(Constants.BUNDLE_VERSION));
    // Do not update if same version, unless snapshot
    boolean snapshot = false;
    final Version currentVersion = b.getVersion();
    snapshot = BundleInfo.isSnapshot(newVersion);
    if (currentVersion.equals(newVersion) && !snapshot) {
        // TODO : Isn't this already checked in the task creator?
        String message = MessageFormat.format("Same version is already installed, and not a snapshot, ignoring update: {0}", getResource());
        this.getLogger().debug(message);
        this.setFinishedState(ResourceState.INSTALLED, null, message);
        return;
    }
    try {
        // If the bundle is active before the update - restart it once updated, but
        // in sequence, not right now
        final boolean reactivate = this.isBundleActive(b);
        // if this is not a fragment, stop the bundle
        final int state = b.getState();
        if (state == Bundle.ACTIVE || state == Bundle.STARTING) {
            b.stop();
        }
        // update bundle
        b.update(getResource().getInputStream());
        ctx.log("Updated bundle {} from resource {}", b, getResource());
        // start level handling - after update to avoid starting the bundle
        // just before the update
        final BundleStartLevel startLevelService = b.adapt(BundleStartLevel.class);
        final int newStartLevel = this.getBundleStartLevel();
        final int oldStartLevel = startLevelService.getStartLevel();
        if (newStartLevel != oldStartLevel && newStartLevel != 0) {
            startLevelService.setStartLevel(newStartLevel);
            ctx.log("Set start level for bundle {} to {}", b, newStartLevel);
        }
        if (reactivate) {
            if (BundleUtil.isSystemBundleFragment(b)) {
                this.setFinishedState(ResourceState.INSTALLED);
                ctx.addTaskToCurrentCycle(new SystemBundleUpdateTask(null, this.getTaskSupport()));
            } else if (BundleUtil.getFragmentHostHeader(b) != null) {
                // if this is a fragment, we're done after a refresh of the host
                final String fragmentHostHeader = BundleUtil.getFragmentHostHeader(b);
                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 {
                BundleUtil.markBundleStart(this.getResource());
                RefreshBundlesTask.markBundleForRefresh(ctx, this.getTaskSupport(), b);
                ctx.addTaskToCurrentCycle(new BundleStartTask(this.getResourceGroup(), b.getBundleId(), this.getTaskSupport()));
            }
        } else {
            this.setFinishedState(ResourceState.INSTALLED);
        }
    } catch (final Exception e) {
        int retries = 0;
        Object obj = getResource().getTemporaryAttribute(ATTR_UPDATE_RETRY);
        if (obj instanceof Integer) {
            retries = (Integer) obj;
        }
        getResource().setTemporaryAttribute(ATTR_UPDATE_RETRY, Integer.valueOf(++retries));
        if (retries > MAX_RETRIES) {
            String message = MessageFormat.format("Removing failing update task due to {0} - unable to retry: {1}", e.getLocalizedMessage(), this);
            this.getLogger().error(message, e);
            this.setFinishedState(ResourceState.IGNORED, null, message);
        } else {
            String message = MessageFormat.format("Failing update task due to {0} - will retry up to {1} more time(s) for {2} later", e.getLocalizedMessage(), MAX_RETRIES - (retries - 1), this);
            this.getLogger().warn(message, e);
        }
    }
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) Version(org.osgi.framework.Version) Bundle(org.osgi.framework.Bundle)

Example 4 with BundleStartLevel

use of org.osgi.framework.startlevel.BundleStartLevel in project aries by apache.

the class StartAction method startBundleResource.

private void startBundleResource(Resource resource, Coordination coordination) throws BundleException {
    if (target.isRoot())
        // root region.
        return;
    if (Utils.isRegionContextBundle(resource))
        // The region context bundle was persistently started elsewhere.
        return;
    final Bundle bundle = ((BundleRevision) resource).getBundle();
    if ((bundle.getState() & (Bundle.STARTING | Bundle.ACTIVE)) != 0)
        return;
    if (logger.isDebugEnabled()) {
        int bundleStartLevel = bundle.adapt(BundleStartLevel.class).getStartLevel();
        Bundle systemBundle = Activator.getInstance().getBundleContext().getBundle(0);
        int fwStartLevel = systemBundle.adapt(FrameworkStartLevel.class).getStartLevel();
        logger.debug("StartAction: starting bundle " + bundle.getSymbolicName() + " " + bundle.getVersion().toString() + " bundleStartLevel=" + bundleStartLevel + " frameworkStartLevel=" + fwStartLevel);
    }
    bundle.start(Bundle.START_TRANSIENT | Bundle.START_ACTIVATION_POLICY);
    if (logger.isDebugEnabled()) {
        logger.debug("StartAction: bundle " + bundle.getSymbolicName() + " " + bundle.getVersion().toString() + " started correctly");
    }
    if (coordination == null)
        return;
    coordination.addParticipant(new Participant() {

        public void ended(Coordination coordination) throws Exception {
        // noop
        }

        public void failed(Coordination coordination) throws Exception {
            bundle.stop();
        }
    });
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) Participant(org.osgi.service.coordinator.Participant) Coordination(org.osgi.service.coordinator.Coordination) Bundle(org.osgi.framework.Bundle) BundleRevision(org.osgi.framework.wiring.BundleRevision) FrameworkStartLevel(org.osgi.framework.startlevel.FrameworkStartLevel) BundleException(org.osgi.framework.BundleException) SubsystemException(org.osgi.service.subsystem.SubsystemException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) CoordinationException(org.osgi.service.coordinator.CoordinationException)

Example 5 with BundleStartLevel

use of org.osgi.framework.startlevel.BundleStartLevel in project karaf by apache.

the class TestBundleFactory method createBundles.

private Bundle[] createBundles() {
    Bundle bundle1 = createBundle(1, "Bundle A");
    Bundle bundle2 = createBundle(2, "Bundle B");
    Bundle bundle3 = createBundle(3, "Bundle C");
    BundleStartLevel bsl = createMock(BundleStartLevel.class);
    ServiceReference<?> ref1 = createServiceRef(Constants.OBJECTCLASS, new String[] { "org.example.MyService" }, "key1", "value1");
    ServiceReference<?> ref2 = createServiceRef(Constants.OBJECTCLASS, new String[] { "org.example.OtherService" }, "key2", 1);
    addRegisteredServices(bundle1, ref1, ref2);
    addRegisteredServices(bundle2, ref2);
    expect(bundle3.getRegisteredServices()).andReturn(null).anyTimes();
    expect(bundle1.getServicesInUse()).andReturn(null).anyTimes();
    addUsedServices(bundle2, ref1);
    addUsedServices(bundle3, ref1, ref2);
    expect(ref1.getUsingBundles()).andReturn(new Bundle[] { bundle2, bundle3 }).anyTimes();
    expect(ref2.getUsingBundles()).andReturn(new Bundle[] { bundle3 }).anyTimes();
    expect(bundle1.adapt(BundleStartLevel.class)).andReturn(bsl).anyTimes();
    expect(bundle2.adapt(BundleStartLevel.class)).andReturn(bsl).anyTimes();
    expect(bundle3.adapt(BundleStartLevel.class)).andReturn(bsl).anyTimes();
    expect(bsl.getStartLevel()).andReturn(80).anyTimes();
    replay(bundle1, bundle2, bundle3, ref1, ref2, bsl);
    return new Bundle[] { bundle1, bundle2, bundle3 };
}
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