View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.web;
18  
19  import java.util.concurrent.locks.Lock;
20  import java.util.concurrent.locks.ReentrantLock;
21  import javax.servlet.ServletContext;
22  
23  import org.apache.logging.log4j.LogManager;
24  import org.apache.logging.log4j.core.LoggerContext;
25  import org.apache.logging.log4j.core.impl.ContextAnchor;
26  
27  /**
28   * Convenience methods for retrieving the {@link org.apache.logging.log4j.core.LoggerContext} associated with a
29   * particular ServletContext. These methods are most particularly useful for asynchronous servlets where the
30   * Thread Context ClassLoader (TCCL) is potentially different from the TCCL used by the
31   * Servlet container that bootstrapped Log4j.
32   *
33   * @since 2.0.1
34   */
35  public final class WebLoggerContextUtils {
36      private WebLoggerContextUtils() {
37      }
38  
39      private static final Lock WEB_SUPPORT_LOOKUP = new ReentrantLock();
40  
41      /**
42       * Finds the main {@link org.apache.logging.log4j.core.LoggerContext} configured for the given ServletContext.
43       *
44       * @param servletContext the ServletContext to locate a LoggerContext for
45       * @return the LoggerContext for the given ServletContext
46       * @since 2.0.1
47       */
48      public static LoggerContext getWebLoggerContext(final ServletContext servletContext) {
49          return (LoggerContext) servletContext.getAttribute(Log4jWebSupport.CONTEXT_ATTRIBUTE);
50      }
51  
52      /**
53       * Finds the main {@link org.apache.logging.log4j.core.LoggerContext} configured for the given ServletContext.
54       *
55       * @param servletContext the ServletContext to locate a LoggerContext for
56       * @return the LoggerContext for the given ServletContext or {@code null} if none was set
57       * @throws java.lang.IllegalStateException if no LoggerContext could be found on the given ServletContext
58       * @since 2.0.1
59       */
60      public static LoggerContext getRequiredWebLoggerContext(final ServletContext servletContext) {
61          final LoggerContext loggerContext = getWebLoggerContext(servletContext);
62          if (loggerContext == null) {
63              throw new IllegalStateException(
64                  "No LoggerContext found in ServletContext attribute " + Log4jWebSupport.CONTEXT_ATTRIBUTE);
65          }
66          return loggerContext;
67      }
68  
69      /**
70       * Finds or initializes the {@link org.apache.logging.log4j.web.Log4jWebLifeCycle} singleton for the given
71       * ServletContext.
72       *
73       * @param servletContext the ServletContext to get the Log4jWebLifeCycle for
74       * @return the Log4jWebLifeCycle for the given ServletContext
75       * @since 2.0.1
76       */
77      public static Log4jWebLifeCycle getWebLifeCycle(final ServletContext servletContext) {
78          WEB_SUPPORT_LOOKUP.lock();
79          try {
80              Log4jWebLifeCycle webLifeCycle = (Log4jWebLifeCycle) servletContext.getAttribute(
81                  Log4jWebSupport.SUPPORT_ATTRIBUTE);
82              if (webLifeCycle == null) {
83                  webLifeCycle = Log4jWebInitializerImpl.initialize(servletContext);
84              }
85              return webLifeCycle;
86          } finally {
87              WEB_SUPPORT_LOOKUP.unlock();
88          }
89      }
90  
91      /**
92       * Wraps a Runnable instance by setting its thread context {@link org.apache.logging.log4j.core.LoggerContext}
93       * before execution and clearing it after execution.
94       *
95       * @param servletContext the ServletContext to locate a LoggerContext for
96       * @param runnable       the Runnable to wrap execution for
97       * @return a wrapped Runnable
98       * @since 2.0.1
99       */
100     public static Runnable wrapExecutionContext(final ServletContext servletContext, final Runnable runnable) {
101         return new Runnable() {
102             @Override
103             public void run() {
104                 final Log4jWebSupport webSupport = getWebLifeCycle(servletContext);
105                 webSupport.setLoggerContext();
106                 try {
107                     runnable.run();
108                 } finally {
109                     webSupport.clearLoggerContext();
110                 }
111             }
112         };
113     }
114 
115     /**
116      * Gets the current {@link ServletContext} if it has already been assigned to a LoggerContext's external context.
117      *
118      * @return the current ServletContext attached to a LoggerContext or {@code null} if none could be found
119      * @since 2.1
120      */
121     public static ServletContext getServletContext() {
122         org.apache.logging.log4j.spi.LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
123         if (lc == null) {
124             lc = LogManager.getContext(false);
125         }
126         return lc == null ? null :
127             lc.getExternalContext() instanceof ServletContext ? (ServletContext) lc.getExternalContext() : null;
128     }
129 }