Java Reference
In-Depth Information
You use BundleContext.installBundle() to install a bundle. In most framework
implementations, the argument to installBundle() is conveniently interpreted as a
URL in String form from which the bundle JAR file can be retrieved. Because the user
enters the URL argument as a String , you can use it directly to install the bundle. If
the install succeeds, then a new Bundle object corresponding to the newly installed
bundle is returned. The bundle is uniquely identified by this URL , which is used as its
location. This location value will also be used in the future to determine if the bundle
is already installed. If a bundle is already associated with this location value, the
Bundle object associated with the previously installed bundle is returned instead of
installing it again. If the install operation is successful, the command outputs the
installed bundle's identifier.
The bundle context also provides an overloaded installBundle() method for
installing a bundle from an input stream. We won't show this method here, but the
other form of installBundle() accepts a location and an open input stream. When
you use this other form of the method, the location is used purely for identification,
and the bundle JAR file is read from the passed-in
input stream. The framework is responsible for
closing the input stream.
Starting
Start
START COMMAND
Now you have a command to install bundles, so the
next operation you'll want to do is start bundles.
The start command shown in listing 3.6 does just
that (see figure 3.9).
Resolved
Active
Figure 3.9 The start-related portion
of the bundle lifecycle state diagram
Listing 3.6 Bundle start command
package org.foo.shell;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
public class StartCommand extends BasicCommand {
public void exec(String id) throws Exception {
Bundle bundle = getBundle(id);
bundle.start();
}
}
Again, the implementation is pretty easy. You use the method from the base command
class to get the Bundle object associated with the user-supplied identifier, and then
you invoke Bundle.start() to start the bundle associated with the identifier.
The result of Bundle.start() depends on the current state of the associated bun-
dle. If the bundle is INSTALLED , it transitions to ACTIVE via the RESOLVED and STARTING
states. If the bundle is UNINSTALLED , the method throws an IllegalStateException .
If the bundle is either STARTING or STOPPING , start() blocks until the bundle enters
either ACTIVE or RESOLVED . If the bundle is already ACTIVE , calling start() again has
no effect. A bundle must be resolved before it can be started. You don't need to
Search WWH ::




Custom Search