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/10/29 19:31:23 UTC
[logging-log4j2] 02/02: Remove dependency on PropertiesUtil from `Strings`
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 37941b6fffe55fce23fb460a13d6fd4ab0a29793
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Sat Oct 29 21:30:16 2022 +0200
Remove dependency on PropertiesUtil from `Strings`
The static initializer of `Strings` depends on `PropertiesUtil` to
retrieve the system's line separator. This might introduce dangerous
recursive dependencies between `Strings`, `PropertiesUtil`,
`ServiceRegistry` and `ServiceLoaderUtil`.
---
.../src/main/java/org/apache/logging/log4j/util/Strings.java | 3 ++-
.../logging/log4j/util3/SystemPropertiesPropertySource.java | 12 ++++++++++++
2 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java
index cc6d38835b..f3f63202e9 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java
@@ -18,6 +18,7 @@ package org.apache.logging.log4j.util;
import org.apache.logging.log4j.util3.Chars;
import org.apache.logging.log4j.util3.PropertiesUtil;
+import org.apache.logging.log4j.util3.SystemPropertiesPropertySource;
import java.util.Iterator;
import java.util.Locale;
@@ -48,7 +49,7 @@ public final class Strings {
* OS-dependent line separator, defaults to {@code "\n"} if the system property {@code ""line.separator"} cannot be
* read.
*/
- public static final String LINE_SEPARATOR = PropertiesUtil.getProperties().getStringProperty("line.separator",
+ public static final String LINE_SEPARATOR = SystemPropertiesPropertySource.getSystemProperty("line.separator",
"\n");
private Strings() {
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util3/SystemPropertiesPropertySource.java b/log4j-api/src/main/java/org/apache/logging/log4j/util3/SystemPropertiesPropertySource.java
index c3e6b9ee65..69de98c411 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util3/SystemPropertiesPropertySource.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util3/SystemPropertiesPropertySource.java
@@ -36,6 +36,18 @@ public class SystemPropertiesPropertySource implements PropertySource {
private static final int DEFAULT_PRIORITY = 0;
private static final String PREFIX = "log4j2.";
+ /**
+ * Used by bootstrap code to get system properties without loading PropertiesUtil.
+ */
+ public static String getSystemProperty(final String key, final String defaultValue) {
+ try {
+ return System.getProperty(key, defaultValue);
+ } catch (SecurityException e) {
+ // Silently ignore the exception
+ return defaultValue;
+ }
+ }
+
@Override
public int getPriority() {
return DEFAULT_PRIORITY;