KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > services > logging > JetspeedLogFactoryService


1 package org.apache.jetspeed.services.logging;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 // Java classes
20
import javax.servlet.ServletConfig JavaDoc;
21 import javax.servlet.ServletContext JavaDoc;
22
23 // Jetspeed classes
24
import org.apache.jetspeed.services.resources.JetspeedResources;
25
26 // Log4J classes
27
import org.apache.log4j.LogManager;
28 import org.apache.log4j.Logger;
29 import org.apache.log4j.PropertyConfigurator;
30 import org.apache.log4j.xml.DOMConfigurator;
31
32 // Turbine classes
33
import org.apache.turbine.Turbine;
34 import org.apache.turbine.services.InitializationException;
35 import org.apache.turbine.services.TurbineBaseService;
36
37 /**
38  * The default implementation of the logging service in Jetspeed.
39  *
40  * This service initializes the underlying logging implementation,
41  * and acts as a factory for loggers.
42  * The current implementation uses Log4J.
43  *
44  * @see org.apache.log4j.LogManager
45  * @see org.apache.log4j.Logger
46  * @author <a HREF="mailto:harald@ommang.com">Harald Ommang</a>
47  */

48 public class JetspeedLogFactoryService extends TurbineBaseService
49 {
50
51     public String JavaDoc SERVICE_NAME = "JetspeedLogFactoryService";
52     private static final String JavaDoc CONFIG_LOG4J_PROPERTIES = "log4j.properties";
53     private static final String JavaDoc CONFIG_LOG4J_PROPERTIES_DEFAULT = "/WEB-INF/conf/log4j.properties";
54     private static final String JavaDoc CONFIG_LOG4J_AND_WATCH = "log4j.configureAndWatch";
55     private static final boolean CONFIG_LOG4J_AND_WATCH_DEFAULT = true;
56     private static final String JavaDoc CONFIG_LOG4J_WATCHINTERVAL = "log4j.watchInterval";
57     private static final long CONFIG_LOG4J_WATCHINTERVAL_DEFAULT = 60000L;
58     private ServletContext JavaDoc context;
59     /**
60      * Flag to check for initilization. Needed to make time of init more robust.
61      * Also, cannot access the init in parent class from static method
62      */

63     private static boolean initDone = false;
64         
65     /**
66      * Default constructor
67      */

68     public JetspeedLogFactoryService()
69     {
70         context = null;
71     }
72    
73     /**
74      * Initializes the service by getting the servlet configuration from Turbine
75      *
76      * @throws InitializationException Initialization failed
77      */

78     public void init() throws InitializationException
79     {
80         ServletConfig JavaDoc conf = Turbine.getTurbineServletConfig();
81         if(conf != null)
82         {
83             init(conf);
84         }
85     }
86
87     /**
88      * Initializes the service with the given configuration
89      * Initializes the underlying logging implementation, Log4J
90      *
91      * @param config The ServletConfiguration from Turbine
92      *
93      * @throws InitializationException Initialization failed
94      */

95     public void init(ServletConfig JavaDoc config) throws InitializationException
96     {
97         context = config.getServletContext();
98         String JavaDoc log4jProperties = JetspeedResources.getString(CONFIG_LOG4J_PROPERTIES, CONFIG_LOG4J_PROPERTIES_DEFAULT);
99         if(log4jProperties != null)
100         {
101             try
102             {
103                 String JavaDoc fileName = Turbine.getRealPath(log4jProperties);
104                 boolean watch = JetspeedResources.getBoolean(CONFIG_LOG4J_AND_WATCH, CONFIG_LOG4J_AND_WATCH_DEFAULT);
105                 long watchInterval = JetspeedResources.getLong(CONFIG_LOG4J_WATCHINTERVAL, CONFIG_LOG4J_WATCHINTERVAL_DEFAULT);
106                 System.setProperty("webappRoot", context.getRealPath("/"));
107                 
108                 // Check to see if property or XML configuration is to be used.
109
if(fileName.endsWith(".properties"))
110                 {
111                     if(watch)
112                     {
113                         // Configure with a property file and watch for changes
114
PropertyConfigurator.configureAndWatch(fileName, watchInterval);
115                     }
116                     else
117                     {
118                         PropertyConfigurator.configure(fileName);
119                     }
120                 }
121                 else
122                 {
123                     if(watch)
124                     {
125                         // Configure with an XML file and watch for changes
126
DOMConfigurator.configureAndWatch(fileName, watchInterval);
127                     }
128                     else
129                     {
130                         DOMConfigurator.configure(fileName);
131                     }
132                 }
133             }
134             catch(Exception JavaDoc e)
135             {
136                 throw new InitializationException("Failed to load " + log4jProperties + " - " + e.toString());
137             }
138         }
139         setInit(true);
140         initDone = true;
141     } // init
142

143     /**
144      * The actual Factory method that gets the appropriate logger from Log4j and
145      * wraps it in a JetspeedLogger
146      */

147     public static JetspeedLogger getLogger(String JavaDoc loggerName)
148     {
149         // This test needed to ensure correct init sequence between this and services that log.
150
if (!initDone)
151         {
152             synchronized (JetspeedLogFactoryService.class)
153             {
154                  if (!initDone)
155                 {
156                     try
157                     {
158                         new JetspeedLogFactoryService().init();
159                     }
160                     catch(Exception JavaDoc e)
161                     {
162                         System.err.println("Init failed no logging available" + e.getMessage());
163                         e.printStackTrace();
164                     }
165                  }
166             }
167         }
168         Logger newLog = LogManager.getLogger(loggerName);
169         JetspeedLogger newLogger = new JetspeedLogger(newLog);
170         return newLogger;
171     }
172 } // class JetspeedLogFactoryService
173

174
175
Popular Tags