Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a particular JAXB implementation?

Tags:

It seems as though I have already done this once in the past, but I cannot find any reference to what I had done to get it to work.

I have a web application in which I want to specify a different JAXB implementation than the one provided by my web server/jre. I downloaded the appropriate artifact from maven, and see that the jar is properly packaged in my war, however, when I start my web app, I see that it is still using the bundled JRE implementation.

I vaguely remember something about a properties file that I could configure, but cannot find a reference to how it needs to be configured. Moreover, if the implementation I want to use is the same one (just a newer version), the class names would be the same as though packaged in the JRE. How can I specify that I want to use the ones bundled in my WAR?

EDIT

I'm currently running on JBoss 7.0.2, with Oracle JDK 1.6_0_27, JAXB RI that comes with the JRE (I think it is v2.1). I'm trying to upgrade to JAXB RI 2.2.5 (found on MvnRepository).

I've done a little more digging this morning, and noticed a strange error message in my logs:

09:43:18,325 WARN  [org.jboss.as.server.deployment] (MSC service thread 1-12) Class Path entry jaxb-api.jar in "/C:/servers/jboss-as-7.0.2.Final/standalone/deployments/LendingSimulationServiceEAR.ear/LendingSimulationService.war/WEB-INF/lib/jaxb-impl-2.2.5.jar"  does not point to a valid jar for a Class-Path reference.
09:43:18,325 WARN  [org.jboss.as.server.deployment] (MSC service thread 1-12) Class Path entry activation.jar in "/C:/servers/jboss-as-7.0.2.Final/standalone/deployments/LendingSimulationServiceEAR.ear/LendingSimulationService.war/WEB-INF/lib/jaxb-impl-2.2.5.jar"  does not point to a valid jar for a Class-Path reference.
09:43:18,326 WARN  [org.jboss.as.server.deployment] (MSC service thread 1-12) Class Path entry jsr173_1.0_api.jar in "/C:/servers/jboss-as-7.0.2.Final/standalone/deployments/LendingSimulationServiceEAR.ear/LendingSimulationService.war/WEB-INF/lib/jaxb-impl-2.2.5.jar"  does not point to a valid jar for a Class-Path reference.
09:43:18,326 WARN  [org.jboss.as.server.deployment] (MSC service thread 1-12) Class Path entry jaxb1-impl.jar in "/C:/servers/jboss-as-7.0.2.Final/standalone/deployments/LendingSimulationServiceEAR.ear/LendingSimulationService.war/WEB-INF/lib/jaxb-impl-2.2.5.jar"  does not point to a valid jar for a Class-Path reference.

Which I found very strange. I wasn't sure where it was finding that information. A little more research found this line in the MANIFEST.MF:

Class-Path: jaxb-api.jar activation.jar jsr173_1.0_api.jar jaxb1-impl.jar

So now I'm even more confused than ever. It would appear that the jaxb implementation depends on the api, activation, jsr and jaxb1 implementation jars. But they aren't listed in the jaxb pom. A little digging online found me this link which discusses how to use JAXB 2.2 in a Java6SE environment. Unfortunately, this has not seemed to work either; I still get the above WARN messages.

I'm using the following snippet to list the JAXB implementation that is running; perhaps this is incorrect?

/**
 * Print the JAXB Implementation information
 */
public static void outputJaxpImplementationInfo() {
    logger.debug(getImplementationInfo("DocumentBuilderFactory", DocumentBuilderFactory.newInstance().getClass()));
    logger.debug(getImplementationInfo("XPathFactory", XPathFactory.newInstance().getClass()));
    logger.debug(getImplementationInfo("TransformerFactory", TransformerFactory.newInstance().getClass()));
    logger.debug(getImplementationInfo("SAXParserFactory", SAXParserFactory.newInstance().getClass()));
}

/**
 * Get the JAXB implementation information for a particular class
 * @param componentName
 * @param componentClass
 * @return
 */
private static String getImplementationInfo(String componentName, Class componentClass) {
    CodeSource source = componentClass.getProtectionDomain().getCodeSource();
    return MessageFormat.format(
            "{0} implementation: {1} loaded from: {2}",
            componentName,
            componentClass.getName(),
            source == null ? "Java Runtime" : source.getLocation());
}

This snippet produces the following log:

10:28:27,402 INFO  [stdout] (MSC service thread 1-14) 2012-04-04 10:28:27,402 DEBUG cws.cs.lendingsimulationservice.util.JAXBUtil  - DocumentBuilderFactory implementation: __redirected.__DocumentBuilderFactory loaded from: file:/C:/servers/jboss-as-7.0.2.Final/jboss-modules.jar
10:28:27,403 INFO  [stdout] (MSC service thread 1-14) 2012-04-04 10:28:27,403 DEBUG cws.cs.lendingsimulationservice.util.JAXBUtil  - XPathFactory implementation: __redirected.__XPathFactory loaded from: file:/C:/servers/jboss-as-7.0.2.Final/jboss-modules.jar
10:28:27,404 INFO  [stdout] (MSC service thread 1-14) 2012-04-04 10:28:27,404 DEBUG cws.cs.lendingsimulationservice.util.JAXBUtil  - TransformerFactory implementation: __redirected.__TransformerFactory loaded from: file:/C:/servers/jboss-as-7.0.2.Final/jboss-modules.jar
10:28:27,406 INFO  [stdout] (MSC service thread 1-14) 2012-04-04 10:28:27,406 DEBUG cws.cs.lendingsimulationservice.util.JAXBUtil  - SAXParserFactory implementation: __redirected.__SAXParserFactory loaded from: file:/C:/servers/jboss-as-7.0.2.Final/jboss-modules.jar
like image 997
Eric B. Avatar asked Apr 03 '12 21:04

Eric B.


People also ask

Why was JAXB removed?

JAXB was integrated within the JVM itself, so you didn't need to add any code dependency to have the functionality within your application. But with the modularization performed in JDK 9, it was removed as the maintainers wanted to have a smaller JDK distribution that only contains the core concepts.

What can I use instead of JAXB?

XOM, JDOM, dom4j, etc. etc. Projects like Castor and Apache XMLBeans predate JAXB, so you could have a look at those. Ulf Dittmer wrote: XOM, JDOM, dom4j, etc.

Is JAXB an API?

Now developers have another Java API at their disposal that can make it easier to access XML documents: Java Architecture for XML Binding (JAXB). A Reference Implementation of the API is now available in the Java Web Services Developer Pack V 1.1.

Is JAXB a framework?

The JAXB binding framework implementation is a runtime API that provides interfaces for unmarshalling, marshalling, and validating XML content in a Java application. The binding framework comprises interfaces in the javax. xml. bind package.


2 Answers

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.

To specify a JAXB (JSR-222) implementation other than the default you need to include a file called jaxb.properties in the same package as your domain classes. The following is an example of the jaxb.properties file used to specify the MOXy implementation of JAXB:

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

For More Information

  • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
like image 169
bdoughan Avatar answered Oct 11 '22 13:10

bdoughan


It is possible to load JAXB implementation from a specific library using the Java Endorsed Standards Override Mechanism. This is also suggested in the guide Using JAXB 2.1/2.2 with JavaSE 6.

Alternatively, in your application server you may modify the class-loader policy for your application to parent last so that classes are first looked up in the libraries bundled with your application before being searched in JRE.

like image 40
Somu Avatar answered Oct 11 '22 12:10

Somu