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.core.LoggerContext;
24
25 /**
26 * Convenience methods for retrieving the {@link org.apache.logging.log4j.core.LoggerContext} associated with a
27 * particular ServletContext. These methods are most particularly useful for asynchronous servlets where the
28 * <abbr title="Thread Context ClassLoader">TCCL</abbr> is potentially different from the TCCL used by the
29 * Servlet container that bootstrapped Log4j.
30 *
31 * @since 2.0.1
32 */
33 public final class WebLoggerContextUtils {
34 private WebLoggerContextUtils() {
35 }
36
37 private static final Lock WEB_SUPPORT_LOOKUP = new ReentrantLock();
38
39 /**
40 * Finds the main {@link org.apache.logging.log4j.core.LoggerContext} configured for the given ServletContext.
41 *
42 * @param servletContext the ServletContext to locate a LoggerContext for
43 * @return the LoggerContext for the given ServletContext
44 * @throws java.lang.IllegalStateException if no LoggerContext could be found on the given ServletContext
45 * @since 2.0.1
46 */
47 public static LoggerContext getWebLoggerContext(final ServletContext servletContext) {
48 return (LoggerContext) servletContext.getAttribute(Log4jWebSupport.CONTEXT_ATTRIBUTE);
49 }
50
51 /**
52 * Finds the main {@link org.apache.logging.log4j.core.LoggerContext} configured for the given ServletContext.
53 *
54 * @param servletContext the ServletContext to locate a LoggerContext for
55 * @return the LoggerContext for the given ServletContext or {@code null} if none was set
56 * @since 2.0.1
57 */
58 public static LoggerContext getRequiredWebLoggerContext(final ServletContext servletContext) {
59 final LoggerContext loggerContext = getWebLoggerContext(servletContext);
60 if (loggerContext == null) {
61 throw new IllegalStateException(
62 "No LoggerContext found in ServletContext attribute " + Log4jWebSupport.CONTEXT_ATTRIBUTE);
63 }
64 return loggerContext;
65 }
66
67 /**
68 * Finds or initializes the {@link org.apache.logging.log4j.web.Log4jWebLifeCycle} singleton for the given
69 * ServletContext.
70 *
71 * @param servletContext the ServletContext to get the Log4jWebLifeCycle for
72 * @return the Log4jWebLifeCycle for the given ServletContext
73 * @since 2.0.1
74 */
75 public static Log4jWebLifeCycle getWebLifeCycle(final ServletContext servletContext) {
76 WEB_SUPPORT_LOOKUP.lock();
77 try {
78 Log4jWebLifeCycle webLifeCycle = (Log4jWebLifeCycle) servletContext.getAttribute(
79 Log4jWebSupport.SUPPORT_ATTRIBUTE);
80 if (webLifeCycle == null) {
81 webLifeCycle = Log4jWebInitializerImpl.initialize(servletContext);
82 }
83 return webLifeCycle;
84 } finally {
85 WEB_SUPPORT_LOOKUP.unlock();
86 }
87 }
88
89 /**
90 * Wraps a Runnable instance by setting its thread context {@link org.apache.logging.log4j.core.LoggerContext}
91 * before execution and clearing it after execution.
92 *
93 * @param servletContext the ServletContext to locate a LoggerContext for
94 * @param runnable the Runnable to wrap execution for
95 * @return a wrapped Runnable
96 * @since 2.0.1
97 */
98 public static Runnable wrapExecutionContext(final ServletContext servletContext, final Runnable runnable) {
99 return new Runnable() {
100 @Override
101 public void run() {
102 final Log4jWebSupport webSupport = getWebLifeCycle(servletContext);
103 webSupport.setLoggerContext();
104 try {
105 runnable.run();
106 } finally {
107 webSupport.clearLoggerContext();
108 }
109 }
110 };
111 }
112 }