10 July 2013

Monitoring OSGi services and bundles

Keeping track of OSGi services and bundles can be a tedious task. Declerative Services provide a good degree of control here, but for fine grain control the OSGi api presents the org.osgi.util.tracker package.
With the ServiceTracker and BundleTracker classes it is really easy to monitor your enviroment for add, modify or remove events. It provides filtering capabillities trough the customizer interfaces and keeps a record of all registred objects being tracked.
This is also the perfect location to hook into these events, like for updating any logging to be done on the bundles or services in your framework.
The bruise to be avoided here is the addingService / Bundle methodes. These normally return the object that raised the event for the add methode. However, this can be overridden in a subclass to customize the object to be tracked for the one just being added!