Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know what JAXB implementation is used?

I am using MOXy as JAXB Implementation but somehow I would like to show the Implementation Name (e.g. Moxy) and the version number on some admin screen (dynamically).

How can I retrieve that info from JAXB?

Cheers

like image 313
basZero Avatar asked Jan 24 '11 12:01

basZero


People also ask

What is JAXB reference implementation?

The Java™ Architecture for XML Binding (JAXB) provides an API and tools that automate the mapping between XML documents and Java objects. The JAXB framework enables developers to perform the following operations: Unmarshal XML content into a Java representation. Access and update the Java representation.

What version of JAXB is in Java 8?

1. 2.4. 0-* (under development)

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.

Is JAXB included in JDK?

The JAXB-specific xjc and schemagen tools, which you use to convert an XML Schema (*. xsd file) to a set of Java classes and vice versa, are included with the JDK up to version 10, but have been removed in JDK 11.

Is JAXB still supported?

As of Java 11, JAXB is not part of the JRE anymore and you need to configure the relevant libraries via your dependency management system, for example Maven or Gradle. This tutorial demonstrates both approaches. The JAXB reference implementation is developed on Github Jaxb api project page.

When should JAXB be used?

JAXB stands for Java Architecture for XML Binding. It provides mechanism to marshal (write) java objects into XML and unmarshal (read) XML into object. Simply, you can say it is used to convert java object into xml and vice-versa.


2 Answers

Based on Blaise Doughan's answer, a slight modification (JUnit test). Note that the package of the Metro implementation seems to have changed (maybe around Java6u4). There still is no self-inspection interface? SAD!

import org.junit.Test;

public class JaxbVersion {

    @Test
    public void printJaxbInformation() throws JAXBException {
        String MOXY = "org.eclipse.persistence.jaxb";
        String METRO_EARLY = "com.sun.xml.bind.v2";
        String METRO_LATE = "com.sun.xml.internal.bind.v2"; // since JDK 6u4 (?)
        String CAMEL = "org.apache.camel.converter.jaxb";       
        Class<?> clazz = SomeJaxbGeneratedClass.class;
        // http://docs.oracle.com/javaee/7/api/javax/xml/bind/JAXBContext.html
        JAXBContext jc = JAXBContext.newInstance(clazz); 
        String jcClassName = jc.getClass().getName();
        String res;
        if (jcClassName.startsWith(MOXY)) {
            res = "EclipseLink MOXy";
        } else if (jcClassName.startsWith(METRO_EARLY) || jcClassName.startsWith(METRO_LATE)) {
            res = "Oracle Metro";
        } else if (jcClassName.startsWith(CAMEL)) {
            res = "Apache Camel";
        } else {
            res = "Unknown";
        }
        res = res + "(" + jcClassName + ")";        
        System.out.println(res);
    }

}
like image 157
David Tonhofer Avatar answered Sep 21 '22 18:09

David Tonhofer


You could do something like the following to figure out the JAXB impl being used:

import javax.xml.bind.JAXBContext;

public class Demo {

    private static final String MOXY_JAXB_CONTEXT = "org.eclipse.persistence.jaxb.JAXBContext";
    private static final String METRO_JAXB_CONTEXT = "com.sun.xml.bind.v2.runtime.JAXBContextImpl";

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        String jaxbContextImpl = jc.getClass().getName();
        if(MOXY_JAXB_CONTEXT.equals(jaxbContextImpl)) {
            System.out.println("EclipseLink MOXy");
        } else if(METRO_JAXB_CONTEXT.equals(jaxbContextImpl)) {
            System.out.println("Metro");
        } else {
            System.out.println("Other");
        }
    }

}

You can get information about the EclipseLink version being used from it's Version class:

import org.eclipse.persistence.Version;

public class VersionDemo {

    public static void main(String[] args) {
        System.out.println(Version.getVersion());
    }
}
like image 41
bdoughan Avatar answered Sep 20 '22 18:09

bdoughan