View Javadoc

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.jetspeed.webapp.logging;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.Properties;
22  
23  import javax.servlet.ServletContextEvent;
24  import javax.servlet.ServletContextListener;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.log4j.Hierarchy;
29  import org.apache.log4j.Level;
30  import org.apache.log4j.PropertyConfigurator;
31  import org.apache.log4j.spi.RootCategory;
32  
33  public class Log4JConfigurator implements ServletContextListener
34  {
35      /*** The optional web.xml context parameter name for the Log4J Config File (location). 
36       *  The specified location is interpreted as begin (webapp) context relative.
37       */
38      public static final String LOG4J_CONFIG_FILE = "log4j.config.file";
39  
40      /*** The default value for the Log4J Config File (location) */
41      public static final String LOG4J_CONFIG_FILE_DEFAULT = "/WEB-INF/log4j.properties";
42  
43      /*** The optional web.xml context parameter name for the Log4J Config webApplicationRoot (property) */
44      public static final String LOG4J_CONFIG_WEB_APPLICATION_ROOT_KEY = "log4j.config.webApplicationRoot.key";
45  
46      /*** The default value for the Log4J Config webApplicationRoot (property) */
47      public static final String LOG4J_CONFIG_WEB_APPLICATION_ROOT_KEY_DEFAULT = "webApplicationRoot";
48      
49      private Hierarchy isolatedHierarchy;
50      
51      private static Log log;
52  
53      public void contextInitialized(ServletContextEvent event)
54      {
55          String log4JConfigFile = event.getServletContext().getInitParameter(LOG4J_CONFIG_FILE);
56          if ( log4JConfigFile == null || log4JConfigFile.length() == 0 )
57          {
58              log4JConfigFile = LOG4J_CONFIG_FILE_DEFAULT;
59          }
60          String rootPath = event.getServletContext().getRealPath("");
61          InputStream input = event.getServletContext().getResourceAsStream(log4JConfigFile);
62          if ( input != null )
63          {
64              try
65              {
66                  Properties p = new Properties();
67                  p.load(input);
68                  String waRootKey = event.getServletContext().getInitParameter(LOG4J_CONFIG_WEB_APPLICATION_ROOT_KEY);
69                  if ( waRootKey == null || waRootKey.length() == 0 )
70                  {
71                      waRootKey = LOG4J_CONFIG_WEB_APPLICATION_ROOT_KEY_DEFAULT;
72                  }
73                  p.setProperty(waRootKey,rootPath);
74                  isolatedHierarchy = new Hierarchy(new RootCategory(Level.INFO));
75                  new PropertyConfigurator().doConfigure(p,isolatedHierarchy);
76                  IsolatedLog4JLogger.setHierarchy(isolatedHierarchy);
77                  log = LogFactory.getLog(this.getClass());
78                  log.info("IsolatedLog4JLogger configured");
79              }
80              catch (IOException ioe)
81              {
82                  event.getServletContext().log("Failed to configure Log4J from "+event.getServletContext().getServletContextName(),ioe);
83              }
84          }
85          else
86          {
87              event.getServletContext().log(event.getServletContext().getServletContextName()+" Log4JConfigurator: "+rootPath+log4JConfigFile+" not found.");
88          }
89      }
90  
91      public void contextDestroyed(ServletContextEvent event)
92      {
93          if ( log != null )
94          {
95              log.info("Shutting down IsolatedLog4JLogger");
96              log = null;
97          }
98          // flush Logger cache which might be kept in a shared context if
99          // commons-logging isn't loaded through this ContextClassLoader
100         LogFactory.release(Thread.currentThread().getContextClassLoader());
101         // shutdown Log4J hierarchy (log4J keeps log files locked on Windows otherwise)
102         if (isolatedHierarchy != null)
103         {
104         	isolatedHierarchy.shutdown();
105         }
106     }
107 }