如何知道使用了什么 JAXB 实现?

发布于 2024-10-14 03:21:45 字数 102 浏览 2 评论 0原文

我使用 MOXy 作为 JAXB 实现,但不知何故我想在某些管理屏幕上(动态)显示实现名称(例如 Moxy)和版本号。

我如何从 JAXB 检索该信息?

干杯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房的律动 2024-10-21 03:21:46

您可以执行类似以下操作来确定正在使用的 JAXB impl:

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");
        }
    }

}

您可以从其 Version 类中获取有关正在使用的 EclipseLink 版本的信息:

import org.eclipse.persistence.Version;

public class VersionDemo {

    public static void main(String[] args) {
        System.out.println(Version.getVersion());
    }
}

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());
    }
}
智商已欠费 2024-10-21 03:21:46

基于 Blaise Doughan 的回答,稍作修改(JUnit 测试)。请注意,Metro 实现的包似乎已更改(可能在 Java6u4 左右)。还是没有自检界面?伤心!

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);
    }

}

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);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文