Author: rgoers
Date: Sun Oct 23 17:04:35 2011
New Revision: 1187931

URL: http://svn.apache.org/viewvc?rev=1187931&view=rev
Log:
Prevent endless recursion when a configuration component uses a Logger

Added:
    
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/LogEventFactory.java
      - copied, changed from r1165682, 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LogEventFactory.java
Removed:
    
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LogEventFactory.java
    
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/slf4j-impl/src/main/java/org/apache/logging/log4j/
Modified:
    
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
    
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java
    
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java
    
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/javaee/ContextListener.java
    
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/javaee/JNDIContextFilter.java
    
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/slf4j-impl/src/test/resources/log4j-test1.xml

Modified: 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java?rev=1187931&r1=1187930&r2=1187931&view=diff
==============================================================================
--- 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
 (original)
+++ 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
 Sun Oct 23 17:04:35 2011
@@ -19,6 +19,7 @@ package org.apache.logging.log4j.core;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.ConfigurationFactory;
 import org.apache.logging.log4j.core.config.ConfigurationListener;
+import org.apache.logging.log4j.core.config.DefaultConfiguration;
 import org.apache.logging.log4j.core.config.NullConfiguration;
 import org.apache.logging.log4j.status.StatusLogger;
 
@@ -32,7 +33,7 @@ import java.util.concurrent.ConcurrentMa
  * applications and a reference to the Configuration. The Configuration will 
contain the configured loggers, appenders,
  * filters, etc and will be atomically updated whenever a reconfigure occurs.
  */
