Java Reference
In-Depth Information
explicitly resolve the bundle, because the specification requires the framework to
implicitly resolve the bundle if it's not already resolved. If the bundle's dependencies
can't be resolved,
start()
throws a
BundleException
and the bundle can't be used
until its dependencies are satisfied. If this happens, you'll typically install additional
bundles to satisfy the missing dependencies and try to start the bundle again.
I f t h e b u n d l e h a s a n a c t i v a t o r, t h e f r a m e w o r k i n v o k e s t h e
BundleActivator.start()
method when starting the bundle. Any exceptions thrown from the activator result in
a failed attempt to start the bundle and an exception
being thrown from
Bundle.start()
. One last case where
an exception may result is if a bundle tries to start itself;
the specification says attempts to do so should result in
an
IllegalStateException
.
STOP COMMAND
That's it for starting bundles. Now we can look at stop-
ping bundles, which is similar to starting them; see the
next listing and figure 3.10.
Active
Resolved
Stop
Stopping
Figure 3.10
The stop-related
portion of the bundle lifecycle
state diagram
Listing 3.7 Bundle
stop
command
package org.foo.shell;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
public class StopCommand extends BasicCommand {
public void exec(String id) throws Exception {
Bundle bundle = getBundle(id);
bundle.stop();
}
}
Like starting a bundle, stopping a bundle takes a simple call to
Bundle.stop()
on the
Bundle
object retrieved from the specified identifier. As before, you must be mindful
of the bundle's state. If it's
UNINSTALLED
, an
Illegal-
StateException
results. Either
STARTING
or
STOPPING
blocks until
ACTIVE
or
RESOLVED
is reached, respec-
tively. In the
ACTIVE
state, the bundle transitions to
RESOLVED
via the
STOPPING
state. If the bundle has an
activator and the activator's
stop()
method throws an
exception, a
BundleException
is thrown. Finally, a
bundle isn't supposed to change its own state; trying to
do so may result in an
IllegalStateException
.
UPDATE COMMAND
Let's continue with the
update
command in the follow-
ing listing (see figure 3.11).
Update
Installed
Update
Resolved
Figure 3.11
The update-related
portion of the bundle lifecycle
state diagram