KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > log4jMini > MonologLoggerFactory


1 /**
2  * Copyright (C) 2001-2003 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package org.objectweb.util.monolog.wrapper.log4jMini;
20
21 import org.apache.log4j.Category;
22 import org.apache.log4j.PropertyConfigurator;
23 import org.objectweb.util.monolog.api.BasicLevel;
24 import org.objectweb.util.monolog.api.Handler;
25 import org.objectweb.util.monolog.api.HandlerFactory;
26 import org.objectweb.util.monolog.api.Level;
27 import org.objectweb.util.monolog.api.LevelFactory;
28 import org.objectweb.util.monolog.api.Logger;
29 import org.objectweb.util.monolog.api.LoggerFactory;
30 import org.objectweb.util.monolog.api.TopicalLogger;
31 import org.objectweb.util.monolog.api.MonologFactory;
32 import org.objectweb.util.monolog.wrapper.common.Configurable;
33
34 import java.util.Hashtable JavaDoc;
35 import java.util.Properties JavaDoc;
36 import java.util.Vector JavaDoc;
37 import java.util.Enumeration JavaDoc;
38 import java.io.FileInputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.InputStream JavaDoc;
41 import java.io.FileNotFoundException JavaDoc;
42
43 public class MonologLoggerFactory implements MonologFactory {
44
45     static Hashtable JavaDoc monologCategories;
46
47     public static final String JavaDoc LOG4J_CF_PROP = "log4j.categoryFactory";
48     public static final String JavaDoc LOG4J_CF_VALUE
49         = "org.objectweb.util.monolog.wrapper.log4j.MonologCategoryFactory";
50
51     /**
52      * This static code initializes the BasicLevel fields.
53      */

54     static {
55         BasicLevel.INHERIT = -1;
56         BasicLevel.DEBUG = org.apache.log4j.Priority.DEBUG_INT;
57         BasicLevel.INFO = org.apache.log4j.Priority.INFO_INT;
58         BasicLevel.WARN = org.apache.log4j.Priority.WARN_INT;
59         BasicLevel.ERROR = org.apache.log4j.Priority.ERROR_INT;
60         BasicLevel.FATAL = org.apache.log4j.Priority.FATAL_INT;
61
62         BasicLevel.LEVEL_INHERIT = new LevelImpl("INHERIT", BasicLevel.INHERIT);
63         BasicLevel.LEVEL_DEBUG = new LevelImpl("DEBUG", BasicLevel.DEBUG);
64         BasicLevel.LEVEL_INFO = new LevelImpl("INFO", BasicLevel.INFO);
65         BasicLevel.LEVEL_WARN = new LevelImpl("WARN", BasicLevel.WARN);
66         BasicLevel.LEVEL_ERROR = new LevelImpl("ERROR", BasicLevel.ERROR);
67         BasicLevel.LEVEL_FATAL = new LevelImpl("FATAL", BasicLevel.FATAL);
68
69         monologCategories = new Hashtable JavaDoc();
70         Category root = Category.getRoot();
71     }
72
73     /**
74      * This field references the level instances by their names.<br/>
75      * key = a level name<br/>
76      * value = the unique Level instance linked to the name.
77      */

78     protected Hashtable JavaDoc nameToLevel = null;
79
80     /**
81      * This field reference the level names by their integer value.<br/>
82      * key = a java.lang.Integer which the value is the level<br/>
83      * value = a String or an ArrayList of String. The strings represent the
84      * name which match to the integer value. Indeed both name can be associated
85      * to the same integer value.
86      */

87     protected Hashtable JavaDoc intToNames = null;
88
89     /**
90      * This field references the handler instance by their names.<br/>
91      * key = a String object which is an handler name.
92      * value = the unique handler instance which has the key for name.
93      */

94     protected Hashtable JavaDoc handlers = null;
95
96     /**
97      * The root logger of the logger hierarchy
98      */

99     protected Logger rootLogger = null;
100
101     public MonologLoggerFactory() {
102         intToNames = new Hashtable JavaDoc();
103         nameToLevel = new Hashtable JavaDoc();
104         defineDefaultLevels();
105         handlers = new Hashtable JavaDoc();
106         rootLogger = new MonologCategory(Category.getRoot());
107     }
108
109     /**
110      * It initializes the default monolog level:
111      * <ul>
112      * <li>DEBUG: 10 000</li>
113      * <li>INFO: 20 000</li>
114      * <li>WARN: 30 000</li>
115      * <li>ERROR: 40 000</li>
116      * <li>FATAL: 50 000</li>
117      * </ul>
118      */

