View Javadoc

1   /*
2    * Copyright (C) 2007 ETH Zurich
3    *
4    * This file is part of Fosstrak (www.fosstrak.org).
5    *
6    * Fosstrak is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License version 2.1, as published by the Free Software Foundation.
9    *
10   * Fosstrak is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with Fosstrak; if not, write to the Free
17   * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18   * Boston, MA  02110-1301  USA
19   */
20  
21  package org.fosstrak.epcis.repository;
22  
23  import java.util.Enumeration;
24  
25  import javax.servlet.ServletContext;
26  import javax.servlet.ServletContextEvent;
27  import javax.servlet.ServletContextListener;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /**
33   * This RepositoryContextListener performs the initialization and termination
34   * (shut-down) work required by the Fosstrak EPCIS repository application, such as
35   * initializing the logging framework. This class receives notifications about
36   * changes to the servlet context of the web application, e.g., when the servlet
37   * context is loaded, or is about to be shut down.
38   * 
39   * @author Marco Steybe
40   */
41  public class RepositoryContextListener implements ServletContextListener {
42  
43      private static final Log LOG = LogFactory.getLog(RepositoryContextListener.class);
44  
45      /**
46       * {@inheritDoc}
47       * 
48       * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
49       */
50      public void contextInitialized(ServletContextEvent event) {
51          ServletContext ctx = event.getServletContext();
52  
53          /*
54           * Note: logging is initialized automatically by reading
55           * logging.properties and log4j.properties from the classpath.
56           * logging.properties is used to tell commons-logging to use LOG4J as
57           * its underlying logging toolkit; log4j.properties is used to configure
58           * LOG4J. To initialize LOG4J manually from LOG4J_CONFIG_LOCATION,
59           * un-comment the following code (LOG4J_CONFIG_LOCATION =
60           * "log4jConfigLocation") ...
61           */
62          // "log4jConfigLocation";
63          // String path = ctx.getRealPath("/");
64          // String log4jCfg = ctx.getInitParameter(LOG4J_CONFIG_LOCATION);
65          // // initialize Log4j
66          // if (log4jCfg != null) {
67          // // if no log4j properties file found, then do not try
68          // // to load it (the application runs without logging)
69          // PropertyConfigurator.configure(path + log4jCfg);
70          // }
71          // log = LogFactory.getLog(this.getClass());
72  
73          // set a system property to configure CXF to use LOG4J
74          System.setProperty("org.apache.cxf.Logger", "org.apache.cxf.common.logging.Log4jLogger");
75  
76          LOG.info("Starting Fosstrak EPCIS Repository application");
77  
78          if (LOG.isDebugEnabled()) {
79              LOG.debug("Logging application context init-parameters:");
80              Enumeration<?> e = ctx.getInitParameterNames();
81              while (e.hasMoreElements()) {
82                  String param = (String) e.nextElement();
83                  LOG.debug(param + "=" + ctx.getInitParameter(param));
84              }
85          }
86      }
87  
88      /**
89       * {@inheritDoc}
90       * 
91       * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
92       */
93      public void contextDestroyed(ServletContextEvent event) {
94          LOG.info("Fosstrak EPCIS Repository application shut down\n######################################");
95          LogFactory.releaseAll();
96      }
97  }