Java Reference
In-Depth Information
(continued)
This doesn't mean your bundles won't be able to do anything. Bundles don't neces-
sarily need to be started in order to do useful things. Remember the paint program
you created in chapter 2: none of the bundles had activators, nor did any of them
need to be started, but you still created a fully functioning application.
To u n d e r s t a n d w h a t 's g o i n g o n i n t h e s h e l l e x a m p l e , w e ' l l n o w i n t r o d u c e y o u t o t h r e e
interfaces (
BundleActivator
,
BundleContext
, and
Bundle
) that are the heart and
soul of the lifecycle layer
API
.
3.2.4
Introducing the lifecycle API
The last section described how the shell bundle declares a
BundleActivator
to hook
into the framework at execution time. We can now look into the details of this inter-
face and the other lifecycle
API
s made available from it to the bundle. This is the bun-
dle's hook into the world of
OSG
i.
BUNDLE ACTIVATOR
As you've seen, adding an activator to the bundle is straightforward, because you only
need to create a class implementing the
BundleActivator
interface, which looks like
this:
public interface BundleActivator {
public void start(BundleContext context) throws Exception;
public void stop(BundleContext context) throws Exception;
}
For the shell example, the activator allows it to become lifecycle aware and gain access
to framework facilities. The following listing shows the activator for the shell bundle.
Listing 3.1 Simple shell bundle activator
package org.foo.shell;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
B
Declares volatile
member field
public class Activator implements BundleActivator {
private volatile Binding m_binding;
public void start(BundleContext context) {
int port = getPort(context);
int max = getMaxConnections(context);
m_binding = getTelnetBinding(context, port, max);
m_binding.start();
System.out.println("Bundle " +
context.getBundle().getSymbolicName() +
" started with bundle id" +
context.getBundle().getBundleId() +
" listening on port " + port);
}
Passes context
into telnet
binding
Starts
binding
E
D