When you develop an application that will be deployed into an OSGi container, you need to coordinate the publishing and stopping of your endpoints with the life-cycle of the bundle in which it is packaged. You want your endpoints published when the bundle is started and you want the endpoints stopped when the bundle is stopped.
You tie your endpoints life-cycle to the bundle's life-cycle by implementing an OSGi bundle activator. A bundle activator is used by the OSGi container to create the resource for a bundle when it is started. The container also uses the bundle activator to clean up the bundles resources when it is stopped.
You create a bundle activator for your application by implementing the
org.osgi.framework.BundleActivator
interface. The BundleActivator
interface, shown in Example 9.4, it has two methods that need to be implemented.
Example 9.4. Bundle Activator Interface
interface BundleActivator { public void start(BundleContext context) throws java.lang.Exception; public void stop(BundleContext context) throws java.lang.Exception; }
The start()
method is called by the container when it starts the bundle. This is where you instantiate
and publish the endpoints.
The stop()
method is called by the container when it stops the bundle. This is where you would stop
the endpoints.
The bundle activator's start method is where you publish your endpoints. To publish your endpoints the start method must do the following:
Instantiate an
javax.xml.ws.Endpoint
object for the service provider.
Create an optional server context to use when publishing the service provider.
Publish the service provider
using one of the publish()
methods.
Example 9.5 shows code for publishing a service provider.
Example 9.5. Bundle Activator Start Method for Publishing an Endpoint
package com.widgetvendor.osgi; import javax.xml.ws.Endpoint; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; public class widgetActivator implements BundleActivator { private Endpoint endpt; ... public void start(BundleContext context) {WidgetOrderImpl impl = new WidgetOrderImpl();
endpt = Endpoint.create(impl);
endpt.publish("http://localhost:9000/SoapContext/SoapPort"); } ... }
The code in Example 9.5 does the following:
The bundle activator's stop method is where you clean up the resources used by your application. Its implementation should include logic for stopping all of the endpoint's published by the application.
Example 9.6 shows a stop method for stopping a published endpoint.
Example 9.6. Bundle Activator Stop Method for Stopping an Endpoint
package com.widgetvendor.osgi; import javax.xml.ws.Endpoint; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; public class widgetActivator implements BundleActivator { private Endpoint endpt; ... public void stop(BundleContext context) { endpt.stop(); } ... }
You must add inform the container that the application's bundle includes a bundle activator. You do this by adding the Bundle-Activator property to the bundle's manifest. This property tells the container which class in the bundle to use when activating the bundle. Its value is the fully qualified name of the class implementing the bundle activator.
Example 9.7 shows a manifest entry for a bundle whose activator is implemented by
the class com.widgetvendor.osgi.widgetActivator
.