Search code examples
osgixdocreport

Does javax.imageio.spi.ServiceRegistry work in an OSGi container?


Can anyone tell me definitively if a osgi bundle that contains code that calls

javax.imageio.spi.ServiceRegistry

to find a service (in META-INF\service) will find that service, if that service implementation is in another bundle.

I'm not finding any documentation that is specific about this. I'm using the felix osgi container. Any pointers would be gratefully received.

I suspect it does work and my problem lies elsewhere as I notice that in the xdocreport osgi bundle fr.opensagres.xdocreport.core, ServiceRegistry gets used here but maybe its not supported on all osgi containers?


Solution

  • Hi thanks everyone for your answers. I think ServiceRegistry will work in an osgi container to instantiate a service but only within the same classloader. And that's facilitated by use of osgi fragments. So as long as the implementer is in a fragment that defines its Fragment-host as the bundle that contains the class that has the ServiceRegistry lookup code, then ServiceRegistry will work.

    This is why it is working in the xdocreport code I linked to. In this case the ServiceLoader code is called from an abstract class in fr.opensagres.xdocreport.core (a bundle), which is extended by concrete class in fr.opensagres.xdocreport.document (so the ServiceRegistry call is in fr.opensagres.xdocreport.document). The Service implementation is in fr.opensagres.xdocreport.document.docx. a fragment whose has defined its frament-host as fr.opensagres.xdocreport.document.

    So fr.opensagres.xdocreport.document and fr.opensagres.xdocreport.document.docx use the same class loader...so it works!