1

Task: Batch process to transform data and store the result as XML-File on disk.

Problem: Marshalling objects, loading JAXBContextFactory in OSGI bundle. Deadlock caused by java.lang.NoClassDefFoundError: org/eclipse/persistence/internal/libraries/asm/ClassWriter in FutureTask:311. No stacktrace or cause.

Question: What kind of setting or import do I miss to get the JAXBContext instantiation working?

Explanation:

For data processing I need to store my POJO Class as XML-File on Disk. The POJO Class is generated from an xsd file with jaxb2-maven-plugin (xjc) filled in the batch process and then saved to disk where it will be further processed.

From an different Stack Overflow question I learned that OSGI uses different classloader and I have to use the right newInstance method to instantiate the JAXBContext. (Answer: https://stackoverflow.com/a/1043807/7461710).

This is my current newInstance call, The PrintDocument class is in the auto generated package com.some.package.generated and the ObjectFactory is in the same package and also generated by the jaxb2-maven-plugin. The Programm fails during the newInstance call.

JAXBContext jaxbContext = JAXBContext.newInstance(PrintDocument.class.getPackage().getName(),
            ObjectFactory.class.getClassLoader());
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

Logfile:

DEBUG XMLUtil:24 -Step 4/4: Save result into file. path: /somewhere/result.xml
DEBUG bind:282 - Searching jaxb.properties
DEBUG bind:300 - Searching the system property
DEBUG bind:565 - Trying to load org.eclipse.persistence.jaxb.JAXBContextFactory
[no further output]

We use bnd to Import packages, I have set the bnd.bnd file in the bundle to

Import-Package: \
    org.eclipse.persistence.jaxb,\
    *

As JAXB implementation I use moxy.

If I add this jaxb.properties file to the generated pojo package:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 

A regular class not found exception is thrown. Even in my unit test. (The unit-test performs the marshalling outside of the OSGI bundle)

javax.xml.bind.JAXBException: Provider org.eclipse.persistence.jaxb.JAXBContextFactory  not found
 - with linked exception:
[java.lang.ClassNotFoundException: org.eclipse.persistence.jaxb.JAXBContextFactory ]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:134)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:293)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:431)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:394)
    [...]

But if I change the newInstance call to JAXBContextFactory.createContext based on this Answer https://stackoverflow.com/a/6820113/7461710, my unit-test works again and the programm gets stuck in the Deadlock caused by java.lang.NoClassDefFoundError: org/eclipse/persistence/internal/libraries/asm/ClassWriter as mentioned above.

Project dependencies:

<dependency>
    <groupId>biz.aQute.bnd</groupId>
    <artifactId>biz.aQute.launcher</artifactId>
    <version>3.3.0</version>
</dependency>
<dependency>
    <groupId>org.eclipse.platform</groupId>
    <artifactId>org.eclipse.osgi</artifactId>
    <version>3.11.2</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.5</version>
</dependency>
<dependency>
    <groupId>org.ops4j.pax.logging</groupId>
    <artifactId>pax-logging-api</artifactId>
    <version>1.9.1</version>
</dependency>
<dependency>
    <groupId>org.ops4j.pax.logging</groupId>
    <artifactId>pax-logging-service</artifactId>
    <version>1.9.1</version>
</dependency>
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20160810</version>
</dependency>
<dependency>
    <groupId>org.zeroturnaround</groupId>
    <artifactId>zt-exec</artifactId>
    <version>1.9</version>
</dependency>
<dependency>
    <groupId>com.io7m.xom</groupId>
    <artifactId>xom</artifactId>
    <version>1.2.10</version>
</dependency>
<dependency>
    <groupId>org.ops4j.pax.jdbc</groupId>
    <artifactId>pax-jdbc</artifactId>
    <version>0.9.0</version>
    <exclusions>
        <exclusion>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.compendium</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.ops4j.pax.jdbc</groupId>
    <artifactId>pax-jdbc-h2</artifactId>
    <version>0.4.0</version>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.193</version>
</dependency>
<dependency>
    <groupId>org.ops4j.pax.jdbc</groupId>
    <artifactId>pax-jdbc-oracle</artifactId>
    <version>0.9.0</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>
<dependency>
    <groupId>org.osgi</groupId>
    <artifactId>org.osgi.core</artifactId>
    <version>6.0.0</version>
</dependency>
<dependency>
    <groupId>org.osgi</groupId>
    <artifactId>osgi.cmpn</artifactId>
    <version>6.0.0</version>
</dependency>
<dependency>
    <groupId>org.apache.felix</groupId>
    <artifactId>org.apache.felix.scr</artifactId>
    <version>2.0.6</version>
</dependency>
<dependency>
    <groupId>org.apache.servicemix.bundles</groupId>
    <artifactId>org.apache.servicemix.bundles.asm</artifactId>
    <version>3.3.1_1</version>
</dependency>
<dependency>
    <groupId>org.apache.servicemix.bundles</groupId>
    <artifactId>org.apache.servicemix.bundles.saxon</artifactId>
    <version>9.7.0-10_1</version>
</dependency>
<dependency>
    <groupId>xml-resolver</groupId>
    <artifactId>xml-resolver</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.felix</groupId>
    <artifactId>org.apache.felix.configadmin</artifactId>
    <version>1.8.12</version>
</dependency>
<dependency>
    <groupId>org.apache.felix</groupId>
    <artifactId>org.apache.felix.gogo.shell</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>org.apache.felix</groupId>
    <artifactId>org.apache.felix.gogo.command</artifactId>
    <version>1.0.0</version>
    <exclusions>
        <exclusion>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.compendium</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.apache.felix</groupId>
    <artifactId>org.apache.felix.gogo.runtime</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.moxy</artifactId>
    <version>2.6.4</version>
</dependency>
Community
  • 1
  • 1
doofmars
  • 11
  • 4
  • I do not have experience with moxy but the JAXB support from the jre works fine for me. Have you tried this? – Christian Schneider Jan 26 '17 at 08:51
  • @ChristianSchneider to what package should i set my JAXBContextFactory in the jaxb.properties? `javax.xml.bind.JAXBContextFactory` is not working – doofmars Jan 26 '17 at 09:16

1 Answers1

1

The default is com.sun.xml.internal.ws.developer.JAXBContextFactory but you should not set it in your bundle as it is a sun private package.

Instead try to use this in your OSGi framework properties:

org.osgi.framework.bootdelegation=com.sun.*

This will allow the JAXBContext to find an instantiate the default JAXBContextImpl.

Christian Schneider
  • 19,420
  • 2
  • 39
  • 64