Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving a "loader constraint violation" error on "org.w3c.dom.Node" in an OSGi environment

I'm getting the following error in my OSGi environment:

java.lang.LinkageError: loader constraint violation: loader (instance of <bootloader>) previously initiated loading for a different type with name "org/w3c/dom/Node"
    at javax.imageio.metadata.IIOMetadata.getStandardTree(IIOMetadata.java:716)
    at com.sun.imageio.plugins.gif.GIFImageMetadata.getAsTree(GIFImageMetadata.java:128)
    at com.xmlmind.fo.graphic.GraphicFactoryImpl.getResolution(GraphicFactoryImpl.java:184)
    at com.xmlmind.fo.graphic.GraphicFactoryImpl.createGraphic(GraphicFactoryImpl.java:145)
    at com.xmlmind.fo.graphic.GraphicFactories.createGraphic(GraphicFactories.java:128)
    at com.xmlmind.fo.converter.Converter.createGraphic(Converter.java:1943)
    at com.xmlmind.fo.converter.Converter.startExternalGraphic(Converter.java:1910)
    at com.xmlmind.fo.converter.Converter.startElement(Converter.java:635)
    at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
    at org.apache.xerces.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.SAXParserImpl.parse(Unknown Source)
    at com.xmlmind.fo.converter.Converter.convert(Converter.java:417)

The problem is the bundle where this call is coming from imports org.w3c.dom.Node

(from the "xml-apis" bundle), because this is the return type of the getAsTree(String formatName) method of javax.imageio.metadata.IIOMetadata, while this class itself imports org.w3c.dom.Node from the system bundle, because it resides in the system bundle.

This leads to org.w3c.dom.Node being loaded by two different classloaders and hence to the above displayed loader constraint violation, when the getAsTree method is called.

The "xml-apis" bundle is necessary in this OSGi-environment as it provides a different version of the org.w3c.dom package than the system bundle (e.g. the org.w3c.dom.ElementTraversal class can be found in the "xml-apis" bundle (and is used by other bundles in my environment), but does not exist in the system bundle).

The javax.imageio package is not available in a separate bundle, so I cannot "force" it to use the "xml-apis" import. I also tried explicitly importing version "0.0.0" (the system bundle 'version') of org.w3c.dom in the calling bundle , but this does not work as well, because this leads to "package uses conflicts" on other imports (which have org.w3c.dom as uses constraint).

I'm a bit out of ideas right now. Does anyone have an idea how I can fix this problem? Thank you very much for your help in advance!

like image 863
arjenh Avatar asked Jul 26 '13 12:07

arjenh


1 Answers

One possibility (quite easy in fact in bnd(tools)) is to place the XML APIs bundle on the normal classpath. Assuming the new javax.xml.* are backward compatible, you will get at least one definition of these APIs. In bndtools you would add:

-runpath: ${repo;xml-apis__xml-apis} # assuming the bsn = xml-apis__xml-apis

You will also have to add the extra system packages then.

Of course the real problem is that Java does not version its packages, leaving you with this mess ...

like image 170
Peter Kriens Avatar answered Oct 11 '22 10:10

Peter Kriens