logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nickwilli...@apache.org
Subject svn commit: r1561933 - in /logging/log4j/log4j2/trunk: log4j-core/src/main/java/org/apache/logging/log4j/core/web/ log4j-core/src/test/java/org/apache/logging/log4j/core/web/ src/changes/
Date Tue, 28 Jan 2014 04:32:07 GMT
Author: nickwilliams
Date: Tue Jan 28 04:32:07 2014
New Revision: 1561933

URL: http://svn.apache.org/r1561933
Log:
Additional fixes for LOG4J2-359 to abort initialization if a duplicate filter already exists
and to check the actual Servlet EFFECTIVE version.

Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializer.java
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializerTest.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializer.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializer.java?rev=1561933&r1=1561932&r2=1561933&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializer.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializer.java
Tue Jan 28 04:32:07 2014
@@ -25,6 +25,9 @@ import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.UnavailableException;
 
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.status.StatusLogger;
+
 /**
  * In a Servlet 3.0 or newer environment, this initializer is responsible for starting up
Log4j logging before anything
  * else happens in application initialization. For consistency across all containers, if
the effective Servlet major
@@ -34,21 +37,23 @@ public class Log4jServletContainerInitia
 
     @Override
     public void onStartup(final Set<Class<?>> classes, final ServletContext servletContext)
throws ServletException {
-        if (servletContext.getMajorVersion() > 2) {
+        if (servletContext.getMajorVersion() > 2 && servletContext.getEffectiveMajorVersion()
> 2) {
             servletContext.log("Log4jServletContainerInitializer starting up Log4j in Servlet
3.0+ environment.");
 
+            final FilterRegistration.Dynamic filter =
+                    servletContext.addFilter("log4jServletFilter", new Log4jServletFilter());
+            if (filter == null) {
+                servletContext.log("WARNING: In a Servlet 3.0+ application, you should not
define a " +
+                        "log4jServletFilter in web.xml. Log4j 2 normally does this for you
automatically. Log4j 2 " +
+                        "web auto-initialization has been canceled.");
+                return;
+            }
+
             final Log4jWebInitializer initializer = Log4jWebInitializerImpl.getLog4jWebInitializer(servletContext);
             initializer.initialize();
             initializer.setLoggerContext(); // the application is just now starting to start
up
 
             servletContext.addListener(new Log4jServletContextListener());
-
-            final FilterRegistration.Dynamic filter =
-                    servletContext.addFilter("log4jServletFilter", new Log4jServletFilter());
-            if (filter == null) {
-                throw new UnavailableException("In a Servlet 3.0+ application, you must not
define a " +
-                        "log4jServletFilter in web.xml. Log4j 2 defines this for you automatically.");
-            }
             filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
         }
     }

Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializerTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializerTest.java?rev=1561933&r1=1561932&r2=1561933&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializerTest.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializerTest.java
Tue Jan 28 04:32:07 2014
@@ -61,15 +61,27 @@ public class Log4jServletContainerInitia
     }
 
     @Test
-    public void testOnStartupWithServletVersion3_x() throws Exception {
+    public void testOnStartupWithServletVersion3_xEffectiveVersion2_x() throws Exception
{
+        expect(this.servletContext.getMajorVersion()).andReturn(3);
+        expect(this.servletContext.getEffectiveMajorVersion()).andReturn(2);
+
+        replay(this.servletContext, this.initializer);
+
+        this.containerInitializer.onStartup(null, this.servletContext);
+    }
+
+    @Test
+    public void testOnStartupWithServletVersion3_xEffectiveVersion3_x() throws Exception
{
         final FilterRegistration.Dynamic registration = createStrictMock(FilterRegistration.Dynamic.class);
 
         final Capture<EventListener> listenerCapture = new Capture<EventListener>();
         final Capture<Filter> filterCapture = new Capture<Filter>();
 
         expect(this.servletContext.getMajorVersion()).andReturn(3);
+        expect(this.servletContext.getEffectiveMajorVersion()).andReturn(3);
         this.servletContext.log(anyObject(String.class));
         expectLastCall();
+        expect(this.servletContext.addFilter(eq("log4jServletFilter"), capture(filterCapture))).andReturn(registration);
         expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer);
         this.initializer.initialize();
         expectLastCall();
@@ -77,7 +89,6 @@ public class Log4jServletContainerInitia
         expectLastCall();
         this.servletContext.addListener(capture(listenerCapture));
         expectLastCall();
-        expect(this.servletContext.addFilter(eq("log4jServletFilter"), capture(filterCapture))).andReturn(registration);
         registration.addMappingForUrlPatterns(eq(EnumSet.allOf(DispatcherType.class)), eq(false),
eq("/*"));
         expectLastCall();
 
@@ -85,51 +96,57 @@ public class Log4jServletContainerInitia
 
         this.containerInitializer.onStartup(null, this.servletContext);
 
+        assertNotNull("The listener should not be null.", listenerCapture.getValue());
+        assertSame("The listener is not correct.", Log4jServletContextListener.class,
+                listenerCapture.getValue().getClass());
+
+        assertNotNull("The filter should not be null.", filterCapture.getValue());
+        assertSame("The filter is not correct.", Log4jServletFilter.class, filterCapture.getValue().getClass());
+
         verify(registration);
     }
 
     @Test
-    public void testOnStartupFailedDueToPreExistingFilter() throws Exception {
-        final Capture<EventListener> listenerCapture = new Capture<EventListener>();
+    public void testOnStartupCanceledDueToPreExistingFilter() throws Exception {
         final Capture<Filter> filterCapture = new Capture<Filter>();
+        final Capture<String> logCapture = new Capture<String>();
 
         expect(this.servletContext.getMajorVersion()).andReturn(3);
+        expect(this.servletContext.getEffectiveMajorVersion()).andReturn(3);
         this.servletContext.log(anyObject(String.class));
         expectLastCall();
-        expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer);
-        this.initializer.initialize();
-        expectLastCall();
-        this.initializer.setLoggerContext();
-        expectLastCall();
-        this.servletContext.addListener(capture(listenerCapture));
-        expectLastCall();
         expect(this.servletContext.addFilter(eq("log4jServletFilter"), capture(filterCapture))).andReturn(null);
+        this.servletContext.log(capture(logCapture));
 
         replay(this.servletContext, this.initializer);
 
-        try {
-            this.containerInitializer.onStartup(null, this.servletContext);
-            fail("Expected an UnavailableException, got no exception.");
-        } catch (final UnavailableException e) {
-            assertEquals("The exception is not correct.",
-                    "In a Servlet 3.0+ application, you must not define a log4jServletFilter
in web.xml. Log4j 2 " +
-                            "defines this for you automatically.",
-                    e.getMessage());
-        }
+        this.containerInitializer.onStartup(null, this.servletContext);
+
+        assertNotNull("The filter should not be null.", filterCapture.getValue());
+        assertSame("The filter is not correct.", Log4jServletFilter.class, filterCapture.getValue().getClass());
+
+        assertNotNull("The second log message should not be null.", logCapture.getValue());
+        assertTrue("The second log message (" + logCapture.getValue() + ") is not correct.",
+                logCapture.getValue().startsWith("WARNING: "));
     }
 
     @Test
     public void testOnStartupFailedDueToInitializerFailure() throws Exception {
+        final FilterRegistration.Dynamic registration = createStrictMock(FilterRegistration.Dynamic.class);
+
+        final Capture<Filter> filterCapture = new Capture<Filter>();
         final UnavailableException exception = new UnavailableException("");
 
         expect(this.servletContext.getMajorVersion()).andReturn(3);
+        expect(this.servletContext.getEffectiveMajorVersion()).andReturn(3);
         this.servletContext.log(anyObject(String.class));
         expectLastCall();
+        expect(this.servletContext.addFilter(eq("log4jServletFilter"), capture(filterCapture))).andReturn(registration);
         expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer);
         this.initializer.initialize();
         expectLastCall().andThrow(exception);
 
-        replay(this.servletContext, this.initializer);
+        replay(this.servletContext, this.initializer, registration);
 
         try {
             this.containerInitializer.onStartup(null, this.servletContext);
@@ -137,5 +154,10 @@ public class Log4jServletContainerInitia
         } catch (final UnavailableException e) {
             assertSame("The exception is not correct.", exception, e);
         }
+
+        assertNotNull("The filter should not be null.", filterCapture.getValue());
+        assertSame("The filter is not correct.", Log4jServletFilter.class, filterCapture.getValue().getClass());
+
+        verify(registration);
     }
 }

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1561933&r1=1561932&r2=1561933&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Tue Jan 28 04:32:07 2014
@@ -158,6 +158,11 @@
       <action issue="LOG4J2-402" dev="ggregory" type="add">
         Configure RandomAccessFileAppender buffer size.
       </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. This includes additional fixes
to abort initialization if a
+        duplicate filter already exists and to check the actual Servlet EFFECTIVE version.
+      </action>
     </release>
     <release version="2.0-beta9" date="2013-09-14" description="Bug fixes and enhancements">
       <action issue="LOG4J2-317" dev="ggregory" type="update">



Mime
View raw message