JAXB和Log4j XML配置文件
Log4j 1.x和Log4j 2.x均支持使用XML文件來指定日志記錄配置 。 這篇文章探討了與使用JAXB通過Java類處理這些XML配置文件相關的一些細微差別。 本文中的示例基于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語法由DTD而不是W3C XML Schema定義 。 幸運的是,JDK附帶的JAXB實現提供了一個“實驗性的,不受支持的”選項,用于將DTD用作生成Java類的輸入。 以下命令可用于對log4j.dtd運行xjc命令行工具 。
xjc -p dustin.examples.l4j1 -d src -dtd log4j.dtd
下一個屏幕快照對此進行了演示。
運行上述命令并在屏幕快照中演示了該命令,這導致在src目錄中的Java程序包(稱為dustin.examples.l4fj1中生成Java類, dustin.examples.l4fj1允許從兼容log4j.dtd的XML解log4j.dtd并編入log4j.dtd兼容的XML。
Log4j 2.x的XML配置可以是“簡潔”或“嚴格”的,在本文中,我需要使用“ strict ”,因為這是使用W3C XML Schema文件Log4j-config.xsd和I定義的語法的形式。 需要一個模式來使用JAXB生成Java類。 可以針對該XML模式運行以下命令,以生成表示Log4j2嚴格XML的Java類。
xjc -p dustin.examples.l4j2 -d src Log4j-config.xsd -b l4j2.jxb
運行上述命令會導致在src目錄中的Java包中(稱為dustin.examples.l4j2生成Java類, dustin.examples.l4j2允許從兼容Log4j-config.xsd的XML解dustin.examples.l4j2 ,并允許封裝到Log4j-config.xsd XML 。
在前面的示例中,我包括一個帶有選項-b的JAXB綁定文件 ,后跟該綁定文件的名稱( -b l4j2.jxb )。 需要使用此綁定來避免錯誤,該錯誤阻止xjc生成帶有錯誤消息“屬性“值”的Log4j 2.x兼容Java類。 使用<jaxb:property>解決此沖突。” 已在百慕大的 “ 屬性”值“屬性”中的“ 英國人”中討論了此問題及其解決方法。 用于解決此沖突 。 接下來顯示我在這里使用的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類。 要付出的一小筆代價(除了編寫和引用綁定文件之外)是,將需要在Java類中將KeyValuePairType的“ value”屬性作為名為pairValue而不是value的字段進行訪問。
解組
使用Log4j 1.x的log4j.dtd和Log4j 2.x的Log-config.xsd JAXB生成的類的潛在用例是將 Log4j 1.x XML配置文件轉換為Log4j 2.x “嚴格” 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的名稱空間處理的性質,因此將Log4j 1.x XML解組比某些XML解組要復雜一些。 在Gik的沒有名稱空間的Jaxb UnMarshall和Deepa S的 “ 如何指示JAXB忽略命名空間 ”中描述了這種處理皺紋的方法。 使用這種方法有助于避免錯誤消息:
UnmarshalException:意外元素(uri:“ http://jakarta.apache.org/log4j/”,local:“ configuration”)。 預期要素...
為了解組我在本例中引用log4j.dtd的Log4j 1.x,在使用Java 8運行此代碼時,我需要為Java啟動器提供一個特殊的Java系統屬性。
-Djavax.xml.accessExternalDTD=all
為避免出現錯誤消息,“由于accessExternalDTD屬性設置的限制,由于不允許'文件'訪問,因此無法讀取外部DTD。” 可以在NetBeans的FaqWSDLExternalSchema Wiki頁面上找到有關此內容的其他詳細信息。
如下面的示例代碼所示,使用JAXB生成的Java類編組Log4j 2.x XML非常簡單。
/*** 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);}}在這種編組情況下,有一個微妙之處在剛剛顯示的代碼清單中可能并不明顯。 從Log4j-config.xsd生成的JAXB xjc的類缺少任何帶有@XmlRootElement的類。 從Log4j 1.x log4j.dtd生成的JAXB類確實包含帶有此@XmlRootElement批注的類。 由于基于Log4j 2.x Log4j-config.xsd的Java類沒有此注釋,因此在嘗試直接封送ConfigurationType實例時會發生以下錯誤:
MarshalException –帶有鏈接的異常:[com.sun.istack.internal.SAXException2:由于缺少@XmlRootElement批注,因此無法將類型為“ dustin.examples.l4j2.ConfigurationType”的元素封送為元素]
為避免此錯誤,我改編了上述代碼清單的第18行new ObjectFactory().createConfiguration(ConfigurationType) ,將傳入的ConfigurationType實例上調用new ObjectFactory().createConfiguration(ConfigurationType)的結果編組起來,現在已將其成功編組。
結論
JAXB可用于從Log4j 1.x的log4j.dtd和Log4j 2.x的Log4j-config.xsd生成Java類,但是與此過程相關聯的一些細微差別可以成功地生成這些Java類并使用生成的Java封送和封送XML的類。
翻譯自: https://www.javacodegeeks.com/2016/07/jaxb-log4j-xml-configuration-files.html
總結
以上是生活随笔為你收集整理的JAXB和Log4j XML配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring自动装配依赖包_解决Spri
- 下一篇: CECT手机 cect手机现在成什么牌子