View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.config.xml;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.File;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.List;
26  import java.util.Map;
27  
28  import javax.xml.XMLConstants;
29  import javax.xml.parsers.DocumentBuilder;
30  import javax.xml.parsers.DocumentBuilderFactory;
31  import javax.xml.parsers.ParserConfigurationException;
32  import javax.xml.transform.Source;
33  import javax.xml.transform.stream.StreamSource;
34  import javax.xml.validation.Schema;
35  import javax.xml.validation.SchemaFactory;
36  import javax.xml.validation.Validator;
37  
38  import org.apache.logging.log4j.core.LoggerContext;
39  import org.apache.logging.log4j.core.config.AbstractConfiguration;
40  import org.apache.logging.log4j.core.config.Configuration;
41  import org.apache.logging.log4j.core.config.ConfigurationSource;
42  import org.apache.logging.log4j.core.config.ConfiguratonFileWatcher;
43  import org.apache.logging.log4j.core.config.Node;
44  import org.apache.logging.log4j.core.config.Reconfigurable;
45  import org.apache.logging.log4j.core.config.plugins.util.PluginType;
46  import org.apache.logging.log4j.core.config.plugins.util.ResolverUtil;
47  import org.apache.logging.log4j.core.config.status.StatusConfiguration;
48  import org.apache.logging.log4j.core.util.Closer;
49  import org.apache.logging.log4j.core.util.FileWatcher;
50  import org.apache.logging.log4j.core.util.Loader;
51  import org.apache.logging.log4j.core.util.Patterns;
52  import org.apache.logging.log4j.core.util.Throwables;
53  import org.w3c.dom.Attr;
54  import org.w3c.dom.Document;
55  import org.w3c.dom.Element;
56  import org.w3c.dom.NamedNodeMap;
57  import org.w3c.dom.NodeList;
58  import org.w3c.dom.Text;
59  import org.xml.sax.InputSource;
60  import org.xml.sax.SAXException;
61  
62  /**
63   * Creates a Node hierarchy from an XML file.
64   */
65  public class XmlConfiguration extends AbstractConfiguration implements Reconfigurable {
66  
67      private static final String XINCLUDE_FIXUP_LANGUAGE =
68              "http://apache.org/xml/features/xinclude/fixup-language";
69      private static final String XINCLUDE_FIXUP_BASE_URIS =
70              "http://apache.org/xml/features/xinclude/fixup-base-uris";
71      private static final String[] VERBOSE_CLASSES = new String[] {ResolverUtil.class.getName()};
72      private static final String LOG4J_XSD = "Log4j-config.xsd";
73  
74      private final List<Status> status = new ArrayList<>();
75      private Element rootElement;
76      private boolean strict;
77      private String schemaResource;
78  
79      public XmlConfiguration(final LoggerContext loggerContext, final ConfigurationSource configSource) {
80          super(loggerContext, configSource);
81          final File configFile = configSource.getFile();
82          byte[] buffer = null;
83  
84          try {
85              final InputStream configStream = configSource.getInputStream();
86              try {
87                  buffer = toByteArray(configStream);
88              } finally {
89                  Closer.closeSilently(configStream);
90              }
91              final InputSource source = new InputSource(new ByteArrayInputStream(buffer));
92              source.setSystemId(configSource.getLocation());
93              final DocumentBuilder documentBuilder = newDocumentBuilder(true);
94              Document document;
95              try {
96                  document = documentBuilder.parse(source);
97              } catch (final Exception e) {
98                  // LOG4J2-1127
99                  final Throwable throwable = Throwables.getRootCause(e);
100                 if (throwable instanceof UnsupportedOperationException) {
101                     LOGGER.warn(
102                             "The DocumentBuilder {} does not support an operation: {}."
103                             + "Trying again without XInclude...",
104                             documentBuilder, e);
105                     document = newDocumentBuilder(false).parse(source);
106                 } else {
107                     throw e;
108                 }
109             }
110             rootElement = document.getDocumentElement();
111             final Map<String, String> attrs = processAttributes(rootNode, rootElement);
112             final StatusConfiguration statusConfig = new StatusConfiguration().withVerboseClasses(VERBOSE_CLASSES)
113                     .withStatus(getDefaultStatus());
114             for (final Map.Entry<String, String> entry : attrs.entrySet()) {
115                 final String key = entry.getKey();
116                 final String value = getStrSubstitutor().replace(entry.getValue());
117                 if ("status".equalsIgnoreCase(key)) {
118                     statusConfig.withStatus(value);
119                 } else if ("dest".equalsIgnoreCase(key)) {
120                     statusConfig.withDestination(value);
121                 } else if ("shutdownHook".equalsIgnoreCase(key)) {
122                     isShutdownHookEnabled = !"disable".equalsIgnoreCase(value);
123                 } else if ("verbose".equalsIgnoreCase(key)) {
124                     statusConfig.withVerbosity(value);
125                 } else if ("packages".equalsIgnoreCase(key)) {
126                     pluginPackages.addAll(Arrays.asList(value.split(Patterns.COMMA_SEPARATOR)));
127                 } else if ("name".equalsIgnoreCase(key)) {
128                     setName(value);
129                 } else if ("strict".equalsIgnoreCase(key)) {
130                     strict = Boolean.parseBoolean(value);
131                 } else if ("schema".equalsIgnoreCase(key)) {
132                     schemaResource = value;
133                 } else if ("monitorInterval".equalsIgnoreCase(key)) {
134                     final int intervalSeconds = Integer.parseInt(value);
135                     if (intervalSeconds > 0) {
136                         getWatchManager().setIntervalSeconds(intervalSeconds);
137                         if (configFile != null) {
138                             final FileWatcher watcher = new ConfiguratonFileWatcher(this, listeners);
139                             getWatchManager().watchFile(configFile, watcher);
140                         }
141                     }
142                 } else if ("advertiser".equalsIgnoreCase(key)) {
143                     createAdvertiser(value, configSource, buffer, "text/xml");
144                 }
145             }
146             statusConfig.initialize();
147         } catch (final SAXException | IOException | ParserConfigurationException e) {
148             LOGGER.error("Error parsing " + configSource.getLocation(), e);
149         }
150         if (strict && schemaResource != null && buffer != null) {
151             try (InputStream is = Loader.getResourceAsStream(schemaResource, XmlConfiguration.class.getClassLoader())) {
152                 if (is != null) {
153                     final Source src = new StreamSource(is, LOG4J_XSD);
154                     final SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
155                     Schema schema = null;
156                     try {
157                         schema = factory.newSchema(src);
158                     } catch (final SAXException ex) {
159                         LOGGER.error("Error parsing Log4j schema", ex);
160                     }
161                     if (schema != null) {
162                         final Validator validator = schema.newValidator();
163                         try {
164                             validator.validate(new StreamSource(new ByteArrayInputStream(buffer)));
165                         } catch (final IOException ioe) {
166                             LOGGER.error("Error reading configuration for validation", ioe);
167                         } catch (final SAXException ex) {
168                             LOGGER.error("Error validating configuration", ex);
169                         }
170                     }
171                 }
172             } catch (final Exception ex) {
173                 LOGGER.error("Unable to access schema {}", this.schemaResource, ex);
174             }
175         }
176 
177         if (getName() == null) {
178             setName(configSource.getLocation());
179         }
180     }
181 
182     /**
183      * Creates a new DocumentBuilder suitable for parsing a configuration file.
184      * 
185      * @param xIncludeAware enabled XInclude
186      * @return a new DocumentBuilder
187      * @throws ParserConfigurationException
188      */
189     static DocumentBuilder newDocumentBuilder(final boolean xIncludeAware) throws ParserConfigurationException {
190         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
191         factory.setNamespaceAware(true);
192         if (xIncludeAware) {
193             enableXInclude(factory);
194         }
195         return factory.newDocumentBuilder();
196     }
197 
198     /**
199      * Enables XInclude for the given DocumentBuilderFactory
200      *
201      * @param factory a DocumentBuilderFactory
202      */
203     private static void enableXInclude(final DocumentBuilderFactory factory) {
204         try {
205             // Alternative: We set if a system property on the command line is set, for example:
206             // -DLog4j.XInclude=true
207             factory.setXIncludeAware(true);
208         } catch (final UnsupportedOperationException e) {
209             LOGGER.warn("The DocumentBuilderFactory [{}] does not support XInclude: {}", factory, e);
210         } catch (@SuppressWarnings("ErrorNotRethrown") final AbstractMethodError | NoSuchMethodError err) {
211             LOGGER.warn("The DocumentBuilderFactory [{}] is out of date and does not support XInclude: {}", factory,
212                     err);
213         }
214         try {
215             // Alternative: We could specify all features and values with system properties like:
216             // -DLog4j.DocumentBuilderFactory.Feature="http://apache.org/xml/features/xinclude/fixup-base-uris true"
217             factory.setFeature(XINCLUDE_FIXUP_BASE_URIS, true);
218         } catch (final ParserConfigurationException e) {
219             LOGGER.warn("The DocumentBuilderFactory [{}] does not support the feature [{}]: {}", factory,
220                     XINCLUDE_FIXUP_BASE_URIS, e);
221         } catch (@SuppressWarnings("ErrorNotRethrown") final AbstractMethodError err) {
222             LOGGER.warn("The DocumentBuilderFactory [{}] is out of date and does not support setFeature: {}", factory,
223                     err);
224         }
225         try {
226             factory.setFeature(XINCLUDE_FIXUP_LANGUAGE, true);
227         } catch (final ParserConfigurationException e) {
228             LOGGER.warn("The DocumentBuilderFactory [{}] does not support the feature [{}]: {}", factory,
229                     XINCLUDE_FIXUP_LANGUAGE, e);
230         } catch (@SuppressWarnings("ErrorNotRethrown") final AbstractMethodError err) {
231             LOGGER.warn("The DocumentBuilderFactory [{}] is out of date and does not support setFeature: {}", factory,
232                     err);
233         }
234     }
235 
236     @Override
237     public void setup() {
238         if (rootElement == null) {
239             LOGGER.error("No logging configuration");
240             return;
241         }
242         constructHierarchy(rootNode, rootElement);
243         if (status.size() > 0) {
244             for (final Status s : status) {
245                 LOGGER.error("Error processing element {} ({}): {}", s.name, s.element, s.errorType);
246             }
247             return;
248         }
249         rootElement = null;
250     }
251 
252     @Override
253     public Configuration reconfigure() {
254         try {
255             final ConfigurationSource source = getConfigurationSource().resetInputStream();
256             if (source == null) {
257                 return null;
258             }
259             final XmlConfiguration config = new XmlConfiguration(getLoggerContext(), source);
260             return config.rootElement == null ? null : config;
261         } catch (final IOException ex) {
262             LOGGER.error("Cannot locate file {}", getConfigurationSource(), ex);
263         }
264         return null;
265     }
266 
267     private void constructHierarchy(final Node node, final Element element) {
268         processAttributes(node, element);
269         final StringBuilder buffer = new StringBuilder();
270         final NodeList list = element.getChildNodes();
271         final List<Node> children = node.getChildren();
272         for (int i = 0; i < list.getLength(); i++) {
273             final org.w3c.dom.Node w3cNode = list.item(i);
274             if (w3cNode instanceof Element) {
275                 final Element child = (Element) w3cNode;
276                 final String name = getType(child);
277                 final PluginType<?> type = pluginManager.getPluginType(name);
278                 final Node childNode = new Node(node, name, type);
279                 constructHierarchy(childNode, child);
280                 if (type == null) {
281                     final String value = childNode.getValue();
282                     if (!childNode.hasChildren() && value != null) {
283                         node.getAttributes().put(name, value);
284                     } else {
285                         status.add(new Status(name, element, ErrorType.CLASS_NOT_FOUND));
286                     }
287                 } else {
288                     children.add(childNode);
289                 }
290             } else if (w3cNode instanceof Text) {
291                 final Text data = (Text) w3cNode;
292                 buffer.append(data.getData());
293             }
294         }
295 
296         final String text = buffer.toString().trim();
297         if (text.length() > 0 || (!node.hasChildren() && !node.isRoot())) {
298             node.setValue(text);
299         }
300     }
301 
302     private String getType(final Element element) {
303         if (strict) {
304             final NamedNodeMap attrs = element.getAttributes();
305             for (int i = 0; i < attrs.getLength(); ++i) {
306                 final org.w3c.dom.Node w3cNode = attrs.item(i);
307                 if (w3cNode instanceof Attr) {
308                     final Attr attr = (Attr) w3cNode;
309                     if (attr.getName().equalsIgnoreCase("type")) {
310                         final String type = attr.getValue();
311                         attrs.removeNamedItem(attr.getName());
312                         return type;
313                     }
314                 }
315             }
316         }
317         return element.getTagName();
318     }
319 
320     private Map<String, String> processAttributes(final Node node, final Element element) {
321         final NamedNodeMap attrs = element.getAttributes();
322         final Map<String, String> attributes = node.getAttributes();
323 
324         for (int i = 0; i < attrs.getLength(); ++i) {
325             final org.w3c.dom.Node w3cNode = attrs.item(i);
326             if (w3cNode instanceof Attr) {
327                 final Attr attr = (Attr) w3cNode;
328                 if (attr.getName().equals("xml:base")) {
329                     continue;
330                 }
331                 attributes.put(attr.getName(), attr.getValue());
332             }
333         }
334         return attributes;
335     }
336 
337     @Override
338     public String toString() {
339         return getClass().getSimpleName() + "[location=" + getConfigurationSource() + "]";
340     }
341 
342     /**
343      * The error that occurred.
344      */
345     private enum ErrorType {
346         CLASS_NOT_FOUND
347     }
348 
349     /**
350      * Status for recording errors.
351      */
352     private static class Status {
353         private final Element element;
354         private final String name;
355         private final ErrorType errorType;
356 
357         public Status(final String name, final Element element, final ErrorType errorType) {
358             this.name = name;
359             this.element = element;
360             this.errorType = errorType;
361         }
362 
363         @Override
364         public String toString() {
365             return "Status [name=" + name + ", element=" + element + ", errorType=" + errorType + "]";
366         }
367 
368     }
369 
370 }