Previous Topic

Next Topic

Book Contents

Book Index

Registry Access

Overview

The Registry Access (RegistryAccess) is one of the helper objects available to all scripts executed through OSGiScriptEngine created by OSGiScriptEngineManager. It is available to script through Core.getRegistry() field and can be accessed in most script languages simply as "mbs.registry".

The Registry Access goal is to simplify the usage of the OSGi Registry from script. It has methods for working with OSGi services and for registering a script object as an OSGi Service.

Using Services

There are two classes of methods for working with services – one for tracking services (and receiving notifications for their appearance/disappearance) and the other for executing a one-time action with a service.

Tracking Services

The trackService methods take as arguments a service class and a callback function. The callback function is called with the service object, when the service appears, and with null argument – when the service disappears. The trackService methods create an org.osgi.util.tracker.ServiceTracker object, to do the actual tracking, which is returned as a result from the method (and should be closed when no longer needed). Below there is a trackService.js provided:

tracker = mbs.registry.trackService("org.osgi.service.http.HttpService", function(service){
   if (service !=  null) {
     // register servlet
  }  else {
     // clean up
  }
})

Have a look at the Scripting HTTP Demo to see how the trackService is used.

There are versions of trackService method that accept the service class as a String or Class argument.

Using One-Time Service

The other class of methods is withService, which lets scripts get a service, execute some action with it and then release it. Such methods take as arguments the service class and some callback function to be executed if the service is available, as shown in the JavaScript example below:

mbs.registry.withService("org.osgi.service.event.EventAdmin", function(eventAdmin) {
  eventAdmin.postEvent(...)
})

The callback function can optionally return some result, which will then be returned by the withService method.

By default, if a service of the specified service class is not available in the OSGi Registry at the moment, the withService method fails with IllegalStateException. This behavior can be controlled with the optional silent argument. If silent is true, and service of the given class is not currently available, the withService method simply returns null.

To see how withService is used check the Scripting OSGi Registry Access Demo.

Registering Service

The Registry Access object has a set of methods, which can be used to register a script object as an OSGi Service. All registerService methods take as arguments the service class and service object, implementing the service interface. Under the hood Invocable.getInterface(Object thiz, Class<T> clasz) method is used to generate Java object implementing the given service class, the generated implementation is registered as an OSGi service and the resulting ServiceRegistration is returned from the registerService method.

The script object must have a corresponding member function for each interface method:

handler = {
  handleEvent : function(event) {
    print(event)
  }
}

properties = {"event.topics" : "com/prosyst/mbs/scripting/demo/*"}

mbs.registry.registerService(handler, "org.osgi.service.event.EventHandler", properties)

As seen from the example, an additional argument for the service registration properties can optionally be passed to the registerService.

An example of registerService usage is provided in the Scripting OSGi Registry Access Demo.