119     protected void defineDefaultLevels() {
120         defineLevel("INHERIT", -1);
121         defineLevel("DEBUG", 10000);
122         defineLevel("INFO", 20000);
123         defineLevel("WARN", 30000);
124         defineLevel("ERROR", 40000);
125         defineLevel("FATAL", 50000);
126     }
127
128     // IMPLEMENTATION OF THE Configurable INTERFACE //
129

130     /**
131      * This method permits to configure the factory.
132      */

133     public void configure(Properties JavaDoc prop) throws Exception JavaDoc {
134         if (prop==null) {
135             return;
136         }
137         String JavaDoc conf = prop.getProperty(LOG_CONFIGURATION_TYPE,
138             prop.getProperty("log4jConfiguration", DEFAULT));
139         if (PROPERTY.equals(conf)) {
140
141             // Fetch the configuration file name
142
String JavaDoc filename = prop.getProperty(LOG_CONFIGURATION_FILE,
143                 prop.getProperty("log4jConfigurationFile", ""));
144             Properties JavaDoc log4jfileprop = null;
145             // Check if the file name designs a path valid into the
146
// classpath, or designs a path valid into the file system.
147
// In both case load the property file
148
if (prop.getProperty(LOG_CONFIGURATION_FILE_USE_CLASSPATH, "false")
149                 .equalsIgnoreCase("true")
150                 || prop.getProperty("findFileInClassPath", "false")
151                 .equalsIgnoreCase("true")) {
152
153                 if (!"log4j.properties".equals(filename)) {
154                     // valid into the classpath
155
log4jfileprop = getProperties(filename);
156                     log4jfileprop.put(LOG4J_CF_PROP, LOG4J_CF_VALUE);
157                     PropertyConfigurator.configure(log4jfileprop);
158                 }
159                 // else the file is already loaded by the Category class
160
}
161             else {
162                 // valid into the file system
163
log4jfileprop = new Properties JavaDoc();
164                 log4jfileprop.load(new FileInputStream JavaDoc(filename));
165                 log4jfileprop.put(LOG4J_CF_PROP, LOG4J_CF_VALUE);
166                 PropertyConfigurator.configure(log4jfileprop);
167             }
168         }
169         else if (XML.equals(conf)) {
170             throw new Exception JavaDoc(
171                 "xml log4jConfiguration is not suported in the log4jME wrapper");
172         }
173     }
174
175     // Return null if properties are not reachable
176
private Properties JavaDoc getProperties(String JavaDoc name) throws IOException JavaDoc {
177         InputStream JavaDoc is = ClassLoader.getSystemResourceAsStream(name);
178         if (is != null) {
179             Properties JavaDoc props = new Properties JavaDoc();
180             props.load(is);
181             return props;
182         }
183         throw new FileNotFoundException JavaDoc("Not found in classpath: " + name);
184     }
185
186     // IMPLEMENTATION OF INTERFACE LoggerFactory
187
public Logger[] getLoggers() {
188         Logger[] logs = new TopicalLogger[monologCategories.size() + 1];
189         logs[0] = rootLogger;
190         int i=1;
191         for(Enumeration JavaDoc en = monologCategories.elements(); en.hasMoreElements();) {
192             logs[i++] = (TopicalLogger) en.nextElement();
193         }
194         return logs;
195     }
196     public String JavaDoc getTopicPrefix() {
197         return null;
198     }
199
200     public Logger getLogger(String JavaDoc key) {
201         if (key == null || key.length() == 0 || key.equalsIgnoreCase("root")) {
202             return rootLogger;
203         }
204         Logger log = (Logger) monologCategories.get(key);
205         if (log == null) {
206             log = new MonologCategory(Category.getInstance(key));
207             monologCategories.put(key, log);
208         }
209         return log;
210     }
211
212     public synchronized Logger getLogger(String JavaDoc key, String JavaDoc rbn) {
213         return getLogger(key);
214     }
215
216     public String JavaDoc getResourceBundleName() {
217         return "";
218     }
219
220     public void setResourceBundleName(String JavaDoc rbn) {
221     }
222
223     // IMPLEMENTATION OF THE LevelFactory INTERFACE //
224
//-----------------------------------------------//
225

