From fe5fe48488b9672380994ae743e519f6a68118bf Mon Sep 17 00:00:00 2001 From: Matt Sicker Date: Sat, 18 Jan 2014 19:20:32 -0600 Subject: [PATCH 2/2] Add async support to Log4jServletFilter. --- .../core/web/Log4jServletContainerInitializer.java | 7 ++- .../core/web/Log4jServletContextListener.java | 2 +- .../logging/log4j/core/web/Log4jServletFilter.java | 4 +- .../log4j/core/web/Log4jWebInitializer.java | 8 +-- .../log4j/core/web/Log4jWebInitializerImpl.java | 48 ++++++---------- .../log4j/core/web/Log4jWebInitializers.java | 50 ++++++++++++++++ .../web/Log4jServletContainerInitializerTest.java | 8 ++- .../core/web/Log4jServletContextListenerTest.java | 4 +- .../log4j/core/web/Log4jServletFilterTest.java | 6 +- .../core/web/Log4jWebInitializerImplTest.java | 52 ++++++++++++++--- .../logging/log4j/core/web/WebLookupTest.java | 66 ++++++++++++---------- 11 files changed, 166 insertions(+), 89 deletions(-) create mode 100644 log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializers.java diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializer.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializer.java index 50b0820..d9dafa7 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializer.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializer.java @@ -28,7 +28,9 @@ import javax.servlet.UnavailableException; /** * 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 - * version of the application is less than 3.0, this initializer does nothing. + * version of the application is less than 3.0, this initializer does nothing. Note that this means if your + * {@code web.xml} specifies its version as 2.x, then your effective Servlet major version will be 2 regardless of the + * actual version of the Servlet spec your Servlet container implements. */ public class Log4jServletContainerInitializer implements ServletContainerInitializer { @@ -37,7 +39,7 @@ public class Log4jServletContainerInitializer implements ServletContainerInitial if (servletContext.getMajorVersion() > 2) { servletContext.log("Log4jServletContainerInitializer starting up Log4j in Servlet 3.0+ environment."); - final Log4jWebInitializer initializer = Log4jWebInitializerImpl.getLog4jWebInitializer(servletContext); + final Log4jWebInitializer initializer = Log4jWebInitializers.getInitializer(servletContext); initializer.initialize(); initializer.setLoggerContext(); // the application is just now starting to start up @@ -49,6 +51,7 @@ public class Log4jServletContainerInitializer implements ServletContainerInitial 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.setAsyncSupported(true); filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*"); } } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContextListener.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContextListener.java index 9a8bd24..ee9f2a2 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContextListener.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletContextListener.java @@ -36,7 +36,7 @@ public class Log4jServletContextListener implements ServletContextListener { this.servletContext = event.getServletContext(); this.servletContext.log("Log4jServletContextListener ensuring that Log4j starts up properly."); - this.initializer = Log4jWebInitializerImpl.getLog4jWebInitializer(this.servletContext); + this.initializer = Log4jWebInitializers.getInitializer(this.servletContext); try { this.initializer.initialize(); this.initializer.setLoggerContext(); // the application is just now starting to start up diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletFilter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletFilter.java index a15b6ba..a9124dc 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletFilter.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jServletFilter.java @@ -16,7 +16,6 @@ */ package org.apache.logging.log4j.core.web; -import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; @@ -24,6 +23,7 @@ import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; +import java.io.IOException; /** * This is responsible for the following: @@ -48,7 +48,7 @@ public class Log4jServletFilter implements Filter { this.servletContext = filterConfig.getServletContext(); this.servletContext.log("Log4jServletFilter initialized."); - this.initializer = Log4jWebInitializerImpl.getLog4jWebInitializer(this.servletContext); + this.initializer = Log4jWebInitializers.getInitializer(this.servletContext); this.initializer.clearLoggerContext(); // the application is mostly finished starting up now } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializer.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializer.java index 231ad46..3a07257 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializer.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializer.java @@ -21,6 +21,8 @@ import javax.servlet.UnavailableException; /** * Specifies an interface for initializing and deinitializing Log4j in a Java EE web application. The default and only * implementation is {@link Log4jWebInitializerImpl}. The initializer is based on an interface to improve testability. + * + * @see org.apache.logging.log4j.core.web.Log4jWebInitializers#getInitializer(javax.servlet.ServletContext) Factory */ interface Log4jWebInitializer { /** @@ -40,12 +42,6 @@ interface Log4jWebInitializer { String IS_LOG4J_CONTEXT_SELECTOR_NAMED = "isLog4jContextSelectorNamed"; /** - * The attribute key for the {@link javax.servlet.ServletContext} attribute that the singleton initializer instance - * is stored in. - */ - String INITIALIZER_ATTRIBUTE = Log4jWebInitializer.class.getName() + ".INSTANCE"; - - /** * Starts up Log4j in the web application. Calls {@link #setLoggerContext()} after initialization is complete. * * @throws UnavailableException if a JNDI config location is specified but no name is specified. diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java index 78708dc..0815156 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java @@ -19,6 +19,7 @@ package org.apache.logging.log4j.core.web; import java.net.URI; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; import javax.servlet.ServletContext; import javax.servlet.UnavailableException; @@ -38,7 +39,6 @@ import org.apache.logging.log4j.spi.LoggerContextFactory; * This class initializes and deinitializes Log4j no matter how the initialization occurs. */ final class Log4jWebInitializerImpl implements Log4jWebInitializer { - private static final Object MUTEX = new Object(); static { try { @@ -59,33 +59,35 @@ final class Log4jWebInitializerImpl implements Log4jWebInitializer { private NamedContextSelector selector; private LoggerContext loggerContext; - private boolean initialized = false; - private boolean deinitialized = false; + private final AtomicBoolean initialized = new AtomicBoolean(false); + private final AtomicBoolean deinitialized = new AtomicBoolean(false); - private Log4jWebInitializerImpl(final ServletContext servletContext) { + // constructor is default access to allow factory class to have access + Log4jWebInitializerImpl(final ServletContext servletContext) { this.servletContext = servletContext; map.put("hostName", NetUtils.getLocalHostname()); } @Override - public synchronized void initialize() throws UnavailableException { - if (this.deinitialized) { + public void initialize() throws UnavailableException { + if (this.deinitialized.get()) { throw new IllegalStateException("Cannot initialize Log4jWebInitializer after it was destroyed."); } // only do this once - if (!this.initialized) { - this.initialized = true; + if (this.initialized.compareAndSet(false, true)) { this.name = this.substitutor.replace(this.servletContext.getInitParameter(LOG4J_CONTEXT_NAME)); final String location = this.substitutor.replace(this.servletContext.getInitParameter(LOG4J_CONFIG_LOCATION)); - final boolean isJndi = "true".equals(this.servletContext.getInitParameter(IS_LOG4J_CONTEXT_SELECTOR_NAMED)); - if (isJndi) { + if (Boolean.parseBoolean(this.servletContext.getInitParameter(IS_LOG4J_CONTEXT_SELECTOR_NAMED))) { this.initializeJndi(location); } else { this.initializeNonJndi(location); } + this.servletContext.setAttribute(LoggerContext.LOGGER_CONTEXT_ATTRIBUTE, this.loggerContext); + this.servletContext.log("Stored logger context for [" + this.name + "] into servlet context attribute [" + + LoggerContext.LOGGER_CONTEXT_ATTRIBUTE + "]."); } } @@ -142,14 +144,13 @@ final class Log4jWebInitializerImpl implements Log4jWebInitializer { } @Override - public synchronized void deinitialize() { - if (!this.initialized) { + public void deinitialize() { + if (!this.initialized.get()) { throw new IllegalStateException("Cannot deinitialize Log4jWebInitializer because it has not initialized."); } // only do this once - if (!this.deinitialized) { - this.deinitialized = true; + if (this.deinitialized.compareAndSet(false, true)) { if (this.loggerContext != null) { this.servletContext.log("Removing LoggerContext for [" + this.name + "]."); @@ -159,6 +160,7 @@ final class Log4jWebInitializerImpl implements Log4jWebInitializer { this.loggerContext.stop(); this.loggerContext.setExternalContext(null); this.loggerContext = null; + this.servletContext.removeAttribute(LoggerContext.LOGGER_CONTEXT_ATTRIBUTE); } } } @@ -186,22 +188,4 @@ final class Log4jWebInitializerImpl implements Log4jWebInitializer { return Log4jWebInitializerImpl.class.getClassLoader(); } } - - /** - * Get the current initializer from the {@link ServletContext}. If the initializer does not exist, create a new one - * and add it to the {@link ServletContext}, then return that. - * - * @param servletContext The {@link ServletContext} for this web application - * @return the initializer, never {@code null}. - */ - static Log4jWebInitializer getLog4jWebInitializer(final ServletContext servletContext) { - synchronized (MUTEX) { - Log4jWebInitializer initializer = (Log4jWebInitializer) servletContext.getAttribute(INITIALIZER_ATTRIBUTE); - if (initializer == null) { - initializer = new Log4jWebInitializerImpl(servletContext); - servletContext.setAttribute(INITIALIZER_ATTRIBUTE, initializer); - } - return initializer; - } - } } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializers.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializers.java new file mode 100644 index 0000000..2628fe5 --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializers.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.core.web; + +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; +import javax.servlet.ServletContext; + +class Log4jWebInitializers { + private Log4jWebInitializers() { + } + + private static final Lock LOCK = new ReentrantLock(); + public static final String INITIALIZER_ATTRIBUTE = Log4jWebInitializers.class.getName() + ".INSTANCE"; + + /** + * Get the current initializer for the given {@link javax.servlet.ServletContext} object. If no initializer has + * been created yet, one will be created and stored to the servlet context attributes. + * + * @param context The current servlet context for this web application. + * @return the web initializer for the current application. + */ + public static Log4jWebInitializer getInitializer(final ServletContext context) { + LOCK.lock(); + try { + Log4jWebInitializer initializer = (Log4jWebInitializer) context.getAttribute(INITIALIZER_ATTRIBUTE); + if (initializer == null) { + initializer = new Log4jWebInitializerImpl(context); + context.setAttribute(INITIALIZER_ATTRIBUTE, initializer); + } + return initializer; + } finally { + LOCK.unlock(); + } + } +} diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializerTest.java index 2553e0d..56333e4 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializerTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContainerInitializerTest.java @@ -70,7 +70,7 @@ public class Log4jServletContainerInitializerTest { expect(this.servletContext.getMajorVersion()).andReturn(3); this.servletContext.log(anyObject(String.class)); expectLastCall(); - expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer); + expect(this.servletContext.getAttribute(Log4jWebInitializers.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer); this.initializer.initialize(); expectLastCall(); this.initializer.setLoggerContext(); @@ -78,6 +78,8 @@ public class Log4jServletContainerInitializerTest { this.servletContext.addListener(capture(listenerCapture)); expectLastCall(); expect(this.servletContext.addFilter(eq("log4jServletFilter"), capture(filterCapture))).andReturn(registration); + registration.setAsyncSupported(eq(true)); + expectLastCall(); registration.addMappingForUrlPatterns(eq(EnumSet.allOf(DispatcherType.class)), eq(false), eq("/*")); expectLastCall(); @@ -96,7 +98,7 @@ public class Log4jServletContainerInitializerTest { expect(this.servletContext.getMajorVersion()).andReturn(3); this.servletContext.log(anyObject(String.class)); expectLastCall(); - expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer); + expect(this.servletContext.getAttribute(Log4jWebInitializers.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer); this.initializer.initialize(); expectLastCall(); this.initializer.setLoggerContext(); @@ -125,7 +127,7 @@ public class Log4jServletContainerInitializerTest { expect(this.servletContext.getMajorVersion()).andReturn(3); this.servletContext.log(anyObject(String.class)); expectLastCall(); - expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer); + expect(this.servletContext.getAttribute(Log4jWebInitializers.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer); this.initializer.initialize(); expectLastCall().andThrow(exception); diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContextListenerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContextListenerTest.java index 8271da2..fad864b 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContextListenerTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletContextListenerTest.java @@ -54,7 +54,7 @@ public class Log4jServletContextListenerTest { expect(this.event.getServletContext()).andReturn(this.servletContext); this.servletContext.log(anyObject(String.class)); expectLastCall(); - expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer); + expect(this.servletContext.getAttribute(Log4jWebInitializers.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer); this.initializer.initialize(); expectLastCall(); this.initializer.setLoggerContext(); @@ -84,7 +84,7 @@ public class Log4jServletContextListenerTest { expect(this.event.getServletContext()).andReturn(this.servletContext); this.servletContext.log(anyObject(String.class)); expectLastCall(); - expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer); + expect(this.servletContext.getAttribute(Log4jWebInitializers.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer); this.initializer.initialize(); expectLastCall().andThrow(new UnavailableException("")); diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletFilterTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletFilterTest.java index 95fbea9..de0bdfe 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletFilterTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jServletFilterTest.java @@ -54,7 +54,7 @@ public class Log4jServletFilterTest { expect(this.filterConfig.getServletContext()).andReturn(this.servletContext); this.servletContext.log(anyObject(String.class)); expectLastCall(); - expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer); + expect(this.servletContext.getAttribute(Log4jWebInitializers.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer); this.initializer.clearLoggerContext(); expectLastCall(); @@ -87,7 +87,7 @@ public class Log4jServletFilterTest { expect(this.filterConfig.getServletContext()).andReturn(this.servletContext); this.servletContext.log(anyObject(String.class)); expectLastCall(); - expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer); + expect(this.servletContext.getAttribute(Log4jWebInitializers.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer); this.initializer.clearLoggerContext(); expectLastCall(); @@ -124,7 +124,7 @@ public class Log4jServletFilterTest { expect(this.filterConfig.getServletContext()).andReturn(this.servletContext); this.servletContext.log(anyObject(String.class)); expectLastCall(); - expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer); + expect(this.servletContext.getAttribute(Log4jWebInitializers.INITIALIZER_ATTRIBUTE)).andReturn(this.initializer); this.initializer.clearLoggerContext(); expectLastCall(); diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImplTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImplTest.java index b9e64c0..83ff991 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImplTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImplTest.java @@ -39,13 +39,13 @@ public class Log4jWebInitializerImplTest { final Capture initializerCapture = new Capture(); this.servletContext = createStrictMock(ServletContext.class); - expect(this.servletContext.getAttribute(Log4jWebInitializer.INITIALIZER_ATTRIBUTE)).andReturn(null); - this.servletContext.setAttribute(eq(Log4jWebInitializer.INITIALIZER_ATTRIBUTE), capture(initializerCapture)); + expect(this.servletContext.getAttribute(Log4jWebInitializers.INITIALIZER_ATTRIBUTE)).andReturn(null); + this.servletContext.setAttribute(eq(Log4jWebInitializers.INITIALIZER_ATTRIBUTE), capture(initializerCapture)); expectLastCall(); replay(this.servletContext); - final Log4jWebInitializer initializer = Log4jWebInitializerImpl.getLog4jWebInitializer(this.servletContext); + final Log4jWebInitializer initializer = Log4jWebInitializers.getInitializer(this.servletContext); assertNotNull("The initializer should not be null.", initializer); assertSame("The capture is not correct.", initializer, initializerCapture.getValue()); @@ -102,6 +102,10 @@ public class Log4jWebInitializerImplTest { expect(this.servletContext.getInitParameter(Log4jWebInitializer.IS_LOG4J_CONTEXT_SELECTOR_NAMED)) .andReturn(null); expect(this.servletContext.getServletContextName()).andReturn("helloWorld01"); + this.servletContext.setAttribute(eq(LoggerContext.LOGGER_CONTEXT_ATTRIBUTE), anyObject(LoggerContext.class)); + expectLastCall(); + this.servletContext.log(anyString()); + expectLastCall(); replay(this.servletContext); @@ -127,7 +131,9 @@ public class Log4jWebInitializerImplTest { verify(this.servletContext); reset(this.servletContext); - this.servletContext.log(anyObject(String.class)); + this.servletContext.log(anyString()); + expectLastCall(); + this.servletContext.removeAttribute(eq(LoggerContext.LOGGER_CONTEXT_ATTRIBUTE)); expectLastCall(); replay(this.servletContext); @@ -153,6 +159,10 @@ public class Log4jWebInitializerImplTest { .andReturn("false"); expect(this.servletContext.getServletContextName()).andReturn("helloWorld02"); expect(this.servletContext.getClassLoader()).andReturn(this.getClass().getClassLoader()); + this.servletContext.setAttribute(eq(LoggerContext.LOGGER_CONTEXT_ATTRIBUTE), anyObject(LoggerContext.class)); + expectLastCall(); + this.servletContext.log(anyString()); + expectLastCall(); replay(this.servletContext); @@ -178,7 +188,9 @@ public class Log4jWebInitializerImplTest { verify(this.servletContext); reset(this.servletContext); - this.servletContext.log(anyObject(String.class)); + this.servletContext.log(anyString()); + expectLastCall(); + this.servletContext.removeAttribute(eq(LoggerContext.LOGGER_CONTEXT_ATTRIBUTE)); expectLastCall(); replay(this.servletContext); @@ -204,6 +216,10 @@ public class Log4jWebInitializerImplTest { .andReturn("nothing"); expect(this.servletContext.getServletContextName()).andReturn("helloWorld03"); expect(this.servletContext.getClassLoader()).andReturn(this.getClass().getClassLoader()); + this.servletContext.setAttribute(eq(LoggerContext.LOGGER_CONTEXT_ATTRIBUTE), anyObject(LoggerContext.class)); + expectLastCall(); + this.servletContext.log(anyString()); + expectLastCall(); replay(this.servletContext); @@ -222,7 +238,9 @@ public class Log4jWebInitializerImplTest { verify(this.servletContext); reset(this.servletContext); - this.servletContext.log(anyObject(String.class)); + this.servletContext.log(anyString()); + expectLastCall(); + this.servletContext.removeAttribute(eq(LoggerContext.LOGGER_CONTEXT_ATTRIBUTE)); expectLastCall(); replay(this.servletContext); @@ -238,6 +256,10 @@ public class Log4jWebInitializerImplTest { .andReturn(null); expect(this.servletContext.getServletContextName()).andReturn("helloWorld04"); expect(this.servletContext.getClassLoader()).andReturn(this.getClass().getClassLoader()); + this.servletContext.setAttribute(eq(LoggerContext.LOGGER_CONTEXT_ATTRIBUTE), anyObject(LoggerContext.class)); + expectLastCall(); + this.servletContext.log(anyString()); + expectLastCall(); replay(this.servletContext); @@ -248,7 +270,9 @@ public class Log4jWebInitializerImplTest { verify(this.servletContext); reset(this.servletContext); - this.servletContext.log(anyObject(String.class)); + this.servletContext.log(anyString()); + expectLastCall(); + this.servletContext.removeAttribute(eq(LoggerContext.LOGGER_CONTEXT_ATTRIBUTE)); expectLastCall(); replay(this.servletContext); @@ -271,6 +295,10 @@ public class Log4jWebInitializerImplTest { .andReturn(null); expect(this.servletContext.getServletContextName()).andReturn("helloWorld05"); expect(this.servletContext.getClassLoader()).andReturn(this.getClass().getClassLoader()); + this.servletContext.setAttribute(eq(LoggerContext.LOGGER_CONTEXT_ATTRIBUTE), anyObject(LoggerContext.class)); + expectLastCall(); + this.servletContext.log(anyString()); + expectLastCall(); replay(this.servletContext); @@ -281,7 +309,9 @@ public class Log4jWebInitializerImplTest { verify(this.servletContext); reset(this.servletContext); - this.servletContext.log(anyObject(String.class)); + this.servletContext.log(anyString()); + expectLastCall(); + this.servletContext.removeAttribute(eq(LoggerContext.LOGGER_CONTEXT_ATTRIBUTE)); expectLastCall(); replay(this.servletContext); @@ -316,7 +346,11 @@ public class Log4jWebInitializerImplTest { expect(this.servletContext.getInitParameter(Log4jWebInitializer.LOG4J_CONFIG_LOCATION)).andReturn(null); expect(this.servletContext.getInitParameter(Log4jWebInitializer.IS_LOG4J_CONTEXT_SELECTOR_NAMED)) .andReturn("true"); - this.servletContext.log(anyObject(String.class)); + this.servletContext.log(anyString()); + expectLastCall(); + this.servletContext.setAttribute(eq(LoggerContext.LOGGER_CONTEXT_ATTRIBUTE), anyObject(LoggerContext.class)); + expectLastCall(); + this.servletContext.log(anyString()); expectLastCall(); replay(this.servletContext); diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/web/WebLookupTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/web/WebLookupTest.java index 5abbf34..201110a 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/web/WebLookupTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/web/WebLookupTest.java @@ -16,6 +16,10 @@ */ package org.apache.logging.log4j.core.web; +import java.util.Map; +import javax.servlet.ServletContext; +import javax.servlet.UnavailableException; + import org.apache.logging.log4j.core.Appender; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.appender.FileAppender; @@ -25,30 +29,40 @@ import org.apache.logging.log4j.core.lookup.StrSubstitutor; import org.junit.After; import org.junit.Before; import org.junit.Test; - -import javax.servlet.ServletContext; -import javax.servlet.UnavailableException; - import org.springframework.mock.web.MockServletContext; -import java.util.Map; - import static org.junit.Assert.*; public class WebLookupTest { + private ServletContext servletContext; + private Log4jWebInitializer initializer; + + @Before + public void setUp() throws Exception { + ContextAnchor.THREAD_CONTEXT.remove(); + this.servletContext = new MockServletContext(); + } + + @After + public void tearDown() throws Exception { + if (this.initializer != null) { + this.initializer.deinitialize(); + this.initializer = null; + } + ContextAnchor.THREAD_CONTEXT.remove(); + } + @Test public void testLookup() throws Exception { - ContextAnchor.THREAD_CONTEXT.remove(); - ServletContext servletContext = new MockServletContext(); - servletContext.setAttribute("TestAttr", "AttrValue"); - servletContext.setInitParameter("TestParam", "ParamValue"); - servletContext.setAttribute("Name1", "Ben"); - servletContext.setInitParameter("Name2", "Jerry"); - Log4jWebInitializer initializer = Log4jWebInitializerImpl.getLog4jWebInitializer(servletContext); + this.servletContext.setAttribute("TestAttr", "AttrValue"); + this.servletContext.setInitParameter("TestParam", "ParamValue"); + this.servletContext.setAttribute("Name1", "Ben"); + this.servletContext.setInitParameter("Name2", "Jerry"); + this.initializer = Log4jWebInitializers.getInitializer(this.servletContext); try { - initializer.initialize(); - initializer.setLoggerContext(); + this.initializer.initialize(); + this.initializer.setLoggerContext(); LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get(); assertNotNull("No LoggerContext", ctx); assertNotNull("No ServletContext", ctx.getExternalContext()); @@ -71,22 +85,18 @@ public class WebLookupTest { } catch (final UnavailableException e) { fail("Failed to initialize Log4j properly." + e.getMessage()); } - initializer.deinitialize(); - ContextAnchor.THREAD_CONTEXT.remove(); } @Test public void testLookup2() throws Exception { - ContextAnchor.THREAD_CONTEXT.remove(); - ServletContext servletContext = new MockServletContext(); - servletContext.setAttribute("TestAttr", "AttrValue"); - servletContext.setInitParameter("myapp.logdir", "target"); - servletContext.setAttribute("Name1", "Ben"); - servletContext.setInitParameter("Name2", "Jerry"); - servletContext.setInitParameter("log4jConfiguration", "log4j-webvar.xml"); - Log4jWebInitializer initializer = Log4jWebInitializerImpl.getLog4jWebInitializer(servletContext); - initializer.initialize(); - initializer.setLoggerContext(); + this.servletContext.setAttribute("TestAttr", "AttrValue"); + this.servletContext.setInitParameter("myapp.logdir", "target"); + this.servletContext.setAttribute("Name1", "Ben"); + this.servletContext.setInitParameter("Name2", "Jerry"); + this.servletContext.setInitParameter("log4jConfiguration", "log4j-webvar.xml"); + this.initializer = Log4jWebInitializers.getInitializer(this.servletContext); + this.initializer.initialize(); + this.initializer.setLoggerContext(); LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get(); assertNotNull("No LoggerContext", ctx); assertNotNull("No ServletContext", ctx.getExternalContext()); @@ -99,8 +109,6 @@ public class WebLookupTest { assertTrue("target/myapp.log".equals(fa.getFileName())); } } - initializer.deinitialize(); - ContextAnchor.THREAD_CONTEXT.remove(); } } -- 1.8.5.3