This is an automated email from the ASF dual-hosted git repository. pkarwasz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 430ff520575f359402883a5e118029f7ffc7d462 Author: Piotr P. Karwasz <[email protected]> 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 25ae2a715e..5e5d653b83 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 @@ -202,9 +202,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 (final 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); } } @@ -224,26 +230,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
