下面列出了org.osgi.framework.Bundle#START_ACTIVATION_POLICY 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* the actual starting happens here. This method does not modify the
* persistent metadata.
*
* @throws BundleException
* if the bundle cannot be resolved or the Activator throws an
* exception.
*/
synchronized void activate(final int options) throws BundleException {
if (state == ACTIVE) {
return;
}
if (currentRevision.isFragment()) {
return;
}
// step4
if (state == INSTALLED) {
// this time, it is critical to get the bundle resolved
// so if we need exports from other unresolved bundles,
// we will try to resolve them (recursively) to get the bundle
// started
currentRevision.resolve(true);
}
// step5
this.context = framework.createBundleContext(this);
if ((options & Bundle.START_ACTIVATION_POLICY) > 0 && lazyActivation) {
if (state != STARTING) {
beingLazy = true;
state = STARTING;
framework.notifyBundleListeners(BundleEvent.LAZY_ACTIVATION,
this);
}
synchronized (this) {
notify();
}
return;
} else {
beingLazy = false;
}
activate0();
}
public int cmdStart(Dictionary<String,?> opts, Reader in, PrintWriter out,
Session session) {
int startOptions = 0;
if (opts.get("-t") != null) {
startOptions |= Bundle.START_TRANSIENT;
}
if (opts.get("-e") == null) {
startOptions |= Bundle.START_ACTIVATION_POLICY;
}
final Bundle[] b = getBundles((String[]) opts.get("bundle"), true);
boolean found = false;
for (final Bundle element : b) {
if (element != null) {
try {
element.start(startOptions);
out.println("Started: " + showBundle(element));
} catch (final BundleException e) {
Throwable t = e;
while (t instanceof BundleException
&& ((BundleException) t).getNestedException() != null) {
t = ((BundleException) t).getNestedException();
}
out.println("Couldn't start bundle: " + showBundle(element)
+ " (due to: " + t + ")");
t.printStackTrace(out);
}
found = true;
}
}
if (!found) {
out.println("ERROR! No matching bundle");
return 1;
}
return 0;
}
int getStartOptions()
{
int options = 0;
if (itemStartOptionsTransient.getState()) {
options |= Bundle.START_TRANSIENT;
}
if (itemStartOptionsPolicy.getState()) {
options |= Bundle.START_ACTIVATION_POLICY;
}
return options;
}
void syncStartLevel(BundleImpl bs) {
try {
if (fwCtx.debug.startlevel) {
fwCtx.debug.println("syncstartlevel: " + bs);
}
synchronized (lock) {
synchronized (fwCtx.resolver) {
if (bs.getStartLevel() <= currentLevel) {
final BundleGeneration current = bs.current();
if ((bs.getState() & (Bundle.INSTALLED|Bundle.RESOLVED|Bundle.STOPPING)) != 0
&& current.archive.getAutostartSetting()!=-1) {
if (fwCtx.debug.startlevel) {
fwCtx.debug.println("startlevel: start " + bs);
}
int startOptions = Bundle.START_TRANSIENT;
if (isBundleActivationPolicyUsed(current.archive)) {
startOptions |= Bundle.START_ACTIVATION_POLICY;
}
bs.start(startOptions);
}
} else {
if ((bs.getState() & (Bundle.ACTIVE|Bundle.STARTING)) != 0) {
if (fwCtx.debug.startlevel) {
fwCtx.debug.println("startlevel: stop " + bs);
}
bs.stop(Bundle.STOP_TRANSIENT);
}
}
}
}
} catch (final Throwable t) {
fwCtx.frameworkError(bs, t);
}
}
boolean isBundleActivationPolicyUsed(BundleArchive archive) {
return archive != null && archive.getAutostartSetting() == Bundle.START_ACTIVATION_POLICY;
}
/**
* Start this framework.
*
* @see org.osgi.framework.Framework#start
*/
@Override
public void start(int options) throws BundleException {
List<String> bundlesToStart = null;
synchronized (lock) {
waitOnOperation(lock, "Framework.start", true);
switch (state) {
case INSTALLED:
case RESOLVED:
doInit();
// Fall through
case STARTING:
operation = ACTIVATING;
break;
case ACTIVE:
return;
default:
throw new IllegalStateException("INTERNAL ERROR, Illegal state, " + state);
}
if (fwCtx.startLevelController == null) {
bundlesToStart = fwCtx.storage.getStartOnLaunchBundles();
}
}
if (fwCtx.startLevelController != null) {
// start level open is delayed to this point to
// correctly work at restart
fwCtx.startLevelController.open();
} else {
// Start bundles according to their autostart setting.
final Iterator<String> i = bundlesToStart.iterator();
while (i.hasNext()) {
final BundleImpl b = (BundleImpl)fwCtx.bundles.getBundle(i.next());
try {
final int autostartSetting = b.current().archive.getAutostartSetting();
// Launch must not change the autostart setting of a bundle
int option = Bundle.START_TRANSIENT;
if (Bundle.START_ACTIVATION_POLICY == autostartSetting) {
// Transient start according to the bundles activation policy.
option |= Bundle.START_ACTIVATION_POLICY;
}
b.start(option);
} catch (final BundleException be) {
fwCtx.frameworkError(b, be);
}
}
}
synchronized (lock) {
state = ACTIVE;
operation = IDLE;
lock.notifyAll();
fwCtx.listeners.frameworkEvent(new FrameworkEvent(FrameworkEvent.STARTED, this, null));
}
}