KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > servlets > Initializer


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.servlets;
14
15 import info.magnolia.cms.beans.config.ConfigLoader;
16 import info.magnolia.logging.Log4jConfigurer;
17
18 import java.util.Enumeration JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.servlet.ServletContext JavaDoc;
23 import javax.servlet.ServletContextEvent JavaDoc;
24 import javax.servlet.ServletContextListener JavaDoc;
25
26 import org.apache.log4j.Logger;
27
28
29 /**
30  * Magnolia default initializer: initialize logging, reads any parameter specified as context-param in web.xml and calls
31  * ConfigLoader. Users are free to implement custom loaders which read parameters from other sources. Parameters should
32  * be defined in <code>web.xml</code> using <code>context-param</code> elements:
33  *
34  * <pre>
35  * &lt;context-param>
36  * &lt;param-name>&lt;/param-name>
37  * &lt;param-value>&lt;/param-value>
38  * &lt;/context-param>
39  * </pre>
40  *
41  * The following parameters are needed:
42  * <dl>
43  * <dt>magnolia.cache.startdir</dt>
44  * <dd>directory used for cached pages</dd>
45  * <dt>magnolia.upload.tmpdir</dt>
46  * <dd>tmp directory for uploaded files</dd>
47  * <dt>magnolia.exchange.history</dt>
48  * <dd>history directory used for activation</dd>
49  * <dt>magnolia.repositories.config</dt>
50  * <dd>repositories configuration</dd>
51  * <dt>log4j.config</dt>
52  * <dd>Name of a log4j config file. Can be a .properties or .xml file. The value can be:
53  * <ul>
54  * <li>a full path</li>
55  * <li>a path relative to the webapp root</li>
56  * <li> a file name which will be loaded from the classpath</li>
57  * </ul>
58  * </dd>
59  * <dt>magnolia.root.sysproperty</dt>
60  * <dd>Name of a system variable which will be set to the webapp root. You can use this property in log4j configuration
61  * files to handle relative paths, such as <code>${magnolia.root}logs/magnolia-debug.log</code>. <strong>Important</strong>:
62  * if you drop multiple magnolia wars in a container which doesn't isolate system properties (e.g. tomcat) you will need
63  * to change the name of the <code>magnolia.root.sysproperty</code> variable in web.xml and in log4j configuration
64  * files.</dd>
65  * <dt>magnolia.bootstrap.dir</dt>
66  * <dd> Directory containing xml files for initialization of a blank magnolia instance. If no content is found in any of
67  * the repository, they are initialized importing xml files found in this folder. If you don't want to let magnolia
68  * automatically initialize repositories simply remove this parameter.</dd>
69  * </dl>
70  * @author Sameer Charles
71  * @author Fabrizio Giustina
72  * @version $Id: Initializer.java 1110 2005-07-06 15:30:44Z fgiust $
73  */

74 public class Initializer implements ServletContextListener JavaDoc {
75
76     /**
77      * Stable serialVersionUID.
78      */

79     private static final long serialVersionUID = 222L;
80
81     /**
82      * Logger.
83      */

84     private static Logger log = Logger.getLogger(Initializer.class);
85
86     /**
87      * <p>
88      * load configuration parameters from servlet context, then:
89      * </p>
90      * <ol>
91      * <li>Initialize Log4j</li>
92      * <li>Instantiate a <code>info.magnolia.cms.beans.config.ConfigLoader</code> instance</li>
93      * </ol>
94      * @see ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
95      * @see ConfigLoader
96      */

97     public void contextInitialized(ServletContextEvent JavaDoc sce) {
98         ServletContext JavaDoc context = sce.getServletContext();
99
100         // copy all the initialization parameters in a Map, so that ConfigLoader is not tied to a ServletConfig instance
101
Map JavaDoc parameters = new HashMap JavaDoc();
102         Enumeration JavaDoc configParams = context.getInitParameterNames();
103         while (configParams.hasMoreElements()) {
104             String JavaDoc paramName = (String JavaDoc) configParams.nextElement();
105             parameters.put(paramName, context.getInitParameter(paramName));
106         }
107
108         Log4jConfigurer.initLogging(context, parameters);
109
110         try {
111             new ConfigLoader(context, parameters);
112         }
113         catch (Exception JavaDoc e) {
114             log.fatal(e.getMessage(), e);
115         }
116     }
117
118     /**
119      * Shutdown logging.
120      * @see ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
121      */

122     public void contextDestroyed(ServletContextEvent JavaDoc sce) {
123         ServletContext JavaDoc context = sce.getServletContext();
124
125         // copy all the initialization parameters in a Map
126
Map JavaDoc parameters = new HashMap JavaDoc();
127         Enumeration JavaDoc configParams = context.getInitParameterNames();
128         while (configParams.hasMoreElements()) {
129             String JavaDoc paramName = (String JavaDoc) configParams.nextElement();
130             parameters.put(paramName, context.getInitParameter(paramName));
131         }
132
133         Log4jConfigurer.shutdownLogging(parameters);
134     }
135
136 }
137
Popular Tags