logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nickwilli...@apache.org
Subject svn commit: r1519217 - in /logging/log4j/log4j2/trunk: log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java src/changes/changes.xml
Date Sat, 31 Aug 2013 20:58:41 GMT
Author: nickwilliams
Date: Sat Aug 31 20:58:41 2013
New Revision: 1519217

URL: http://svn.apache.org/r1519217
Log:
[LOG4J2-293] Changed the ConfigurationFactory to recognize and properly use the classpath:
URI scheme in addition to the classloader: URI scheme. Thanks to Abhinav Shah for the patch
this change was based on.

Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java?rev=1519217&r1=1519216&r2=1519217&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
Sat Aug 31 20:58:41 2013
@@ -88,6 +88,18 @@ public abstract class ConfigurationFacto
      */
     protected static final String DEFAULT_PREFIX = "log4j2";
 
+    /**
+     * The name of the classloader URI scheme.
+     */
+    private static final String CLASS_LOADER_SCHEME = "classloader";
+    private static final int CLASS_LOADER_SCHEME_LENGTH = CLASS_LOADER_SCHEME.length() +
1;
+
+    /**
+     * The name of the classpath URI scheme, synonymous with the classloader URI scheme.
+     */
+    private static final String CLASS_PATH_SCHEME = "classpath";
+    private static final int CLASS_PATH_SCHEME_LENGTH = CLASS_PATH_SCHEME.length() + 1;
+
     private static volatile List<ConfigurationFactory> factories = null;
 
     private static ConfigurationFactory configFactory = new Factory();
@@ -221,9 +233,19 @@ public abstract class ConfigurationFacto
             }
         }
         final String scheme = configLocation.getScheme();
-        if (scheme == null || scheme.equals("classloader")) {
+        final boolean isClassLoaderScheme = scheme != null && scheme.equals(CLASS_LOADER_SCHEME);
+        final boolean isClassPathScheme = scheme != null && !isClassLoaderScheme
&& scheme.equals(CLASS_PATH_SCHEME);
+        if (scheme == null || isClassLoaderScheme || isClassPathScheme) {
             final ClassLoader loader = this.getClass().getClassLoader();
-            final ConfigurationSource source = getInputFromResource(configLocation.getPath(),
loader);
+            String path;
+            if (isClassLoaderScheme) {
+                path = configLocation.toString().substring(CLASS_LOADER_SCHEME_LENGTH);
+            } else if (isClassPathScheme) {
+                path = configLocation.toString().substring(CLASS_PATH_SCHEME_LENGTH);
+            } else {
+                path = configLocation.getPath();
+            }
+            final ConfigurationSource source = getInputFromResource(path, loader);
             if (source != null) {
                 return source;
             }
@@ -365,7 +387,7 @@ public abstract class ConfigurationFacto
                     final String[] types = factory.getSupportedTypes();
                     if (types != null) {
                         for (final String type : types) {
-                            if (type.equals("*") || configLocation.getPath().endsWith(type))
{
+                            if (type.equals("*") || configLocation.toString().endsWith(type))
{
                                 final Configuration config = factory.getConfiguration(name,
configLocation);
                                 if (config != null) {
                                     return config;

Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java?rev=1519217&r1=1519216&r2=1519217&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/TestConfigurator.java
Sat Aug 31 20:58:41 2013
@@ -132,6 +132,38 @@ public class TestConfigurator {
     }
 
     @Test
+    public void testFromClassPathWithClassPathPrefix() throws Exception {
+        final LoggerContext ctx = Configurator.initialize("Test1", "classpath:log4j2-config.xml");
+        LogManager.getLogger("org.apache.test.TestConfigurator");
+        Configuration config = ctx.getConfiguration();
+        assertNotNull("No configuration", config);
+        assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
+        final Map<String, Appender> map = config.getAppenders();
+        assertNotNull("Appenders map should not be null.", map);
+        assertTrue("Appenders map should not be empty.", map.size() > 0);
+        assertTrue("Wrong configuration", map.containsKey("List"));
+        Configurator.shutdown(ctx);
+        config = ctx.getConfiguration();
+        assertEquals("Incorrect Configuration.", NullConfiguration.NULL_NAME, config.getName());
+    }
+
+    @Test
+    public void testFromClassPathWithClassLoaderPrefix() throws Exception {
+        final LoggerContext ctx = Configurator.initialize("Test1", "classloader:log4j2-config.xml");
+        LogManager.getLogger("org.apache.test.TestConfigurator");
+        Configuration config = ctx.getConfiguration();
+        assertNotNull("No configuration", config);
+        assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
+        final Map<String, Appender> map = config.getAppenders();
+        assertNotNull("Appenders map should not be null.", map);
+        assertTrue("Appenders map should not be empty.", map.size() > 0);
+        assertTrue("Wrong configuration", map.containsKey("List"));
+        Configurator.shutdown(ctx);
+        config = ctx.getConfiguration();
+        assertEquals("Incorrect Configuration.", NullConfiguration.NULL_NAME, config.getName());
+    }
+
+    @Test
     public void testByName() throws Exception {
         final LoggerContext ctx = Configurator.initialize("-config", null);
         LogManager.getLogger("org.apache.test.TestConfigurator");

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1519217&r1=1519216&r2=1519217&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Sat Aug 31 20:58:41 2013
@@ -21,6 +21,10 @@
   </properties>
   <body>
     <release version="2.0-beta9" date="soon, very soon" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-293" dev="nickwilliams" type="fix" due-to="Abhinav Shah">
+        Changed the ConfigurationFactory to recognize and properly use the classpath: URI
scheme in addition to the
+        classloader: URI scheme.
+      </action>
       <action issue="LOG4J2-359" dev="nickwilliams" type="fix" due-to="Abhinav Shah">
         Changed the Servlet 3.0 auto-initializer so that it does nothing in a Servlet 2.5
or older application. This
         ensures behavioral consistency across containers.



Mime
View raw message