-public class LoggerContext implements 
org.apache.logging.log4j.spi.LoggerContext, ConfigurationListener {
+public class LoggerContext implements 
org.apache.logging.log4j.spi.LoggerContext, ConfigurationListener, Lifecycle {
 
     private static StatusLogger logger = StatusLogger.getLogger();
 
@@ -42,7 +43,7 @@ public class LoggerContext implements or
      * The Configuration is volatile to guarantee that initialization of the 
Configuration has completed before
      * the reference is updated.
      */
-    private volatile Configuration config;
+    private volatile Configuration config = new DefaultConfiguration();
 
     private Object externalContext = null;
 
@@ -50,6 +51,8 @@ public class LoggerContext implements or
 
     private final URI configLocation;
 
+    private boolean isStarted;
+
     /**
      * Constructor taking only a name.
      * @param name The context name.
@@ -77,7 +80,6 @@ public class LoggerContext implements or
         contextName = name;
         this.externalContext = externalContext;
         this.configLocation = configLocn;
-        reconfigure();
     }
 
     /**
@@ -101,7 +103,22 @@ public class LoggerContext implements or
         } else {
             configLocation = null;
         }
+    }
+
+    public void start() {
         reconfigure();
+        isStarted = true;
+    }
+
+    public synchronized void stop() {
+        isStarted = false;
+        updateLoggers(new NullConfiguration());
+        config.stop();
+        externalContext = null;
+    }
+
+    public boolean isStarted() {
+        return isStarted;
     }
 
     /**
@@ -172,15 +189,6 @@ public class LoggerContext implements or
     }
 
     /**
-     * Shutdown the logging system.
-     */
-    public synchronized void shutdown() {
-        updateLoggers(new NullConfiguration());
-        config.stop();
-        externalContext = null;
-    }
-
-    /**
      * Set the Configuration to be used.
      * @param config The new Configuration.
      * @return The previous Configuration.

Modified: 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java?rev=1187931&r1=1187930&r2=1187931&view=diff
==============================================================================
--- 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java
 (original)
+++ 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java
 Sun Oct 23 17:04:35 2011
@@ -23,7 +23,7 @@ import org.apache.logging.log4j.Marker;
 import org.apache.logging.log4j.core.Appender;
 import org.apache.logging.log4j.core.impl.Log4jLogEvent;
 import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.LogEventFactory;
+import org.apache.logging.log4j.core.impl.LogEventFactory;
 import org.apache.logging.log4j.core.config.plugins.Plugin;
 import org.apache.logging.log4j.core.config.plugins.PluginAttr;
 import org.apache.logging.log4j.core.config.plugins.PluginFactory;

Modified: 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java?rev=1187931&r1=1187930&r2=1187931&view=diff
==============================================================================
--- 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java
 (original)
+++ 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java
 Sun Oct 23 17:04:35 2011
@@ -17,6 +17,7 @@
 package org.apache.logging.log4j.core.impl;
 
 import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.DefaultConfiguration;
 import org.apache.logging.log4j.core.helpers.Constants;
 import org.apache.logging.log4j.core.helpers.Loader;
 import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector;
@@ -33,6 +34,8 @@ public class Log4jContextFactory impleme
 
     private StatusLogger logger = StatusLogger.getLogger();
 
+    private ThreadLocal<Log4jContextFactory> recursive = new 
ThreadLocal<Log4jContextFactory>();
+
     public Log4jContextFactory() {
         String sel = System.getProperty(Constants.LOG4J_CONTEXT_SELECTOR);
         if (sel != null) {
@@ -55,7 +58,18 @@ public class Log4jContextFactory impleme
     }
 
     public LoggerContext getContext(String fqcn, boolean currentContext) {
-        return selector.getContext(fqcn, currentContext);
-
+        LoggerContext ctx = selector.getContext(fqcn, currentContext);
+        synchronized (ctx) {
+            if (recursive.get() != null || ctx.isStarted()) {
+                return ctx;
+            }
+            try {
+                recursive.set(this);
+                ctx.start();
+                return ctx;
+            } finally {
+                recursive.remove();
+            }
+        }
     }
 }

Copied: 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/LogEventFactory.java
 (from r1165682, 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LogEventFactory.java)
URL: 
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/LogEventFactory.java?p2=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/LogEventFactory.java&p1=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LogEventFactory.java&r1=1165682&r2=1187931&rev=1187931&view=diff
==============================================================================
--- 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/LogEventFactory.java
 (original)
+++ 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/impl/LogEventFactory.java
 Sun Oct 23 17:04:35 2011
@@ -15,10 +15,11 @@
  * limitations under the license.
  */
 
-package org.apache.logging.log4j.core;
+package org.apache.logging.log4j.core.impl;
 
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.Marker;
+import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.message.Message;
 
 /**

Modified: 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/javaee/ContextListener.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/javaee/ContextListener.java?rev=1187931&r1=1187930&r2=1187931&view=diff
==============================================================================
--- 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/javaee/ContextListener.java
 (original)
+++ 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/javaee/ContextListener.java
 Sun Oct 23 17:04:35 2011
@@ -42,7 +42,7 @@ public class ContextListener implements 
         LoggerContext ctx = (LoggerContext) 
context.getAttribute(LOG4J_CONTEXT_ATTRIBUTE);
         if (ctx != null) {
             context.removeAttribute(LOG4J_CONTEXT_ATTRIBUTE);
-            ctx.shutdown();
+            ctx.stop();
         }
     }
 }

Modified: 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/javaee/JNDIContextFilter.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/javaee/JNDIContextFilter.java?rev=1187931&r1=1187930&r2=1187931&view=diff
==============================================================================
--- 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/javaee/JNDIContextFilter.java
 (original)
+++ 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/javaee/JNDIContextFilter.java
 Sun Oct 23 17:04:35 2011
@@ -96,7 +96,7 @@ public class JNDIContextFilter implement
             if (selector != null) {
                 selector.removeContext(name);
             }
-            ctx.shutdown();
+            ctx.stop();
         }
     }
 }

Modified: 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/slf4j-impl/src/test/resources/log4j-test1.xml
URL: 
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/slf4j-impl/src/test/resources/log4j-test1.xml?rev=1187931&r1=1187930&r2=1187931&view=diff
==============================================================================
--- 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/slf4j-impl/src/test/resources/log4j-test1.xml
 (original)
+++ 
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/slf4j-impl/src/test/resources/log4j-test1.xml
 Sun Oct 23 17:04:35 2011
@@ -22,6 +22,7 @@
     <List name="List">
       <PatternLayout pattern="%C{1.} %m MDC%X%n"/>
     </List>
+    <SLF4J name="SLF4J"/>
   </appenders>
 
   <loggers>


Reply via email to