在OSGI Bundle中创建JAXBContext的问题

时间:2017-01-26 08:11:24

标签: java xml eclipse jaxb osgi

任务:批处理转换数据并将结果作为XML文件存储在磁盘上。

问题:编组对象,在OSGI包中加载JAXBContextFactory。 FutureTask中由java.lang.NoClassDefFoundError: org/eclipse/persistence/internal/libraries/asm/ClassWriter引起的死锁:311。没有堆栈跟踪或原因。

问题:为了让JAXBContext实例化工作,我会错过什么样的设置或导入?

说明:

对于数据处理,我需要将我的POJO类存储为磁盘上的XML文件。 POJO类是从xsd文件生成的,其中jaxb2-maven-plugin(xjc)在批处理过程中填充,然后保存到磁盘,在那里进一步处理。

从另一个Stack Overflow问题我得知OSGI使用不同的类加载器,我必须使用正确的newInstance方法来实例化JAXBContext。 (答案:https://stackoverflow.com/a/1043807/7461710)。

这是我当前的newInstance调用,PrintDocument类位于自动生成的包com.some.package.generated中,ObjectFactory位于同一个包中,也由jaxb2-maven-plugin生成。 在newInstance调用期间,Programm失败。

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

日志文件:

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]

我们使用bnd来导入包,我已经将包中的bnd.bnd文件设置为

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

作为JAXB实现,我使用了moxy。

如果我将此jaxb.properties文件添加到生成的pojo包中:

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

抛出常规类未找到异常。甚至在我的单元测试中。 (单元测试在OSGI包之外执行编组)

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)
    [...]

但是如果我根据此答案https://stackoverflow.com/a/6820113/7461710将newInstance调用更改为JAXBContextFactory.createContext,我的单元测试会再次运行,并且程序会陷入由java.lang.NoClassDefFoundError: org/eclipse/persistence/internal/libraries/asm/ClassWriter导致的死锁中,如上所述

项目依赖项:

<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>

1 个答案:

答案 0 :(得分:1)

默认值为com.sun.xml.internal.ws.developer.JAXBContextFactory,但您不应将其设置在捆绑包中,因为它是sun私有包。

而是尝试在OSGi框架属性中使用它:

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

这将允许JAXBContext找到一个实例化默认的JAXBContextImpl。