Skip to content

Commit 67b8137

Browse files
committed
Add API to list supported configuration file extensions
Add getSupportedExtensions() methods to allow external frameworks like Spring Boot to determine which configuration file formats are currently supported at runtime. - Add abstract getSupportedExtensions() method to ConfigurationFactory - Implement getSupportedExtensions() in all ConfigurationFactory subclasses - Add comprehensive test closes #3775
1 parent c5a1955 commit 67b8137

File tree

21 files changed

+241
-7
lines changed

21 files changed

+241
-7
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.io.IOException;
2020
import java.io.InputStream;
21+
import java.util.Collections;
22+
import java.util.List;
2123
import org.apache.logging.log4j.core.LoggerContext;
2224
import org.apache.logging.log4j.core.config.Configuration;
2325
import org.apache.logging.log4j.core.config.ConfigurationException;
@@ -53,4 +55,9 @@ public Configuration getConfiguration(final LoggerContext loggerContext, final C
5355
protected String[] getSupportedTypes() {
5456
return SUFFIXES;
5557
}
58+
59+
@Override
60+
public List<String> getSupportedExtensions() {
61+
return Collections.emptyList();
62+
}
5663
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.log4j.config;
1818

19+
import java.util.Collections;
20+
import java.util.List;
1921
import org.apache.logging.log4j.core.LoggerContext;
2022
import org.apache.logging.log4j.core.config.Configuration;
2123
import org.apache.logging.log4j.core.config.ConfigurationFactory;
@@ -52,6 +54,11 @@ protected String[] getSupportedTypes() {
5254
return new String[] {FILE_EXTENSION};
5355
}
5456

57+
@Override
58+
public List<String> getSupportedExtensions() {
59+
return Collections.emptyList();
60+
}
61+
5562
@Override
5663
public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
5764
final int interval = PropertiesUtil.getProperties().getIntegerProperty(Log4j1Configuration.MONITOR_INTERVAL, 0);

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.21.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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.log4j.xml;
1818

19+
import java.util.Collections;
20+
import java.util.List;
1921
import org.apache.log4j.config.Log4j1Configuration;
2022
import org.apache.logging.log4j.core.LoggerContext;
2123
import org.apache.logging.log4j.core.config.Configuration;
@@ -56,6 +58,11 @@ protected String[] getSupportedTypes() {
5658
return new String[] {FILE_EXTENSION};
5759
}
5860

61+
@Override
62+
public List<String> getSupportedExtensions() {
63+
return Collections.emptyList();
64+
}
65+
5966
@Override
6067
public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
6168
final int interval = PropertiesUtil.getProperties().getIntegerProperty(Log4j1Configuration.MONITOR_INTERVAL, 0);

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.20.2")
21+
@Version("2.21.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/BasicConfigurationFactory.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.apache.log4j;
1818

1919
import java.net.URI;
20+
import java.util.Collections;
21+
import java.util.List;
2022
import org.apache.logging.log4j.Level;
2123
import org.apache.logging.log4j.core.LoggerContext;
2224
import org.apache.logging.log4j.core.config.AbstractConfiguration;
@@ -35,6 +37,11 @@ public String[] getSupportedTypes() {
3537
return new String[] {"*"};
3638
}
3739

40+
@Override
41+
public List<String> getSupportedExtensions() {
42+
return Collections.emptyList();
43+
}
44+
3845
@Override
3946
public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
4047
return new BasicConfiguration(loggerContext);

log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/BasicConfigurationFactory.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.apache.logging.log4j.core.test;
1818

1919
import java.net.URI;
20+
import java.util.Collections;
21+
import java.util.List;
2022
import org.apache.logging.log4j.Level;
2123
import org.apache.logging.log4j.core.LoggerContext;
2224
import org.apache.logging.log4j.core.config.AbstractConfiguration;
@@ -41,6 +43,11 @@ public String[] getSupportedTypes() {
4143
return null;
4244
}
4345

46+
@Override
47+
public List<String> getSupportedExtensions() {
48+
return Collections.emptyList();
49+
}
50+
4451
@Override
4552
public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
4653
return null;

log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the license.
1616
*/
1717
@Export
18-
@Version("2.25.0")
18+
@Version("2.26.0")
1919
@BaselineIgnore("2.25.0")
2020
package org.apache.logging.log4j.core.test;
2121

log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/ConfigurationFactoryTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.apache.logging.log4j.util.Unbox.box;
2020
import static org.junit.jupiter.api.Assertions.assertAll;
2121
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertFalse;
2223
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
2324
import static org.junit.jupiter.api.Assertions.assertNotNull;
2425
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -36,6 +37,10 @@
3637
import org.apache.logging.log4j.core.Logger;
3738
import org.apache.logging.log4j.core.LoggerContext;
3839
import org.apache.logging.log4j.core.appender.ConsoleAppender;
40+
import org.apache.logging.log4j.core.config.json.JsonConfigurationFactory;
41+
import org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory;
42+
import org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory;
43+
import org.apache.logging.log4j.core.config.yaml.YamlConfigurationFactory;
3944
import org.apache.logging.log4j.core.filter.ThreadContextMapFilter;
4045
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
4146
import org.apache.logging.log4j.test.junit.TempLoggingDir;
@@ -130,4 +135,55 @@ void properties(final LoggerContext context) throws IOException {
130135
final Path logFile = loggingPath.resolve("test-properties.log");
131136
checkFileLogger(context, logFile);
132137
}
138+
139+
@Test
140+
void getSupportedExtensions() {
141+
final List<String> extensions = ConfigurationFactory.getActiveFileExtensions();
142+
assertNotNull(extensions);
143+
assertFalse(extensions.isEmpty());
144+
assertTrue(extensions.contains("xml"));
145+
assertTrue(extensions.contains("properties"));
146+
final long distinctCount = extensions.stream().distinct().count();
147+
assertEquals(extensions.size(), distinctCount);
148+
}
149+
150+
@Test
151+
void xmlConfigurationFactoryExtensions() {
152+
final XmlConfigurationFactory factory = new XmlConfigurationFactory();
153+
final List<String> extensions = factory.getSupportedExtensions();
154+
assertEquals(1, extensions.size());
155+
assertEquals("xml", extensions.get(0));
156+
}
157+
158+
@Test
159+
void propertiesConfigurationFactoryExtensions() {
160+
final PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
161+
final List<String> extensions = factory.getSupportedExtensions();
162+
assertEquals(1, extensions.size());
163+
assertEquals("properties", extensions.get(0));
164+
}
165+
166+
@Test
167+
@Tag("json")
168+
void jsonConfigurationFactoryExtensions() {
169+
final JsonConfigurationFactory factory = new JsonConfigurationFactory();
170+
final List<String> extensions = factory.getSupportedExtensions();
171+
assertTrue(extensions.size() == 0 || extensions.size() == 2);
172+
if (!extensions.isEmpty()) {
173+
assertTrue(extensions.contains("json"));
174+
assertTrue(extensions.contains("jsn"));
175+
}
176+
}
177+
178+
@Test
179+
@Tag("yaml")
180+
void yamlConfigurationFactoryExtensions() {
181+
final YamlConfigurationFactory factory = new YamlConfigurationFactory();
182+
final List<String> extensions = factory.getSupportedExtensions();
183+
assertTrue(extensions.size() == 0 || extensions.size() == 2);
184+
if (!extensions.isEmpty()) {
185+
assertTrue(extensions.contains("yml"));
186+
assertTrue(extensions.contains("yaml"));
187+
}
188+
}
133189
}

log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/builder/CustomConfigurationFactory.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.apache.logging.log4j.core.config.builder;
1818

1919
import java.net.URI;
20+
import java.util.Collections;
21+
import java.util.List;
2022
import org.apache.logging.log4j.Level;
2123
import org.apache.logging.log4j.core.Filter;
2224
import org.apache.logging.log4j.core.LoggerContext;
@@ -86,4 +88,9 @@ public Configuration getConfiguration(
8688
protected String[] getSupportedTypes() {
8789
return new String[] {"*"};
8890
}
91+
92+
@Override
93+
public List<String> getSupportedExtensions() {
94+
return Collections.emptyList();
95+
}
8996
}

0 commit comments

Comments
 (0)