JAXB和Log4j 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)行上述命令會(huì)導(dǎo)致在src目錄中的Java包中(稱為dustin.examples.l4j2生成Java類, dustin.examples.l4j2允許從兼容Log4j-config.xsd的XML解dustin.examples.l4j2 ,并允許封裝到Log4j-config.xsd XML 。
在前面的示例中,我包括一個(gè)帶有選項(xiàng)-b的JAXB綁定文件 ,后跟該綁定文件的名稱( -b l4j2.jxb )。 需要使用此綁定來(lái)避免錯(cuò)誤,該錯(cuò)誤阻止xjc生成帶有錯(cuò)誤消息“屬性“值”的Log4j 2.x兼容Java類。 使用<jaxb:property>解決此沖突?!?已在百慕大的 “ 屬性”值“屬性”中的“ 英國(guó)人”中討論了此問(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à)(除了編寫和引用綁定文件之外)是,將需要在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的沒(méi)有名稱空間的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)器提供一個(gè)特殊的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) ,將傳入的ConfigurationType實(shí)例上調(diào)用new ObjectFactory().createConfiguration(ConfigurationType)的結(jié)果編組起來(lái),現(xiàn)在已將其成功編組。
結(jié)論
JAXB可用于從Log4j 1.x的log4j.dtd和Log4j 2.x的Log4j-config.xsd生成Java類,但是與此過(guò)程相關(guān)聯(lián)的一些細(xì)微差別可以成功地生成這些Java類并使用生成的Java封送和封送XML的類。
翻譯自: https://www.javacodegeeks.com/2016/07/jaxb-log4j-xml-configuration-files.html
總結(jié)
以上是生活随笔為你收集整理的JAXB和Log4j XML配置文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: spring自动装配依赖包_解决Spri
- 下一篇: spring配置xml文件_XML配置文