Java Reference
In-Depth Information
What does all this mean in simple terms? The framework's active start level and a bun-
dle's start level control whether a bundle can be started. This means that if you explic-
itly start a bundle (invoke
Bundle.start()
on it), it won't activate unless the bundle's
start level is less than or equal to the framework's active start level. In such a case, the
only effect of invoking
Bundle.start()
is that the bundle is persistently marked as
started. If the framework's active start level is eventually changed to a greater or equal
value, the bundle will be automatically activated by the framework.
As you can imagine, changing the active start level of the framework can have a
dramatic impact on the framework, because a lot of bundles may be started or
stopped as a result. When you use the Start Level Service to change the framework's
active start level, all active bundles with start levels greater than the target start level
are stopped, whereas all bundles persistently marked as started with start levels
less than or equal to the target start level are started. When you invoke
StartLevel.
setStartLevel()
, the actual process occurs on a background thread, so the method
returns immediately. The background thread effectively increments or decrements
the current active start level one step at a time, depending on whether the new active
start level is greater than or less than the current active start level, respectively. At each
step, the background thread starts or stops the bundles at that level until the new tar-
get level is reached.
10.2.2
Using the Start Level Service
To i l l u s t r a t e h o w y o u u s e t h e S t a r t L e v e l S e r v i c e , y o u ' l l a d d
startlevel
and
bundlelevel
commands to the shell. These two commands, implemented in the fol-
lowing listing, perform the four functions mentioned earlier.
Listing 10.5 Start Level Service shell commands example
package org.foo.shell;
import java.io.PrintStream;
import org.osgi.service.startlevel.StartLevel;
public class StartLevelCommand extends BasicCommand {
public void exec(String args, PrintStream out, PrintStream err)
throws Exception {
if (args == null) {
out.println(getStartLevelService().getStartLevel());
} else {
getStartLevelService().setStartLevel(
Integer.parseInt(args.trim()));
}
}
...
}
...
Prints
framework's
active start
level
B
public class BundleLevelCommand extends BasicCommand {
public void exec(String args, PrintStream out, PrintStream err)