001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements. See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache license, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License. You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the license for the specific language governing permissions and
015     * limitations under the license.
016     */
017    package org.apache.logging.log4j.core.web;
018    
019    import org.apache.logging.log4j.LogManager;
020    import org.apache.logging.log4j.core.impl.ContextAnchor;
021    import org.apache.logging.log4j.core.impl.Log4jContextFactory;
022    import org.apache.logging.log4j.core.selector.ContextSelector;
023    import org.apache.logging.log4j.core.LoggerContext;
024    import org.apache.logging.log4j.core.selector.NamedContextSelector;
025    import org.apache.logging.log4j.spi.LoggerContextFactory;
026    
027    import javax.servlet.Filter;
028    import javax.servlet.FilterChain;
029    import javax.servlet.FilterConfig;
030    import javax.servlet.ServletContext;
031    import javax.servlet.ServletException;
032    import javax.servlet.ServletRequest;
033    import javax.servlet.ServletResponse;
034    import javax.servlet.UnavailableException;
035    import java.io.IOException;
036    
037    /**
038     * ServletFilter than may be used to set up a LoggerContext for each web application.
039     */
040    public class JNDIContextFilter implements Filter {
041        /**
042         * The Filter init parameter that defines the name of the LoggerContext.
043         */
044        public static final String CONTEXT_NAME = "context-name";
045        /**
046         * The Filter init parameter that defines the configuration location for the LoggerContext.
047         */
048        public static final String CONFIG_LOCATION = "config-location";
049        private ServletContext context;
050        private boolean created = false;
051        private String name;
052        private NamedContextSelector selector = null;
053    
054        public void init(FilterConfig filterConfig) throws ServletException {
055            context = filterConfig.getServletContext();
056            name = filterConfig.getInitParameter(CONTEXT_NAME);
057            String configLocn = filterConfig.getInitParameter(CONFIG_LOCATION);
058            if (name == null) {
059                throw new UnavailableException("A context-name attribute is required");
060            }
061            if (context.getAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE) == null) {
062                LoggerContext ctx;
063                LoggerContextFactory factory = LogManager.getFactory();
064                if (factory instanceof Log4jContextFactory) {
065                    ContextSelector sel = ((Log4jContextFactory) factory).getSelector();
066                    if (sel instanceof NamedContextSelector) {
067                        selector = (NamedContextSelector) sel;
068                        ctx = selector.locateContext(name, configLocn);
069                    } else {
070                        return;
071                    }
072                } else {
073                    return;
074                }
075                context.setAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE, ctx);
076                created = true;
077                context.log("Created context for " + name + " using " + ctx.getClass().getClassLoader());
078            }
079        }
080    
081        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
082            throws IOException, ServletException {
083            LoggerContext ctx = (LoggerContext) context.getAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE);
084            if (ctx != null) {
085                ContextAnchor.THREAD_CONTEXT.set(ctx);
086                try {
087                    filterChain.doFilter(servletRequest, servletResponse);
088                } finally {
089                    ContextAnchor.THREAD_CONTEXT.remove();
090                }
091            } else {
092                filterChain.doFilter(servletRequest, servletResponse);
093            }
094        }
095    
096        public void destroy() {
097            LoggerContext ctx = (LoggerContext) context.getAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE);
098            if (ctx != null && created) {
099                context.log("Removing context for " + name);
100                context.removeAttribute(Log4jContextListener.LOG4J_CONTEXT_ATTRIBUTE);
101                if (selector != null) {
102                    selector.removeContext(name);
103                }
104                ctx.stop();
105            }
106        }
107    }