Getting started with jSLP-OSGi

jSLP OSGi registers ch.ethz.iks.slp.ServiceLocationManager as ServiceFactory. The registered services are ch.ethz.iks.slp.Advertiser and ch.ethz.iks.slp.Locator and every bundle requesting one of the services will get their own instance. Since requests for ServiceReferences cannot pass parameters to the ServiceFactory, the Advertiser and Locator instances will be created with the empty default Locale and both classes have a setter method .setLocale(Locale locale) to change the locale at runtime (this differs from the jSLP standalone version).

The following example shows how to get Advertiser and Locator instances and use them:

public class SLPTestBundle implements BundleActivator {
	
    public void start(BundleContext context) throws Exception {

        ServiceReference advRef = context.getServiceReference("ch.ethz.iks.slp.Advertiser");
        ServiceReference locRef = context.getServiceReference("ch.ethz.iks.slp.Locator");

        if (advRef != null) {
            System.out.println("Got reference for Advertiser");
            Advertiser advertiser = (Advertiser) context.getService(advRef);

            advertiser.register(new ServiceURL("service:osgi:test://192.168.24.118", 20), null);
        }

        if (locRef != null) {
            System.out.println("Got reference for Locator");
            Locator locator = (Locator) context.getService(locRef);

            ServiceLocationEnumeration slenum = locator.findServices(new ServiceType("service:osgi"), null, null);
            System.out.println("RESULT:");
            while (slenum.hasMoreElements()) {
                System.out.println(slenum.nextElement());
            }
        }
    }


    public void stop(BundleContext context) throws Exception {

    }

}

jSLP OSGi does not require org.osgi.service.log to run but if a bundle is present that provides this service, jSLP OSGi makes use of it. If the service is not present, trace options passed by properties have no effect. jSLP-OSGi has been successfully tested with Concierge OSGi , Knopflerfish , Oscar and Eclipse Equinox .

For developing Eclipse Plugins, it is recommended to checkout the jSLP-OSGi project from the subversion repository . This project is already an Eclipse Plugin and can be used as dependency for your own project. Unfortunately, Eclipse does not show the exception that is thrown when jSLP runs on a linux box as non-root user and it tries to open port 427. Instead, it will print a plain ExceptionInInitializer. The result is a general unavailability of the requested Locator or Advertiser instance.

Furthermore, Eclipse uses a resolving and starting strategy which is different from traditional OSGi. jSLP has Eclipse-LazyStart set so it is started whenever a class from the bundle is accessed. This somewhat collides with the OSGi service model where bundles are completely decoupled. So in the above example, jSLP as a Plugin is never started and no Advertiser or Locator instance can be retrieved. To prevent this, simple use

        ServiceReference advRef = context.getServiceReference(Advertiser.class.getName());
The call to the static field class will force the resolving of ch.ethz.slp.Advertiser, the Plugin will be started and thus the ServiceFactory registered with the OSGi registry. (The same indeed works for the Locator)

For debugging, jSLP-OSGi features the ch.ethz.iks.slp.debug property. If set to true and a LogService is present, jSLP-OSGi prints additional information about internal state changes which makes it easier to debug applications.