226     public Level defineLevel(String JavaDoc name, int value) {
227         return defineLevel(new LevelImpl(name, value));
228     }
229
230     public Level defineLevel(String JavaDoc name, String JavaDoc value) {
231         return defineLevel(new LevelImpl(name, value, this));
232     }
233
234     public Level getLevel(String JavaDoc name) {
235         return (Level) nameToLevel.get(name);
236     }
237
238     public Level getLevel(int value) {
239         Object JavaDoc temp = intToNames.get(new Integer JavaDoc(value));
240         if (temp == null) {
241             return null;
242         }
243         else if (temp instanceof String JavaDoc) {
244             return getLevel((String JavaDoc) temp);
245         }
246         else if (temp instanceof Vector JavaDoc) {
247             return getLevel((String JavaDoc) ((Vector JavaDoc) temp).elementAt(0));
248         }
249         return null;
250     }
251
252     public Level[] getLevels() {
253         if (nameToLevel==null) {
254             return new Level[0];
255         }
256         Level[] result = new Level[nameToLevel.size()];
257         int i = 0;
258         for (Enumeration JavaDoc e= nameToLevel.elements(); e.hasMoreElements() ; i++) {
259             result[i] = (Level)(e.nextElement());
260         }
261         return result;
262     }
263
264     public void removeLevel(String JavaDoc name) {
265         Level removed = (Level) nameToLevel.remove(name);
266         if (removed != null) {
267             Integer JavaDoc i = new Integer JavaDoc(removed.getIntValue());
268             Object JavaDoc temp = intToNames.get(i);
269             if (temp instanceof String JavaDoc) {
270                 intToNames.remove(i);
271             }
272             else if (temp instanceof Vector JavaDoc) {
273                 ((Vector JavaDoc) temp).removeElement(name);
274             }
275         }
276     }
277     // OTHER METHODS //
278
//---------------//
279

280     /**
281      * Insert a level into the data structure.<br/>
282      * If the level name is already used with other integer value, the null
283      * value is returned.<br/>
284      * If the level name is already used with the same integer value, the level
285      * found in the data structure is returned.<br/>
286      *
287      * @param l the Level instance which must be inserted.
288      * @return the Level instance or a null value.
289      */

290     private Level defineLevel(Level l) {
291         //System.out.println("def(" + l + ") begin");
292
String JavaDoc name = l.getName();
293         int value = l.getIntValue();
294         Level res = (Level) nameToLevel.get(name);
295         if (res != null) {
296             // The name is already defined.
297
return (res.getIntValue() == value ? res : null);
298         }
299         else {
300             res = l;
301             nameToLevel.put(name, res);
302             Integer JavaDoc i = new Integer JavaDoc(value);
303             Object JavaDoc temp = intToNames.get(i);
304             if (temp != null) {
305                 if (temp instanceof String JavaDoc) {
306                     if (!((String JavaDoc) temp).equalsIgnoreCase(name)) {
307                         // The int value has already another name
308
// Add the new name to the other
309
Vector JavaDoc al = new Vector JavaDoc(5);
310                         al.addElement(temp);
311                         al.addElement(name);
312                         intToNames.put(i, al);
313                     }
314                 }
315                 else if (temp instanceof Vector JavaDoc) {
316                     // The int value has already several another name
317
Vector JavaDoc al = (Vector JavaDoc) temp;
318                     if (!al.contains(name)) {
319                         // Add the new name to the others
320
al.addElement(name);
321                     }
322                 }
323             }
324             else {
325                 // The int value does not have any name
326
intToNames.put(i, name);
327             }
328         }
329         //System.out.println("def(" + l + ") end");
330
return res;
331     }
332     // IMPLEMENTATION OF THE HandlerFactory INTERFACE //
333
//------------------------------------------------//
334

335     public Handler createHandler(String JavaDoc hn, String JavaDoc handlertype) {
336         Handler res = (Handler) handlers.get(hn);
337         if (res != null) {
338             return null;
339         }
340         res = new FileHandler(handlertype, hn);
341         handlers.put(hn, res);
342         return res;
343     }
344
345     public Handler[] getHandlers() {
346         if (handlers==null) {
347             return new Handler[0];
348         }
349         Handler[] result = new Handler[handlers.size()];
350         int i = 0;
351         for (Enumeration JavaDoc e= handlers.elements(); e.hasMoreElements() ; i++) {
352             result[i] = (Handler)(e.nextElement());
353         }
354         return result;
355     }
356
357     public Handler getHandler(String JavaDoc hn) {
358         return (Handler) handlers.get(hn);
359     }
360
361     public Handler removeHandler(String JavaDoc hn) {
362         return (Handler) handlers.remove(hn);
363     }
364 }
Popular Tags