Purpose
Show the simplest bundle that registers a service. This is demonstrated with a component as well as the classic Bundle Activator way.
Prerequisites
- Log Service - org.osgi.service.log
- osgi interfaces, provides all service interfaces for the OSGi specifications.
Instructions
We first create a Log Service implementation class. For simplicity, we only implement the Log Service, and not the Log Reader Service.
Implementation is really simple, we log to the console:
Source: aQute.registerservice.SimpleLogService package aQute.registerservice; import org.osgi.framework.*; import org.osgi.service.log.*; public class SimpleLogService implements org.osgi.service.log.LogService { public void log(int level, String message) { log(level, message, null); } public void log(int level, String message, Throwable exception) { System.err.println(level(level) + ":" + message + ":" + exception); } public void log(ServiceReference sr, int level, String message) { log(sr, level, message, null); } public void log(ServiceReference sr, int level, String message, Throwable exception) { System.err.println(level(level) + ":" + sr.getProperty(Constants.SERVICE_ID) + ":" + message + ":" + exception); } private String level(int level) { switch (level) { case LogService.LOG_DEBUG: return "DEBUG"; case LogService.LOG_ERROR: return "ERROR"; case LogService.LOG_INFO: return "INFO"; case LogService.LOG_WARNING:return "WARNING"; default: return "UNKNOWN(" + level + ")"; } } }
The SimpleLogService must be registered with the OSGi service registry. This requires a BundleContext object; this object is handed over to a Bundle Activator. The following code therefore shows the bundle activator that registers the service.
Source: aQute.registerservice.RegisterService package aQute.registerservice; import java.util.*; import org.osgi.framework.*; import org.osgi.service.log.*; public class RegisterService implements BundleActivator { ServiceRegistration registration; public void start(BundleContext context) throws Exception { Hashtable properties = new Hashtable(); properties.put(Constants.SERVICE_PID, "biz.aQute.snippet.registerservice"); properties.put(Constants.SERVICE_DESCRIPTION, "Simplistic log service that logs to console out. " + "Will not dispatch to LogReader services"); properties.put(Constants.SERVICE_VENDOR, "aQute"); properties.put(Constants.SERVICE_RANKING, new Integer( 1000)); registration = context.registerService(LogService.class .getName(), new SimpleLogService(), properties); } public void stop(BundleContext context) throws Exception { // automatically unregistered } }
Notice that it is not necessary to unregister the service. After the stop method is called, the service is automatically unregistered.
To put it all together
Bnd: aQute.registerservice.bnd
Export-Package: aQute.registerservice
Bundle-Activator: aQute.registerservice.RegisterService
Links
- Activation and deactivation is described in the core spec, chapter 4, specifically section 4.3.5.
- Bundle Activator interface
- aQute.registerservice.jar
- Components make this even less code, see ServiceComponent



