Skip to content

OSGi and ServiceLoaders

Will Rogers edited this page Oct 28, 2016 · 11 revisions

OSGi

Since CS-Studio is built using Eclipse, it uses OSGi, "a component and service model for Java".

One significant benefit of using OSGi is that it helps keep code modular with well-defined interfaces. In Eclipse, OSGi bundles map to Eclipse plugins.

A second benefit of OSGi is its definition of services. A plugin may advertise that it provides a certain service and other plugins request that service. This allows a pluggable interface whereby different plugins may provide the same service in different ways.

Meanwhile, Project Jigsaw, an implementation of the Java Module System, will arrive in Java 9.

Java SPI

However, Java has its own method of providing services, known as Java-SPI (service provider interface). It can be used to enable framework extension and replaceable components. The SPI infrastructure consists of services defined via interfaces, the concrete implementations of which are provided by various other 3rd parties. The service provider jars declare the service implementations by including a simple text file in the jar under META-INF/services/<name_of_SPI> with a single line consisting of the class name implementing the SPI. The final piece of this infrastructure consists of the ServiceLoader which searches all the jars in the classpath for the Service Providers available for the various SPIs, once found and loaded they are made available to all consumers of that service. The java SPI framework is being increasingly adopted across the java eco system, in particular diirt, jdbc, jaxp, jaxrs, and javax.

OSGi and SPI

We are finding that SPI and OSGi service mechanisms do not work well together. Java SPI breaks down in the osgi environment...with each bundle having its own classloader (which will not discover the service provider in modules not loaded/visible), even after the service providers are discovered there are various others issues associated with osgi rules on import/export of packages and the dynamic lifecycle of bundles (http://coderthoughts.blogspot.com/2011/08/javautilserviceloader-in-osgi.html & http://hwellmann.blogspot.com/2009/04/jdbc-drivers-in-osgi.html)

While in CS-Studio we could follow the philosophy of the author of the Aries project, which states that the osgi services are superior in every possible way and thus should be used everywhere, unless we rewrite the above mentioned libraries and many more we have to come up with a mechanism on how to use java SPI within CS-Studio.

Clone this wiki locally