Skip to content

Commit 6b8ec39

Browse files
committed
Honor ConfigurationFactory active state
1 parent 04c93c1 commit 6b8ec39

22 files changed

Lines changed: 614 additions & 13 deletions

log4j-1.2-api/src/main/java/org/apache/log4j/config/PropertiesConfigurationFactory.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,22 @@ public class PropertiesConfigurationFactory extends ConfigurationFactory {
4343
*/
4444
protected static final String DEFAULT_PREFIX = "log4j";
4545

46+
@Override
47+
protected boolean isActive() {
48+
return PropertiesUtil.getProperties()
49+
.getBooleanProperty(ConfigurationFactory.LOG4J1_EXPERIMENTAL, Boolean.FALSE);
50+
}
51+
4652
@Override
4753
protected String[] getSupportedTypes() {
48-
if (!PropertiesUtil.getProperties()
49-
.getBooleanProperty(ConfigurationFactory.LOG4J1_EXPERIMENTAL, Boolean.FALSE)) {
50-
return null;
51-
}
5254
return new String[] {FILE_EXTENSION};
5355
}
5456

5557
@Override
5658
public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
59+
if (!isActive()) {
60+
return null;
61+
}
5762
final int interval = PropertiesUtil.getProperties().getIntegerProperty(Log4j1Configuration.MONITOR_INTERVAL, 0);
5863
return new PropertiesConfiguration(loggerContext, source, interval);
5964
}

log4j-1.2-api/src/main/java/org/apache/log4j/config/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Log4j 1.x compatibility layer.
1919
*/
2020
@Export
21-
@Version("2.20.1")
21+
@Version("2.27.0")
2222
@Open("org.apache.logging.log4j.core")
2323
package org.apache.log4j.config;
2424

log4j-1.2-api/src/main/java/org/apache/log4j/xml/XmlConfigurationFactory.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,22 @@ public class XmlConfigurationFactory extends ConfigurationFactory {
4747
*/
4848
protected static final String DEFAULT_PREFIX = "log4j";
4949

50+
@Override
51+
protected boolean isActive() {
52+
return PropertiesUtil.getProperties()
53+
.getBooleanProperty(ConfigurationFactory.LOG4J1_EXPERIMENTAL, Boolean.FALSE);
54+
}
55+
5056
@Override
5157
protected String[] getSupportedTypes() {
52-
if (!PropertiesUtil.getProperties()
53-
.getBooleanProperty(ConfigurationFactory.LOG4J1_EXPERIMENTAL, Boolean.FALSE)) {
54-
return null;
55-
}
5658
return new String[] {FILE_EXTENSION};
5759
}
5860

5961
@Override
6062
public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
63+
if (!isActive()) {
64+
return null;
65+
}
6166
final int interval = PropertiesUtil.getProperties().getIntegerProperty(Log4j1Configuration.MONITOR_INTERVAL, 0);
6267
return new XmlConfiguration(loggerContext, source, interval);
6368
}

log4j-1.2-api/src/main/java/org/apache/log4j/xml/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Log4j 1.x compatibility layer.
1919
*/
2020
@Export
21-
@Version("2.25.3")
21+
@Version("2.27.0")
2222
package org.apache.log4j.xml;
2323

2424
import org.osgi.annotation.bundle.Export;

log4j-1.2-api/src/test/java/org/apache/log4j/LoggerJira3410Test.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727
import org.apache.logging.log4j.core.Appender;
2828
import org.apache.logging.log4j.core.LoggerContext;
2929
import org.apache.logging.log4j.core.config.Configuration;
30+
import org.apache.logging.log4j.test.junit.SetTestProperty;
3031
import org.apache.logging.log4j.util.SortedArrayStringMap;
3132
import org.junit.jupiter.api.Test;
3233

