29.4. Publishing a Service in an OSGi Container
Overview
The bundle activator interface
org.osgi.framework.BundleActivator interface. The BundleActivator interface, shown in Example 29.4, “Bundle Activator Interface”, it has two methods that need to be implemented.
Example 29.4. Bundle Activator Interface
interface BundleActivator
{
public void start(BundleContext context)
throws java.lang.Exception;
public void stop(BundleContext context)
throws java.lang.Exception;
}start() method is called by the container when it starts the bundle. This is where you instantiate and publish the endpoints.
stop() method is called by the container when it stops the bundle. This is where you would stop the endpoints.
Implementing the start method
- Instantiate an
javax.xml.ws.Endpointobject 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 29.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)
{
1 WidgetOrderImpl impl = new WidgetOrderImpl();
2 endpt = Endpoint.create(impl);
3 endpt.publish("http://localhost:9000/SoapContext/SoapPort");
}
...
}Implementing the stop method
Example 29.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();
}
...
}Informing the container
com.widgetvendor.osgi.widgetActivator.
Example 29.7. Bundle Activator Manifest Entry
Bundle-Activator: com.widgetvendor.osgi.widgetActivator

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.