The framework passes a BundleContext object to your bundle when it invokes your BundleActivator's start method. Your bundle can use the BundleContext object to interact with the framework by calling the methods of the BundleContext object. One method that your bundle can call is registerService, which uses a service object and an interface name to register a service with the framework's service registry.

The framework passes a BundleContext object to your bundle when it invokes your BundleActivator's start method. Your bundle can use the BundleContext object to interact with the framework by calling the methods of the BundleContext object. One method that your bundle can call is registerService, which uses a service object and an interface name to register a service with the framework's service registry.

The recommended approach for using services is to provide all interface and object classes referred to in the service definition in a bundle separate from the service implementation. The service implementation bundle then imports the packages from the defining bundle, and exports no packages of its own. Therefore in a typical service usage, there are three bundles involved – a service interface bundle, the service implementation, and the service consumer.

In the following example, three bundles are created:

InterfaceBundle

ServiceImplBundle

ServiceConsumerBundle

Interface Bundle

The InterfaceBundle exports the com.ibm.osg.example.mtservice package that contains the com.ibm.osg.example.mtservice.MyTestService interface. The InterfaceBundle adds an Export-Package: com.ibm.osg.example.mtservice to its MANIFEST.MF file. Since this bundle has no initialization or startup needs, no BundleActivator is required for this bundle. This interface defines a service than can print a message:

package com.ibm.osg.example.mtservice;

public interface MyTestService {

// One method is provided by the service.

// This method will simply print

// the message to standard out.

public void printMessage(String message);

}

Service Implementation Bundle

The ServiceImplBundle provides an implementation of the MyTestService (Other bundles could provide alternative implementations). The ServiceImplBundle exports no packages, but does contain an Import-Package: com.ibm.osg.example.mtservice in its MANIFEST.MF file so that it can have access to the MyTestService interface.

The following class provides the implementation for our service. In the following example, a service called com.ibm.osg.example.mtservice.MyTestService registers with the framework. This implementation of the service prints the message to the standard output. Generally, packages containing service implementation classes should not be exported to other bundles.

The following BundleActivator class registers the com.ibm.osg.example.mtservice.MyTestService service with the framework.

package com.ibm.osg.example.mytestservice;

import org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext;

import org.osgi.framework.ServiceRegistration;

public class MyBundleActivator implements BundleActivator {

ServiceRegistration registration;

/*Create a new instance of the TestService

and then use the BundleContext object to

register it.

Store the registration object

to use to unregister the service when the

bundle is

stopped by the framework.

*/

public void start(BundleContext context)

{

MyTestService testservice = new MyTestService();

if( registration == null ){

registration =

context.registerService(

"com.ibm.osg.example.mtservice.MyTestService",

testservice,

null);

}

}

public void stop(BundleContext context) {

if ( registration != null ){

registration.unregister();

}

registration=null;

}

}

The ServiceConsumer bundle, like the ServiceImplBundle, must contain an Import-Package: com.ibm.osg.example.mtservice in its MANIFEST.MF file. It may optionally contain an Import-Service: com.ibm.osg.example.mtservice.MyTestService. This is recommended as the tools will use this to ensure the framework has the proper prerequisites, but this is not required. See the section Getting and un-getting services from the OSGi Framework for an example of the ServiceConsumer bundle.