Time for action - creating the Activator

Now that the dependency has been added, we can implement the BundleActivator interface.

We will name our activator BookInventoryMockImplActivator and place it in a different package (say com.packtpub.felix.bookshelf.inventory.impl.mock.activator) and declared as a private package to avoid it being exported along with the other classes of the bundle.

package com.packtpub.felix.bookshelf.inventory.impl.mock.activator;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import com.packtpub.felix.bookshelf.inventory.api.BookInventory;
public class BookInventoryMonkImplActivator
implements BundleActivator
{

The bundle activator start() method is invoked at the start of the service and given a BundleContext reference. This context allows us to register and unregister services (among other operations).

We will keep the service registration reference to be used when unregistering the service.

private ServiceRegistration reg = null;
public void start(BundleContext context) throws Exception {
System.out.println(
"
Starting Book Inventory Mock Impl");
this.reg = context.registerService(
BookInventory.class.getName(),
new BookInventoryMockImpl(), null);
}

The registerService method is used to make a service available for look-up by other bundles on the framework. Its parameters are as follows:

  • String clazz: The class name that will be used to look up the service
  • Object service: The service to be registered
  • Dictionary properties: Optional dictionary of properties attached with this service registration

The second parameter in the registerService method can also be a ServiceFactory object that the framework would use to create an instance of the service.

When the bundle is stopping, the activator stop() method is invoked. We can use the stored service reference to unget the service.

public void stop(BundleContext context) throws Exception {
System.out.println("
Stoping Book Inventory Mock Impl");
if (this.reg!=null) {
context.ungetService(reg.getReference());
this.reg = null;
}
}
}

Note that this is not strictly necessary, as all services from a bundle are unregistered by the framework when the bundle is stopped. However, it's good to know how this is done.

More on Bundle Contexts

The BundleContext can be considered as the proxy that bundles use to interact with the framework. It allows access to framework functionality such as:

  • Registering a BundleListener to get framework events. BundleEvents are fired when:
    • A bundle is resolved (BundleEvent.RESOLVED)
    • A bundle is installed (BundleEvent.INSTALLED)
    • A bundle is about to start (BundleEvent.STARTING)
    • A bundle has started (BundleEvent.STARTED), among others. Have a look at the BundleEvent API Docs online for the other events that can be fired at: (http://www.osgi.org/Specifications/Javadoc).
  • Registering and retrieving registered services as well as unregistering them
  • Installing bundles to the framework, listing the installed bundles or a specific bundle
  • Request for a location (File) to use as persistent storage

The BundleContext object is private to the bundle and can be shared within the bundle. However, it is not supposed to be shared with other bundles.

This should be enough for this class; let's declare it as the bundle activator in the POM and then deploy it. We'll try the bundles in the next chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.117.191