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.core.config.Configurator;
020    import org.apache.logging.log4j.core.LoggerContext;
021    
022    import javax.servlet.ServletContext;
023    import javax.servlet.ServletContextEvent;
024    import javax.servlet.ServletContextListener;
025    import java.lang.reflect.Method;
026    
027    /**
028     * Saves the LoggerContext into the ServletContext as an attribute.
029     */
030    public class Log4jContextListener implements ServletContextListener {
031    
032        /**
033         * The name of the attribute to use to store the LoggerContext into the ServletContext.
034         */
035        public static final String LOG4J_CONTEXT_ATTRIBUTE = "Log4JContext";
036    
037        /**
038         * The location of the configuration.
039         */
040        public static final String LOG4J_CONFIG = "log4jConfiguration";
041    
042        /**
043         * The name of the LoggerContext.
044         */
045        public static final String LOG4J_CONTEXT_NAME = "log4jContextName";
046    
047        /**
048         * Initialize Logging for the web application.
049         * @param event The ServletContextEvent.
050         */
051        public void contextInitialized(ServletContextEvent event) {
052            ServletContext context = event.getServletContext();
053            String locn = context.getInitParameter(LOG4J_CONFIG);
054            String name = context.getInitParameter(LOG4J_CONTEXT_NAME);
055            if (name == null) {
056                name = context.getServletContextName();
057            }
058            if (name == null && locn == null) {
059                context.log("No Log4j context configuration provided");
060                return;
061            }
062            context.setAttribute(LOG4J_CONTEXT_ATTRIBUTE, Configurator.initialize(name, getClassLoader(context), locn));
063        }
064    
065        /**
066         * Shutdown logging for the web application.
067         * @param event The ServletContextEvent.
068         */
069        public void contextDestroyed(ServletContextEvent event) {
070            LoggerContext ctx = (LoggerContext) event.getServletContext().getAttribute(LOG4J_CONTEXT_ATTRIBUTE);
071            Configurator.shutdown(ctx);
072        }
073    
074        private ClassLoader getClassLoader(ServletContext context) {
075            Method[] methods = context.getClass().getMethods();
076            Method getClassLoader = null;
077            for (Method method : methods) {
078                if (method.getName().equals("getClassLoader")) {
079                    getClassLoader = method;
080                    break;
081                }
082            }
083    
084            if (getClassLoader != null) {
085                try {
086                    return (ClassLoader) getClassLoader.invoke(context, null);
087                } catch (Exception ex) {
088                    // Ignore the exception
089                }
090            }
091    
092            return Log4jContextListener.class.getClassLoader();
093        }
094    }