I am developing an application using OSGi (Equinox platform), and one of the bundles needs to parse XML files. So far I implemented this with SAX (javax.xml.parsers.SAXParserFactory) and I would like to retrieve the SAXParserFactory from the platform.
I saw the OSGi standard provides for a XMLParserActivator to allow JAXP implementations to register themselves (http://www.osgi.org/javadoc/r4v41/org/osgi/util/xml/XMLParserActivator.html), so my guess is that there should be some bundles that offer the SAXParserFactory as a service.
However, I could not figure out which bundle to add as dependency in order to find a service that offers a SAXParserFactory. I try to retrieve a service reference using
context.getServiceReferences(SAXParserFactory.class.getName(), "(&(parser.namespaceAware=true)(parser.validating=true))")
Given that XML parsing is a rather common thing to do, I suppose there are implementations available, or other means for getting a XML parser service from the platform.
Any help would be very welcome!
Generally it is not a good idea to use JAXP in OSGi (because of the classloading mechanism primarily) and a much better idea to get the factory like a service.
If you are using equinox, the SaxParserFactory (using the JRE/JDK one you are running on) is actually provided by the System Bundle, which means you don’t need extra bundles:
{javax.xml.parsers.SAXParserFactory}={service.id=6}
Registered by bundle: System Bundle [0]
If you want to write code that deals with the lifecycle layer of the OSGi platform, I would suggest to track the reference, rather than looking it up directly.
There are many approaches for this; I have written about one I call ServiceMediator here.
e.g. for your case (code is under Apache 2 License, Coalevo Project):