KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > bin > Log4jInitServlet


1 package org.jahia.bin;
2
3 import javax.servlet.http.HttpServlet JavaDoc;
4 import javax.servlet.http.HttpServletRequest JavaDoc;
5 import javax.servlet.http.HttpServletResponse JavaDoc;
6
7 import java.io.InputStream JavaDoc;
8 import java.util.Properties JavaDoc;
9 import java.lang.RuntimeException JavaDoc;
10
11 import org.apache.log4j.xml.*;
12 import org.apache.log4j.PropertyConfigurator;
13 import java.util.Enumeration JavaDoc;
14 import org.apache.log4j.Logger;
15 import org.apache.log4j.LogManager;
16
17 /**
18  * <p>Title: Log4J initialization servlet</p>
19  * <p>Description: This is a servlet that is configured to be initialized
20  * when the servlet container startups, so that it may load the configuration
21  * for the Log4J logging system</p>
22  * <p>Copyright: Copyright (c) 2002</p>
23  * <p>Company: </p>
24  * @author Serge Huber
25  * @version 1.0
26  */

27 public class Log4jInitServlet extends HttpServlet JavaDoc {
28
29     public void init () {
30         boolean isLog4jPathOk = false;
31
32         String JavaDoc realPath;
33         String JavaDoc xmlFile = getInitParameter("log4j-xml-init-file");
34
35         // ------------------------------------
36
// if the log4j-init-file is not set, then no point in trying
37

38         getServletContext().log(">> log4j-xml-init-file = " + xmlFile);
39         realPath = getServletContext().getRealPath(xmlFile);
40
41         getServletContext().log(">> realPath = " + realPath);
42
43         /*
44         if (isLog4JConfigured()) {
45             getServletContext().log("Log4J already configured, aborting configuration...");
46             return;
47         }
48         */

49
50         if (xmlFile != null) {
51             try {
52
53                 DOMConfigurator.configureAndWatch(realPath);
54                 isLog4jPathOk = true;
55
56             } catch (Exception JavaDoc e) {
57                 getServletContext().log("Exception(DOMConfigurator) : " +
58                                         e.getMessage());
59                 getServletContext().log("Looking for WAR file");
60             }
61
62         } else {
63             getServletContext().log(
64                 "Error, couldn't find log4j configuration file " +
65                 xmlFile.toString());
66             getServletContext().log("Looking for WAR file");
67         }
68
69         // -------------------------------------
70

71         InputStream JavaDoc input = getServletContext().getResourceAsStream(xmlFile);
72         if (input != null) {
73
74             try {
75                 Properties JavaDoc prop = new Properties JavaDoc();
76                 prop.load(input);
77
78                 PropertyConfigurator.configure(prop);
79             } catch (Exception JavaDoc e) {
80                 getServletContext().log("Exception(PropertyConfigurator) : " +
81                                         e.getMessage());
82                 getServletContext().log(xmlFile.toString() +
83                                         " has not been found in WAR file");
84                 throw new RuntimeException JavaDoc(e.getMessage());
85             }
86
87         } else {
88             getServletContext().log(xmlFile.toString() +
89                                     " has not been found in WAR file");
90         }
91
92     }
93
94     public void destroy() {
95     }
96
97     /**
98      * Returns true if it appears that log4j have been previously configured. This code
99      * checks to see if there are any appenders defined for log4j which is the
100      * definitive way to tell if log4j is already initialized
101      * @return boolean true if log4j has already been configured, false
102      * otherwise
103      */

104     private static boolean isLog4JConfigured() {
105         Enumeration JavaDoc enumeration = Logger.getRoot().getAllAppenders();
106         if ((enumeration != null) && (!(enumeration instanceof org.apache.log4j.helpers.NullEnumeration))) {
107             return true;
108         }
109         else {
110             Enumeration JavaDoc loggers = LogManager.getCurrentLoggers() ;
111             while (loggers.hasMoreElements()) {
112                 Logger c = (Logger) loggers.nextElement();
113                 if (!(c.getAllAppenders() instanceof org.apache.log4j.helpers.NullEnumeration))
114                     return true;
115             }
116         }
117         return false;
118     }
119
120
121
122     public void doGet (HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
123         getServletContext().log(">>---------------------------------------");
124         getServletContext().log(">> In org.jahia.bin.Log4jInitServlet ");
125         getServletContext().log(">>---------------------------------------");
126
127     }
128
129 }
130
Popular Tags