use JAXP compliant XML parser by OSGi service

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

use JAXP compliant XML parser by OSGi service

maggu2810
Hello,

I (assume I) have some problems using a SAXParser "the OSGi way".

Code like this is currently (Karaf 4.2.0 and Java 8) working:

===
final SAXParserFactory factory = SAXParserFactory.newInstance();
final SAXParser saxParser;
try {
    saxParser = factory.newSAXParser();
} catch (ParserConfigurationException | SAXException ex) {
    throw new IllegalStateException(ex);
}
===


I have read this: https://osgi.org/javadoc/r4v41/org/osgi/util/xml/XMLParserActivator.html

So I would assume there exist a SAX XML parser (factory) "somewhere" that can be referenced as an OSGi service.


I  tried Xerces:

===
karaf@root()> bundle:install -s mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.xerces/2.12.0_1

karaf@root()> bundle:classes -a org.apache.servicemix.bundles.xerces | grep SAXParserFactory                                                                                                                         
META-INF/services/javax.xml.parsers.SAXParserFactory | exported: false
org/apache/xerces/jaxp/SAXParserFactoryImpl.class | exported: true
org/apache/xerces/jaxp/javax.xml.parsers.SAXParserFactory | exported: true
META-INF/services/javax.xml.parsers.SAXParserFactory | exported: false
org/apache/xerces/jaxp/SAXParserFactoryImpl.class | exported: true
org/apache/xerces/jaxp/SAXParserFactoryImpl.java | exported: true
org/apache/xerces/jaxp/javax.xml.parsers.SAXParserFactory | exported: true

karaf@root()> service:list -a -n | grep -i sax
[nothing]
===


I also tried to install the OSGi XML util:

===
karaf@root()> bundle:install -s mvn:org.osgi/org.osgi.util.xml/1.0.1

karaf@root()> service:list -a -n | grep -i sax
[nothing]
===


Can you point me to a SAX parser factory that is provided as an OSGi service?

Should I rely that on a working "SAXParserFactory.newInstance()" call?

How to use SAX parsing correctly?

Best regards,
Markus
Reply | Threaded
Open this post in threaded view
|

Re: use JAXP compliant XML parser by OSGi service

maggu2810
I have found also this one:

As I am using a SAX parser I don't think I will run into some "OSGi-compatible concerning class loading" problems..
But WDYT, does it make more sense to rely on a working "SAXParserFactory.newInstance()"?
Reply | Threaded
Open this post in threaded view
|

Re: use JAXP compliant XML parser by OSGi service

Tim Ward-2
Hi,

There’s an OSGi spec all about parsing XML. You can read it at https://osgi.org/specification/osgi.cmpn/7.0.0/util.xml.html. In summary, you want to be injected with a SAXParserFactory service and use that.

Best Regards,

Tim

On 17 Sep 2018, at 13:04, Markus Rathgeb <[hidden email]> wrote:

I have found also this one:

As I am using a SAX parser I don't think I will run into some "OSGi-compatible concerning class loading" problems..
But WDYT, does it make more sense to rely on a working "SAXParserFactory.newInstance()"?

Reply | Threaded
Open this post in threaded view
|

Re: use JAXP compliant XML parser by OSGi service

maggu2810
Hi Tim,

isn't the information of your link about the R7 spec similar to that one I posted in the first mail for R4.1?
It does not work for me (see output of first mail).

Or didn't I get your point?

Best regards,
Markus

Reply | Threaded
Open this post in threaded view
|

Re: use JAXP compliant XML parser by OSGi service

Tim Ward-2
It is similar, but the specification explains how to expose the SAXParserFactory from a bundle using the specification defined Activator. If you don’t have an implementation that has already been enhanced then you can create one based on the instructions in the specification chapter.

Tim

> On 18 Sep 2018, at 15:05, Markus Rathgeb <[hidden email]> wrote:
>
> Hi Tim,
>
> isn't the information of your link about the R7 spec similar to that one I posted in the first mail for R4.1?
> It does not work for me (see output of first mail).
>
> Or didn't I get your point?
>
> Best regards,
> Markus
>

Reply | Threaded
Open this post in threaded view
|

Re: use JAXP compliant XML parser by OSGi service

jbonofre
In reply to this post by maggu2810
Hi Markus,

I don't know what you need.

Internally in Karaf, we are using the SAXParserFactory: it's the case
for instance in XmlUtils.

Is it what you are looking for ?

Regards
JB

On 18/09/2018 15:05, Markus Rathgeb wrote:

> Hi Tim,
>
> isn't the information of your link about the R7 spec similar to that one
> I posted in the first mail for R4.1?
> It does not work for me (see output of first mail).
>
> Or didn't I get your point?
>
> Best regards,
> Markus
>

--
Jean-Baptiste Onofré
[hidden email]
http://blog.nanthrax.net
Talend - http://www.talend.com
Reply | Threaded
Open this post in threaded view
|

Re: use JAXP compliant XML parser by OSGi service

maggu2810
Hi Tim, hi JB,

sorry for not being clear enough...

As written in my initial post / mail the call to "SAXParserFactory.newInstance();" works for me as expected.


* if anyone knows about a bundle that provides a SAXParserFactory OSGi service (as the specification exist for a long time my assumption has been that there exist already bundles that expose the SAXParserFactory as an OSGi service) and could point me to

* if it is safe to use "SAXParserFactory.newInstance();" static method call or are there any reasons not to use it

* if "SAXParserFactory.newInstance();" works as expected in all the cases why that specification at all


The last point "why that specification exist" leads me to the assumption that it is perhaps better to rely on an OSGi service instead of that static method.

If I had not read the document, I would not have questioned that static method.

Regards,
Markus
Reply | Threaded
Open this post in threaded view
|

Re: use JAXP compliant XML parser by OSGi service

Tim Ward-2
* if it is safe to use "SAXParserFactory.newInstance();" static method call or are there any reasons not to use it

This method will use reflection to try to find and load an instance of SAXParserFactory. If you have one built into your Java runtime then this will be found because the classes are visible to everyone. If you want to add an implementation into your OSGi framework then it won’t work because the bundle class loader is not visible to the SAX API. This is a general issue in a modular environment, where the implementation packages are supposed to be hidden. OSGi solves it with Services, JPMS uses the “provides with” stanza in the module-info.

Best Regards,

Tim

On 18 Sep 2018, at 22:22, Markus Rathgeb <[hidden email]> wrote:

Hi Tim, hi JB,

sorry for not being clear enough...

As written in my initial post / mail the call to "SAXParserFactory.newInstance();" works for me as expected.


* if anyone knows about a bundle that provides a SAXParserFactory OSGi service (as the specification exist for a long time my assumption has been that there exist already bundles that expose the SAXParserFactory as an OSGi service) and could point me to

* if it is safe to use "SAXParserFactory.newInstance();" static method call or are there any reasons not to use it

* if "SAXParserFactory.newInstance();" works as expected in all the cases why that specification at all


The last point "why that specification exist" leads me to the assumption that it is perhaps better to rely on an OSGi service instead of that static method.

If I had not read the document, I would not have questioned that static method.

Regards,
Markus