1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.web;
18
19 import org.apache.logging.log4j.LogManager;
20 import org.apache.logging.log4j.core.impl.ContextAnchor;
21 import org.apache.logging.log4j.core.impl.Log4jContextFactory;
22 import org.apache.logging.log4j.core.selector.ContextSelector;
23 import org.apache.logging.log4j.core.LoggerContext;
24 import org.apache.logging.log4j.core.selector.NamedContextSelector;
25 import org.apache.logging.log4j.spi.LoggerContextFactory;
26
27 import javax.servlet.Filter;
28 import javax.servlet.FilterChain;
29 import javax.servlet.FilterConfig;
30 import javax.servlet.ServletContext;
31 import javax.servlet.ServletException;
32 import javax.servlet.ServletRequest;
33 import javax.servlet.ServletResponse;
34 import javax.servlet.UnavailableException;
35 import java.io.IOException;
36
37
38
39
40 public class JNDIContextFilter implements Filter {
41
42
43
44 public static final String CONTEXT_NAME = "context-name";
45
46
47
48 public static final String CONFIG_LOCATION = "config-location";
49 private ServletContext context;
50 private boolean created = false;
51 private String name;
52 private NamedContextSelector selector = null;
53
54 public void init(final FilterConfig filterConfig) throws ServletException {
55 context = filterConfig.getServletContext();
56 name = filterConfig.getInitParameter(CONTEXT_NAME);
57 final String configLocn = filterConfig.getInitParameter(CONFIG_LOCATION);
58 if (name == null) {
59 throw new UnavailableException("A context-name attribute is required");
60 }
61 if (context.getAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE) == null) {
62 LoggerContext ctx;
63 final LoggerContextFactory factory = LogManager.getFactory();
64 if (factory instanceof Log4jContextFactory) {
65 final ContextSelector sel = ((Log4jContextFactory) factory).getSelector();
66 if (sel instanceof NamedContextSelector) {
67 selector = (NamedContextSelector) sel;
68 ctx = selector.locateContext(name, configLocn);
69 } else {
70 return;
71 }
72 } else {
73 return;
74 }
75 context.setAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE, ctx);
76 created = true;
77 context.log("Created context for " + name + " using " + ctx.getClass().getClassLoader());
78 }
79 }
80
81 public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
82 final FilterChain filterChain)
83 throws IOException, ServletException {
84 final LoggerContext ctx = (LoggerContext) context.getAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE);
85 if (ctx != null) {
86 ContextAnchor.THREAD_CONTEXT.set(ctx);
87 try {
88 filterChain.doFilter(servletRequest, servletResponse);
89 } finally {
90 ContextAnchor.THREAD_CONTEXT.remove();
91 }
92 } else {
93 filterChain.doFilter(servletRequest, servletResponse);
94 }
95 }
96
97 public void destroy() {
98 final LoggerContext ctx = (LoggerContext) context.getAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE);
99 if (ctx != null && created) {
100 context.log("Removing context for " + name);
101 context.removeAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE);
102 if (selector != null) {
103 selector.removeContext(name);
104 }
105 ctx.stop();
106 }
107 }
108 }