jaxb xml配置_JAXB和Log4j XML配置文件
jaxb xml配置
Log4j 1.x和Log4j 2.x均支持使用XML文件來(lái)指定日志記錄配置 。 這篇文章探討了與使用JAXB通過(guò)Java類處理這些XML配置文件相關(guān)的一些細(xì)微差別。 本文中的示例基于Apache Log4j 1.2.17 , Apache Log4j 2.6.2和Java 1.8.0_73(帶有JAXB xjc 2.2.8-b130911.1802)。
Log4j 1.x的XML語(yǔ)法由DTD而不是W3C XML Schema定義 。 幸運(yùn)的是,JDK附帶的JAXB實(shí)現(xiàn)提供了一個(gè)“實(shí)驗(yàn)性的,不受支持的”選項(xiàng),可以將DTD用作生成Java類的輸入。 以下命令可用于對(duì)log4j.dtd運(yùn)行xjc命令行工具 。
xjc -p dustin.examples.l4j1 -d src -dtd log4j.dtd
下一個(gè)屏幕快照對(duì)此進(jìn)行了演示。
運(yùn)行上述命令并在屏幕快照中演示了該命令,這導(dǎo)致在src目錄中的Java程序包(稱為dustin.examples.l4fj1中生成Java類, dustin.examples.l4fj1允許從兼容log4j.dtd的XML解log4j.dtd并編入log4j.dtd兼容的XML。
Log4j 2.x的XML配置可以是“簡(jiǎn)潔”或“嚴(yán)格”的,在本文中我需要使用“ strict ”,因?yàn)檫@是使用W3C XML Schema文件Log4j-config.xsd和I定義的語(yǔ)法的形式。 需要一個(gè)模式來(lái)使用JAXB生成Java類。 可以針對(duì)此XML模式運(yùn)行以下命令,以生成表示Log4j2嚴(yán)格XML的Java類。
xjc -p dustin.examples.l4j2 -d src Log4j-config.xsd -b l4j2.jxb
運(yùn)行上面的命令導(dǎo)致Java類的Java包中所產(chǎn)生src目錄下名為dustin.examples.l4j2允許從解組Log4j-config.xsd兼容XML和編組到Log4j-config.xsd - 兼容的XML 。
在上一個(gè)示例中,我包括一個(gè)帶有選項(xiàng)-b的JAXB綁定文件 ,后跟該綁定文件的名稱( -b l4j2.jxb )。 需要使用此綁定來(lái)避免錯(cuò)誤,該錯(cuò)誤阻止xjc生成具有錯(cuò)誤消息“屬性“值”的Log4j 2.x兼容Java類。 使用<jaxb:property>解決此沖突。” 在百慕大的 “ 屬性”值“屬性”中的“ 英國(guó)人 ”中已經(jīng)討論了此問(wèn)題及其解決方法。 用于解決此沖突 。 接下來(lái)顯示我在這里使用的JAXB綁定文件的源。
l4j2.jxb
<jxb:bindings version="2.0"xmlns:jxb="http://java.sun.com/xml/ns/jaxb"xmlns:xsd="http://www.w3.org/2001/XMLSchema"><jxb:bindings schemaLocation="Log4j-config.xsd" node="/xsd:schema"><jxb:bindings node="//xsd:complexType[@name='KeyValuePairType']"><jxb:bindings node=".//xsd:attribute[@name='value']"><jxb:property name="pairValue"/></jxb:bindings></jxb:bindings></jxb:bindings> </jxb:bindings>剛剛顯示的JAXB綁定文件允許xjc成功解析XSD并生成Java類。 要付出的一小筆代價(jià)(除了編寫(xiě)和引用綁定文件之外)是,將需要在Java類中將KeyValuePairType的“ value”屬性作為名為pairValue而不是value的字段進(jìn)行訪問(wèn)。
解組
使用Log4j 1.x的log4j.dtd和Log4j 2.x的Log-config.xsd JAXB生成的類的潛在用例是將 Log4j 1.x XML配置文件轉(zhuǎn)換為L(zhǎng)og4j 2.x “嚴(yán)格” XML配置文件 。 在這種情況下,需要解log4j.dtd兼容Log4j 1.x log4j.dtd的XML和編組log4j.dtd Log4j 2.x Log4j-config.xsd的XML。
下面的代碼清單演示了如何使用先前生成的JAXB類解組Log4j 1.x XML。
/*** Extract the contents of the Log4j 1.x XML configuration file* with the provided path/name.** @param log4j1XmlFileName Path/name of Log4j 1.x XML config file.* @return Contents of Log4j 1.x configuration file.* @throws RuntimeException Thrown if exception occurs that prevents* extracting contents from XML with provided name.*/public Log4JConfiguration readLog4j1Config(final String log4j1XmlFileName)throws RuntimeException{Log4JConfiguration config;try{final File inputFile = new File(log4j1XmlFileName);if (!inputFile.isFile()){throw new RuntimeException(log4j1XmlFileName + " is NOT a parseable file.");}final SAXParserFactory spf = SAXParserFactory.newInstance();final SAXParser sp = spf.newSAXParser();final XMLReader xr = sp.getXMLReader();final JAXBContext jaxbContext = JAXBContext.newInstance("dustin.examples.l4j1");final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();final UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler();xr.setContentHandler(unmarshallerHandler);final FileInputStream xmlStream = new FileInputStream(log4j1XmlFileName);final InputSource xmlSource = new InputSource(xmlStream);xr.parse(xmlSource);final Object unmarshalledObject = unmarshallerHandler.getResult();config = (Log4JConfiguration) unmarshalledObject;}catch (JAXBException | ParserConfigurationException | SAXException | IOException exception){throw new RuntimeException("Unable to read from file " + log4j1XmlFileName + " - " + exception,exception);}return config;}由于log4j.dtd的名稱空間處理的性質(zhì),因此將Log4j 1.x XML解組比某些XML解組要復(fù)雜一些。 Gik的Jaxb UnMarshall(不帶名稱空間)和Deepa S的 “ 如何指示JAXB忽略命名空間 ”中描述了這種處理皺紋的方法。 使用此方法有助于避免錯(cuò)誤消息:
UnmarshalException:意外元素(uri:“ http://jakarta.apache.org/log4j/”,local:“ configuration”)。 預(yù)期要素...
為了解組我在本例中引用log4j.dtd的Log4j 1.x,在使用Java 8運(yùn)行此代碼時(shí),我需要為Java啟動(dòng)器提供特殊的Java系統(tǒng)屬性。
-Djavax.xml.accessExternalDTD=all
為避免出現(xiàn)錯(cuò)誤消息,“由于accessExternalDTD屬性設(shè)置的限制,由于不允許'文件'訪問(wèn),因此無(wú)法讀取外部DTD。” 可以在NetBeans的FaqWSDLExternalSchema Wiki頁(yè)面上找到有關(guān)此內(nèi)容的其他詳細(xì)信息。
如下面的示例代碼所示,使用JAXB生成的Java類編組Log4j 2.x XML非常簡(jiǎn)單。
/*** Write Log4j 2.x "strict" XML configuration to file with* provided name based on provided content.** @param log4j2Configuration Content to be written to Log4j 2.x* XML configuration file.* @param log4j2XmlFile File to which Log4j 2.x "strict" XML* configuration should be written.*/public void writeStrictLog4j2Config(final ConfigurationType log4j2Configuration,final String log4j2XmlFile){try (final OutputStream os = new FileOutputStream(log4j2XmlFile)){final JAXBContext jc = JAXBContext.newInstance("dustin.examples.l4j2");final Marshaller marshaller = jc.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);marshaller.marshal(new ObjectFactory().createConfiguration(log4j2Configuration), os);}catch (JAXBException | IOException exception){throw new RuntimeException("Unable to write Log4 2.x XML configuration - " + exception,exception);}}在這種編組情況下,有一個(gè)微妙之處在剛剛顯示的代碼清單中可能并不明顯。 從Log4j-config.xsd生成的JAXB xjc的類缺少任何帶有@XmlRootElement的類。 從Log4j 1.x log4j.dtd生成的JAXB類確實(shí)包含帶有此@XmlRootElement批注的類。 由于基于Log4j 2.x Log4j-config.xsd的Java類沒(méi)有此注釋,因此在嘗試直接封送ConfigurationType實(shí)例時(shí)會(huì)發(fā)生以下錯(cuò)誤:
MarshalException –帶有鏈接的異常:[com.sun.istack.internal.SAXException2:由于缺少@XmlRootElement批注,因此無(wú)法將類型為“ dustin.examples.l4j2.ConfigurationType”的元素封送為元素]
為避免此錯(cuò)誤,我改編了(上面代碼清單的第18行new ObjectFactory().createConfiguration(ConfigurationType) ,對(duì)傳入的ConfigurationType實(shí)例上調(diào)用new ObjectFactory().createConfiguration(ConfigurationType)進(jìn)行了編組,現(xiàn)在已將其成功編組。
結(jié)論
JAXB可用于從Log4j 1.x的log4j.dtd和Log4j 2.x的Log4j-config.xsd生成Java類,但是與此過(guò)程相關(guān)的一些細(xì)微差別可以成功地生成這些Java類并使用生成的Java封送和封送XML的類。
翻譯自: https://www.javacodegeeks.com/2016/07/jaxb-log4j-xml-configuration-files.html
jaxb xml配置
總結(jié)
以上是生活随笔為你收集整理的jaxb xml配置_JAXB和Log4j XML配置文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux修改字符集(linux修改字符
- 下一篇: apache spark_Apache