KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > util > monolog > wrapper > javalog > LoggerFactory


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
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  * $Id: LoggerFactory.java 12:04:24 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 /**
23  * Copyright (C) 2001-2003 France Telecom R&D
24  *
25  * This library is free software; you can redistribute it and/or
26  * modify it under the terms of the GNU Lesser General Public
27  * License as published by the Free Software Foundation; either
28  * version 2 of the License, or (at your option) any later version.
29  *
30  * This library is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33  * Lesser General Public License for more details.
34  *
35  * You should have received a copy of the GNU Lesser General Public
36  * License along with this library; if not, write to the Free Software
37  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38  */

39 package org.objectweb.petals.util.monolog.wrapper.javalog;
40
41 import java.util.ArrayList JavaDoc;
42 import java.util.Enumeration JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Properties JavaDoc;
45 import java.util.logging.LogManager JavaDoc;
46
47 import org.objectweb.util.monolog.Monolog;
48 import org.objectweb.util.monolog.api.BasicLevel;
49 import org.objectweb.util.monolog.wrapper.common.AbstractFactory;
50 import org.objectweb.util.monolog.wrapper.javaLog.LevelImpl;
51
52 /**
53  * Logger factory
54  *
55  * @author ddesjardins - eBMWebsourcing
56  */

57 public class LoggerFactory extends AbstractFactory {
58
59     protected String JavaDoc[][] getDefaultHandlerType2className() {
60         return new String JavaDoc[][] {
61             {handlerTypes[0],
62                 "org.objectweb.petals.util.monolog.wrapper.javalog.GenericHandler"},
63             {handlerTypes[1],
64                 "org.objectweb.petals.util.monolog.wrapper.javalog.GenericHandler"},
65             {handlerTypes[2],
66                 "org.objectweb.petals.util.monolog.wrapper.javalog.GenericHandler"},
67             {handlerTypes[3],
68                 "org.objectweb.petals.util.monolog.wrapper.javalog.GenericHandler"},
69             {handlerTypes[4],
70                 "org.objectweb.util.monolog.wrapper.javaLog.JMXGenericHandler"}};
71     }
72
73     /**
74      * the LogManager of java.util.logging
75      */

76     protected static LogManager JavaDoc manager = null;
77
78     /**
79      * the root logger
80      */

81     protected static Logger rootLogger = null;
82
83     /**
84      * Keep a reference to loggers.
85      *
86      * @see java.util.logging.LogManager addLogger javadoc
87      */

88     private List JavaDoc<Logger> loggers = new ArrayList JavaDoc<Logger>();
89
90     /**
91      * This static code initialize the value of the variable defined into
92      * BasicLevel.
93      */

94     static {
95         debug("Initializing " + LoggerFactory.class.getName());
96         debug("set default level values");
97         BasicLevel.INHERIT = -1;
98         debug("\t-INHERIT= " + BasicLevel.INHERIT);
99         BasicLevel.DEBUG = java.util.logging.Level.FINEST.intValue();
100         debug("\t-DEBUG= " + BasicLevel.DEBUG);
101         BasicLevel.INFO = java.util.logging.Level.INFO.intValue();
102         debug("\t-INFO= " + BasicLevel.INFO);
103         BasicLevel.WARN = java.util.logging.Level.WARNING.intValue();
104         debug("\t-WARN= " + BasicLevel.WARN);
105         BasicLevel.ERROR = java.util.logging.Level.SEVERE.intValue();
106         debug("\t-ERROR= " + BasicLevel.ERROR);
107         BasicLevel.FATAL = java.util.logging.Level.SEVERE.intValue();
108         debug("\t-FATAL= " + BasicLevel.FATAL);
109
110         BasicLevel.LEVEL_INHERIT = new LevelImpl("INHERIT", BasicLevel.INHERIT);
111         BasicLevel.LEVEL_DEBUG = new LevelImpl("DEBUG", BasicLevel.DEBUG);
112         BasicLevel.LEVEL_INFO = new LevelImpl("INFO", BasicLevel.INFO);
113         BasicLevel.LEVEL_WARN = new LevelImpl("WARN", BasicLevel.WARN);
114         BasicLevel.LEVEL_ERROR = new LevelImpl("ERROR", BasicLevel.ERROR);
115         BasicLevel.LEVEL_FATAL = new LevelImpl("FATAL", BasicLevel.FATAL);
116
117         manager = LogManager.getLogManager();
118         if (classLoaderIsoltion) {
119             debug("class loader isolation activated");
120             int i = 0;
121             while (rootLogger == null) {
122                 rootLoggerName = "root" + i;
123                 synchronized (manager) {
124                     // the manager object is shared between the multiple
125
// instances of this class
126
if (manager.getLogger(rootLoggerName) == null) {
127                         rootLogger = new Logger(rootLoggerName, null);
128                         manager.addLogger(rootLogger);
129                     } else {
130                         i++;
131                     }
132                 }
133             }
134             rootLogger.setUseParentHandlers(false);
135             Monolog.debug("Instanciate the root logger " + rootLoggerName);
136             rootLoggerPrefix = rootLoggerName + '.';
137         } else {
138             rootLogger = new Logger(manager.getLogger(""));
139         }
140     }
141
142     /**
143      * This constant is used to initialize the factory with the configure method
144      */

145     public final static String JavaDoc JAVALOG_CONFIGURATION = "javalogConfiguration";
146
147     /**
148      * This constant means that this java log system must be initialize with the
149      * default configuration
150      */

151     public final static String JavaDoc DEFAULT = "default";
152
153     /**
154      * This constant means that this java log system must be initialize with a
155      * property file
156      */

157     public final static String JavaDoc PROPERTY = "property";
158
159     /**
160      * This constant means that this java log system must be initialize with a
161      * xml file
162      */

163     public final static String JavaDoc CLASS = "class";
164
165     /**
166      * This constant is the properties file name with wich the java log system
167      * must be initialized.
168      */

169     public final static String JavaDoc JAVALOG_CONFIGURATION_FILE = "javalogConfigurationFile";
170
171     /**
172      * This constant is the properties class name with wich the java log system
173      * must be initialized.
174      */

175     public final static String JavaDoc JAVALOG_CONFIGURATION_CLASS = "javalogConfigurationClass";
176
177     public LoggerFactory() {
178         super();
179     }
180
181     public String JavaDoc getWrapperName() {
182         return "javalog";
183     }
184
185     /**
186      * This method allocates org.objectweb.util.monolog.wrapper.javaLog.Logger
187      * objects whic are also java.util.logging.Logger and
188      * org.objectweb.util.monolog.api.Logger.
189      */

190     protected synchronized Logger getMonoLogger(String JavaDoc name, String JavaDoc resName) {
191         if (name == null)
192             throw new IllegalArgumentException JavaDoc(
193                 "The specified Logger name is null");
194         if (name.equals("root") || name.length() == 0) {
195             return rootLogger;
196         }
197         // isolates the logger hierarchy
198
name = monoLoggerName(name);
199         // Search if the logger already exist.
200
Object JavaDoc o = manager.getLogger(name);
201         if (o == null) {
202             // It doesn't exist => creates and adds it
203
Logger result = new Logger(name, resName);
204             manager.addLogger(result);
205             Monolog.debug("Instanciate the logger " + name);
206
207             // In the javadoc of the LogManager, it is said that the caller
208
// need to keep a reference to this logger to avoid GC.
209
// Because LogManager could use some weak reference on it
210
loggers.add(result);
211             return result;
212         } else if (!o.getClass().equals(Logger.class)) {
213             throw new RuntimeException JavaDoc("Bad logger class (logger name: '"
214                 + name + "'), expected: " + Logger.class + ", found: "
215                 + o.getClass());
216         } else {
217             Monolog.debug("return existing logger " + name);
218             return (Logger) o;
219         }
220     }
221
222     // IMPLEMENTATION OF THE Configurable INTERFACE //
223
// ----------------------------------------------//
224

225     /**
226      * This method permits to configure the factory. The properties parameter
227      * must contains the JAVALOG_CONFIGURATION property. Its value can be
228      * DEFAULT, PROPERTY or XML. In the PROPERTY case of the properties
229      * parameter must also contain the JAVALOG_CONFIGURATION_FILE property which
230      * the value is the configuration file name. In the CLASS case of the
231      * properties parameter must also contain the JAVALOG_CONFIGURATION_CLASS
232      * property which the value is the configuration class name which will
233      * initialized the java log system..
234      *
235      */

236     public void configure(Properties JavaDoc prop) throws Exception JavaDoc {
237         if (prop == null)
238             return;
239         String JavaDoc confMode = prop.getProperty(JAVALOG_CONFIGURATION, null);
240         if (confMode == null)
241             return;
242
243         String JavaDoc param = null;
244         if (confMode.equals(PROPERTY)) {
245             param = prop.getProperty(JAVALOG_CONFIGURATION_FILE, null);
246             if (param != null)
247                 System.setProperty("java.util.logging.config.file", param);
248             manager.readConfiguration();
249         } else if (confMode.equals(CLASS)) {
250             param = prop.getProperty(JAVALOG_CONFIGURATION_CLASS, null);
251             if (param != null)
252                 System.setProperty("java.util.logging.config.class", param);
253             manager.readConfiguration();
254         }
255     }
256
257     // IMPLEMENTATION OF LoggerFactory INTERFACE //
258
// -------------------------------------------//
259

260     public org.objectweb.util.monolog.api.Logger getLogger(String JavaDoc key) {
261         return getMonoLogger(key, resourceBundleName);
262     }
263
264     public org.objectweb.util.monolog.api.Logger getLogger(String JavaDoc key,
265         String JavaDoc rbn) {
266         return getMonoLogger(key, rbn);
267     }
268
269     /**
270      * It retrieves a list of all loggers.
271      */

272     public org.objectweb.util.monolog.api.Logger[] getLoggers() {
273         List JavaDoc<Logger> res = new ArrayList JavaDoc<Logger>();
274         for (Enumeration JavaDoc e = manager.getLoggerNames(); e.hasMoreElements();) {
275             Object JavaDoc o = manager.getLogger((String JavaDoc) e.nextElement());
276             if (o instanceof Logger) {
277                 res.add((Logger) o);
278             }
279         }
280         return (Logger[]) res.toArray(new Logger[0]);
281     }
282
283 }
284
Popular Tags