You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2022/06/08 17:29:37 UTC

[logging-log4j2] 01/01: [LOG4J2-3531] Parser configuration workaround for old Xerces

This is an automated email from the ASF dual-hosted git repository.

pkarwasz pushed a commit to branch parser-configuration
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit d5a5ee7512c4b00e8fe69f533584a386ec1e1f46
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Wed Jun 8 19:29:20 2022 +0200

    [LOG4J2-3531] Parser configuration workaround for old Xerces
    
    Some versions of Xerces validate the requested features upon factory
    instantiation, not when `setFeature` is called.
---
 .../log4j/core/config/xml/XmlConfiguration.java    | 34 +++++++---------------
 1 file changed, 11 insertions(+), 23 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java
index 649f013d08..519fc94b3d 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java
@@ -204,9 +204,15 @@ public class XmlConfiguration extends AbstractConfiguration implements Reconfigu
     private static void setFeature(final DocumentBuilderFactory factory, final String featureName, final boolean value) {
         try {
             factory.setFeature(featureName, value);
-        } catch (Exception | LinkageError e) {
-            getStatusLogger().error("Caught {} setting feature {} to {} on DocumentBuilderFactory {}: {}",
-                    e.getClass().getCanonicalName(), featureName, value, factory, e, e);
+            // LOG4J2-3531: Xerces only throw when creating a factory.
+            // In newer versions 'setFeature' does this automatically.
+            factory.newDocumentBuilder();
+        } catch (final ParserConfigurationException e) {
+            LOGGER.warn("The DocumentBuilderFactory [{}] does not support the feature [{}]: {}", factory,
+                    featureName, e);
+        } catch (final AbstractMethodError err) {
+            LOGGER.warn("The DocumentBuilderFactory [{}] is out of date and does not support setFeature: {}", factory,
+                    err);
         }
     }
 
@@ -226,26 +232,8 @@ public class XmlConfiguration extends AbstractConfiguration implements Reconfigu
             LOGGER.warn("The DocumentBuilderFactory [{}] is out of date and does not support XInclude: {}", factory,
                     err);
         }
-        try {
-            // Alternative: We could specify all features and values with system properties like:
-            // -DLog4j.DocumentBuilderFactory.Feature="http://apache.org/xml/features/xinclude/fixup-base-uris true"
-            factory.setFeature(XINCLUDE_FIXUP_BASE_URIS, true);
-        } catch (final ParserConfigurationException e) {
-            LOGGER.warn("The DocumentBuilderFactory [{}] does not support the feature [{}]: {}", factory,
-                    XINCLUDE_FIXUP_BASE_URIS, e);
-        } catch (@SuppressWarnings("ErrorNotRethrown") final AbstractMethodError err) {
-            LOGGER.warn("The DocumentBuilderFactory [{}] is out of date and does not support setFeature: {}", factory,
-                    err);
-        }
-        try {
-            factory.setFeature(XINCLUDE_FIXUP_LANGUAGE, true);
-        } catch (final ParserConfigurationException e) {
-            LOGGER.warn("The DocumentBuilderFactory [{}] does not support the feature [{}]: {}", factory,
-                    XINCLUDE_FIXUP_LANGUAGE, e);
-        } catch (@SuppressWarnings("ErrorNotRethrown") final AbstractMethodError err) {
-            LOGGER.warn("The DocumentBuilderFactory [{}] is out of date and does not support setFeature: {}", factory,
-                    err);
-        }
+        setFeature(factory, XINCLUDE_FIXUP_BASE_URIS, true);
+        setFeature(factory, XINCLUDE_FIXUP_LANGUAGE, true);
     }
 
     @Override