You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2022/01/06 17:42:10 UTC
[logging-log4j2] branch release-2.x updated: Revert to 2.17.0 behavior: Read the system property for each call.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/release-2.x by this push:
new 8dd8a6a Revert to 2.17.0 behavior: Read the system property for each call.
8dd8a6a is described below
commit 8dd8a6afc84d8341c345309cd46ccf87f53f5b49
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Jan 6 12:42:07 2022 -0500
Revert to 2.17.0 behavior: Read the system property for each call.
---
.../apache/logging/log4j/core/net/JndiManager.java | 43 +++++++++++++++++-----
.../logging/log4j/core/net/JndiManagerTest.java | 32 ++++++++++++++--
2 files changed, 63 insertions(+), 12 deletions(-)
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/JndiManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/JndiManager.java
index 5e807b2..ed6d3a3 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/net/JndiManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/net/JndiManager.java
@@ -42,35 +42,60 @@ public class JndiManager extends AbstractManager {
private static final String PREFIX = "log4j2.enableJndi";
private static final String JAVA_SCHEME = "java";
- private static final boolean JNDI_CONTEXT_SELECTOR_ENABLED = isJndiEnabled("ContextSelector");
- private static final boolean JNDI_JDBC_ENABLED = isJndiEnabled("Jdbc");
- private static final boolean JNDI_JMS_ENABLED = isJndiEnabled("Jms");
- private static final boolean JNDI_LOOKUP_ENABLED = isJndiEnabled("Lookup");
-
private final InitialContext context;
private static boolean isJndiEnabled(final String subKey) {
return PropertiesUtil.getProperties().getBooleanProperty(PREFIX + subKey, false);
}
+ /**
+ * Tests whether <em>any</em> JNDI system properties are currently enabled.
+ *
+ * @return whether <em>any</em> JNDI system properties are currently enabled.
+ */
public static boolean isJndiEnabled() {
+ // The value is not cached to allow complex stacks to effect this setting.
return isJndiContextSelectorEnabled() || isJndiJdbcEnabled() || isJndiJmsEnabled() || isJndiLookupEnabled();
}
+ /**
+ * Tests whether the JNDI system properties for ContextSelector is currently enabled.
+ *
+ * @return whether the JNDI system properties for ContextSelector is currently enabled.
+ */
public static boolean isJndiContextSelectorEnabled() {
- return JNDI_CONTEXT_SELECTOR_ENABLED;
+ // The value is not cached to allow complex stacks to effect this setting.
+ return isJndiEnabled("ContextSelector");
}
+ /**
+ * Tests whether the JNDI system properties for JDBC is currently enabled.
+ *
+ * @return whether the JNDI system properties for JDBC is currently enabled.
+ */
public static boolean isJndiJdbcEnabled() {
- return JNDI_JDBC_ENABLED;
+ // The value is not cached to allow complex stacks to effect this setting.
+ return isJndiEnabled("Jdbc");
}
+ /**
+ * Tests whether the JNDI system properties for JMS is currently enabled.
+ *
+ * @return whether the JNDI system properties for JMS is currently enabled.
+ */
public static boolean isJndiJmsEnabled() {
- return JNDI_JMS_ENABLED;
+ // The value is not cached to allow complex stacks to effect this setting.
+ return isJndiEnabled("Jms");
}
+ /**
+ * Tests whether the JNDI system properties for Lookup is currently enabled.
+ *
+ * @return whether the JNDI system properties for Lookup is currently enabled.
+ */
public static boolean isJndiLookupEnabled() {
- return JNDI_LOOKUP_ENABLED;
+ // The value is not cached to allow complex stacks to effect this setting.
+ return isJndiEnabled("Lookup");
}
private JndiManager(final String name, final InitialContext context) {
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/net/JndiManagerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/net/JndiManagerTest.java
index 0b3501c..164a6ad 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/net/JndiManagerTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/net/JndiManagerTest.java
@@ -19,6 +19,7 @@ package org.apache.logging.log4j.core.net;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Properties;
@@ -29,31 +30,57 @@ import org.junit.jupiter.api.Test;
*/
public class JndiManagerTest {
+ private static final String TRUE = "true";
+
@Test
public void testIsJndiContextSelectorEnabled() {
assertFalse(JndiManager.isJndiContextSelectorEnabled());
+ try {
+ System.setProperty("log4j2.enableJndiContextSelector", TRUE);
+ assertTrue(JndiManager.isJndiContextSelectorEnabled());
+ } finally {
+ System.clearProperty("log4j2.enableJndiContextSelector");
+ }
}
@Test
public void testIsJndiEnabled() {
assertFalse(JndiManager.isJndiEnabled());
+ try {
+ System.setProperty("log4j2.enableJndiJms", TRUE);
+ assertTrue(JndiManager.isJndiEnabled());
+ } finally {
+ System.clearProperty("log4j2.enableJndiJms");
+ }
}
@Test
public void testIsJndiJdbcEnabled() {
assertFalse(JndiManager.isJndiJdbcEnabled());
+ try {
+ System.setProperty("log4j2.enableJndiJdbc", TRUE);
+ assertTrue(JndiManager.isJndiJdbcEnabled());
+ } finally {
+ System.clearProperty("log4j2.enableJndiJdbc");
+ }
}
@Test
public void testIsJndiJmsEnabled() {
assertFalse(JndiManager.isJndiJmsEnabled());
+ try {
+ System.setProperty("log4j2.enableJndiJms", TRUE);
+ assertTrue(JndiManager.isJndiJmsEnabled());
+ } finally {
+ System.clearProperty("log4j2.enableJndiJms");
+ }
}
@Test
public void testIsJndiLookupEnabled() {
assertFalse(JndiManager.isJndiLookupEnabled());
}
-
+
@Test
public void testNoInstanceByDefault() {
assertThrows(IllegalStateException.class, () -> JndiManager.getDefaultManager());
@@ -64,6 +91,5 @@ public class JndiManagerTest {
assertThrows(IllegalStateException.class, () -> JndiManager.getJndiManager(null, null, null, null, null, null));
assertThrows(IllegalStateException.class, () -> JndiManager.getJndiManager("A", "A", "A", "A", "A", new Properties()));
}
-
-
+
}