Configuration Service (OSGi)
*DEPRECATED: Do not use*
Static Configuration
When developing code intended to run in the ZipTie Server environment it is sometimes necessary for a component to read static configuration files. If these files are editable by the user they should reside
outside of the OSGi bundle that is using them. ZipTie has developed a trivial Configuration Service that allows your component to discover the location where configuration files are stored.
Your configuration files should be either
in this directory (location) or in a subdirectory
relative to the configuration location.
The Configuration Service is an OSGi Service, which means it can be looked up in the OSGi Service Registry by name. Because an OSGi service is provided by "some other bundle" (you know not where, and shouldn't care) there is a possibility that the bundle providing the service may "come and go" (i.e. may be restarted or uninstalled/reinstalled). In order to cope with this better, OSGi provides what is known as a Service Tracker object, which itself manages the comings and goings of a Service and ensures that you can always get an appropriate reference. A full discussion of OSGi Services are beyond the scope of this document, but I encourage you to learn something of the Service Model as you yourself may wish to employ it. It is a very good way to decouple dynamic bundles in the OSGi environment.
Here is some example code showing how to acquire a reference to the Configuration Service.
import org.osgi.util.tracker.ServiceTracker;
import org.ziptie.server.config.IConfigurationService;
public class Activator implements BundleActivator
{
private ServiceTracker configServiceTracker;
public void start(BundleContext context)
{
configServiceTracker = new ServiceTracker(context, IConfigurationService.class.getName(), null);
configServiceTracker.open();
IConfigurationService configService = (IConfigurationService) configServiceTracker.getService();
URI root = configService.getConfigurationRoot();
URI quartzConfig = URI.create(root.toASCIIString() + "quartz/quartz.properties");
File file = new File(quartzConfig);
// ...
}
public void stop(BundleContext context)
{
configServiceTracker.close();
}
}
It is strongly advised that
whenever you need to access the Configuration Service you
re-acquire a reference from the ServiceTracker by calling the
getService() method. This call is inexpensive and ensures that if the Configuration Service was restarted or updated that you get a valid reference. This is not a pattern unique to the Configuration Service but instead is best practice for all OSGi Services.
Scratch Area