3334
/**
3435
* Tests Jira3410.
3536
*/
37+
@SetTestProperty(key = "log4j1.compatibility", value = "true")
3638
class LoggerJira3410Test {
3739

3840
@Test

log4j-1.2-api/src/test/java/org/apache/log4j/config/AsyncAppenderTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@
2929
import org.apache.log4j.Logger;
3030
import org.apache.log4j.bridge.AppenderAdapter;
3131
import org.apache.logging.log4j.core.LoggerContext;
32+
import org.apache.logging.log4j.test.junit.SetTestProperty;
3233
import org.apache.logging.log4j.test.junit.UsingStatusListener;
3334
import org.junit.jupiter.params.ParameterizedTest;
3435
import org.junit.jupiter.params.provider.MethodSource;
3536

3637
/**
3738
* Test configuration from XML.
3839
*/
40+
@SetTestProperty(key = "log4j1.compatibility", value = "true")
3941
@UsingStatusListener
4042
class AsyncAppenderTest {
4143

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.log4j.config;
18+
19+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
22+
import static org.junit.jupiter.api.Assertions.assertNull;
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
24+
25+
import java.net.URI;
26+
import java.net.URL;
27+
import org.apache.logging.log4j.core.LoggerContext;
28+
import org.apache.logging.log4j.core.config.ConfigurationFactory;
29+
import org.apache.logging.log4j.core.config.ConfigurationSource;
30+
import org.junit.jupiter.api.Test;
31+
import org.junitpioneer.jupiter.WritesSystemProperty;
32+
33+
@WritesSystemProperty
34+
class PropertiesConfigurationFactoryActivationTest {
35+
36+
@Test
37+
void activationTracksTheCompatibilityProperty() throws Exception {
38+
final String property = ConfigurationFactory.LOG4J1_EXPERIMENTAL;
39+
final String previousValue = System.getProperty(property);
40+
final PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
41+
try {
42+
System.setProperty(property, "false");
43+
assertFalse(factory.isActive());
44+
assertInactive(factory);
45+
46+
System.setProperty(property, "true");
47+
assertTrue(factory.isActive());
48+
assertArrayEquals(new String[] {".properties"}, factory.getSupportedTypes());
49+
final URL resource = getClass().getResource("/config-1.2/log4j-console-SimpleLayout.properties");
50+
final ConfigurationSource source = ConfigurationSource.fromUri(resource.toURI());
51+
try (final LoggerContext context = new LoggerContext("test")) {
52+
assertInstanceOf(PropertiesConfiguration.class, factory.getConfiguration(context, source));
53+
}
54+
55+
System.setProperty(property, "false");
56+
assertFalse(factory.isActive());
57+
assertInactive(factory);
58+
} finally {
59+
if (previousValue == null) {
60+
System.clearProperty(property);
61+
} else {
62+
System.setProperty(property, previousValue);
63+
}
64+
}
65+
}
66+
67+
private static void assertInactive(final PropertiesConfigurationFactory factory) {
68+
assertArrayEquals(new String[] {".properties"}, factory.getSupportedTypes());
69+
assertNull(factory.getConfiguration(null, ConfigurationSource.NULL_SOURCE));
70+
assertNull(factory.getConfiguration(null, "test", URI.create("classpath:log4j.properties")));
71+
assertNull(factory.getConfiguration(
72+
null,
73+
"test",
74+
URI.create("classpath:log4j.properties"),
75+
PropertiesConfigurationFactoryActivationTest.class.getClassLoader()));
76+
}
77+
}

log4j-1.2-api/src/test/java/org/apache/log4j/config/PropertiesConfigurationTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@
5050
import org.apache.logging.log4j.core.filter.Filterable;
5151
import org.apache.logging.log4j.core.filter.LevelRangeFilter;
5252
import org.apache.logging.log4j.core.layout.PatternLayout;
53+
import org.apache.logging.log4j.test.junit.SetTestProperty;
5354
import org.junit.jupiter.api.Test;
5455

5556
/**
5657
* Test configuration from Properties.
5758
*/
59+
@SetTestProperty(key = "log4j1.compatibility", value = "true")
5860
class PropertiesConfigurationTest extends AbstractLog4j1ConfigurationTest {
5961

6062
private static final String TEST_KEY = "log4j.test.tmpdir";

log4j-1.2-api/src/test/java/org/apache/log4j/config/PropertiesReconfigurationTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@
4343
import org.apache.logging.log4j.core.config.ConfigurationListener;
4444
import org.apache.logging.log4j.core.config.LoggerConfig;
4545
import org.apache.logging.log4j.core.config.Reconfigurable;
46+
import org.apache.logging.log4j.test.junit.SetTestProperty;
4647
import org.junit.jupiter.api.Test;
4748

4849
/**
4950
* Test reconfiguring with an XML configuration.
5051
*/
52+
@SetTestProperty(key = "log4j1.compatibility", value = "true")
5153
class PropertiesReconfigurationTest {
5254

5355
private class TestListener implements ConfigurationListener {

log4j-1.2-api/src/test/java/org/apache/log4j/config/SocketAppenderConfigurationTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
import org.apache.logging.log4j.core.filter.ThresholdFilter;
3232
import org.apache.logging.log4j.core.net.Protocol;
3333
import org.apache.logging.log4j.core.net.TcpSocketManager;
34+
import org.apache.logging.log4j.test.junit.SetTestProperty;
3435
import org.junit.jupiter.api.Test;
3536

3637
/**
3738
* Tests configuring a Syslog appender.
3839
*/
40+
@SetTestProperty(key = "log4j1.compatibility", value = "true")
3941
class SocketAppenderConfigurationTest {
4042

4143
private SocketAppender check(final Protocol expected, final Configuration configuration) {

0 commit comments

Comments
 (0)