In OSGi, all service implementations are packaged into bundles. You can
make a bundle lifecycle-aware by including a bundle activator to manage the
lifecycle of the bundle. A bundle activator is a Java class that implements the
org.osgi.framework.BundleActivator interface and is instantiated
when the bundle is started. The activator class is the bundle's hook to the
lifecycle layer for management. You create the bundle activator implementation and
add the activator implementation to the bundle's JAR file.
Bundles register services or start processes using the lifecycle methods,
start(BundleContext) and stop(BundleContext). By
implementing the BundleActivator interface, the OSGi framework invokes
the start() and stop() methods to initialize or shutdown
bundle functionality. The OSGi container calls the implemented start()
and stop() methods when the bundle is started and stopped. The
start() method registers the component in the registry for another
bundle to use.
You also get a BundleContext, which references the API to
communicate with he container. All access to the OSGi Framework is through the
bundle context object supplied to the bundle activator. You can use the
BundleContext to register or look up services from the service registry.
You specify the bundle activator in the metadata in the bundle's manifest file.
When a bundle is deployed, the OSGi runtime looks for a
Bundle-Activator in the metadata, as shown in Example 2.1.
The following is an example of a bundle activator:
public class Activator implements BundleActivator
{
public void start(BundleContext context) {
}
public void stop(BundleContext context) {
}
}Figure 8.4 shows another example of a bundle activator.
The "Publishing a Service in an OSGi Container" chapter in
FUSE Services Framework: Developing Applications Using JAX-WS
shows how to create a bundle activator and implement the start() and
stop() methods.