Package org.internna.iwebmvc.spring.jee

Source Code of org.internna.iwebmvc.spring.jee.IWebMvcContextLoaderListener

/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.spring.jee;

import java.io.File;
import java.net.URL;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Log4jConfigurer;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.util.WebUtils;

/**
* Loads framework default XML configuration files automatically and initilializes Log4J.
*
* @author Jose Noheda
* @since 2.0
*/
public final class IWebMvcContextLoaderListener extends ContextLoaderListener {

    private Log logger = LogFactory.getLog(IWebMvcContextLoaderListener.class);

    private final static String LOG4J_PROPERTIES_LOCATION = "/WEB-INF/log4j.properties";

    private String configFilesLocations = "classpath*:spring/applicationContext.xml classpath*:spring/security.xml /WEB-INF/spring/applicationContext.xml";

    @Override public void contextInitialized(ServletContextEvent event) {
      ServletContext servletContext = event.getServletContext();
      initLog4J(servletContext);
        String serverInfo = servletContext.getServerInfo();
        if (logger.isInfoEnabled()) logger.info("Detected container: " + serverInfo);
        serverInfo = serverInfo.toUpperCase();
        String server = serverInfo.indexOf("TOMCAT") >= 0 ? "tomcat" : "jetty";
        if ((serverInfo.indexOf("SUN JAVA") >= 0) | (serverInfo.indexOf("GLASSFISH") >= 0)) server = "glassfish";
        configFilesLocations = configFilesLocations.replace(" ", " classpath*:spring/" + server + ".xml ");
        if (logger.isInfoEnabled()) logger.info("Spring config locations established as [" + configFilesLocations + "]");
        super.contextInitialized(event);
    }

    @Override protected ContextLoader createContextLoader() {
        return new ContextLoader() {
            @Override protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) {
                applicationContext.setConfigLocation(configFilesLocations);
            }
        };
    }

    /**
     * Initializes Log4J. If WEB-INF/log4j.properties exists it uses it.
     * Otherwise it falls back to a log4.properties or log4j.xml
     * from the classpath.
     *
     * @param sc the servlet context
     */
    protected void initLog4J(ServletContext servletContext) {
      servletContext.log("Initializing Log4J from /WEB-INF/log4.properties");
        try {
            String location = WebUtils.getRealPath(servletContext, IWebMvcContextLoaderListener.LOG4J_PROPERTIES_LOCATION);
            URL url = new URL("file://" + location);
            if (new File(url.getFile()).exists()) Log4jConfigurer.initLogging(location);
            else servletContext.log("No web application specific log4j configuration found. Initializing Log4J from classpath:log4.properties");
        } catch (Exception ex) {
            servletContext.log("No web application specific log4j configuration found. Initializing Log4J from classpath:log4.properties");
        }
    }

    @Override public void contextDestroyed(ServletContextEvent event) {
        super.contextDestroyed(event);
        Log4jConfigurer.shutdownLogging();
    }

}
TOP

Related Classes of org.internna.iwebmvc.spring.jee.IWebMvcContextLoaderListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.