001: /**
002: * Copyright (C) 2001-2003 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.util.monolog.wrapper.javaLog;
018:
019: import org.objectweb.util.monolog.api.BasicLevel;
020: import org.objectweb.util.monolog.wrapper.common.AbstractFactory;
021: import org.objectweb.util.monolog.wrapper.javaLog.Logger;
022: import org.objectweb.util.monolog.Monolog;
023:
024: import java.util.ArrayList;
025: import java.util.Enumeration;
026: import java.util.Properties;
027: import java.util.logging.LogManager;
028:
029: /**
030: * is the LoggerFactory for the wrapper to java.util.logging
031: *
032: * @author S.Chassande-Barrioz
033: */
034: public class LoggerFactory extends AbstractFactory {
035:
036: /**
037: * the LogManager of java.util.logging
038: */
039: protected static LogManager manager = null;
040:
041: /**
042: * the root logger
043: */
044: protected static Logger rootLogger = null;
045:
046: /**
047: * Keep a reference to loggers.
048: * @see java.util.logging.LogManager addLogger javadoc
049: */
050: private ArrayList loggers = new ArrayList();
051:
052: /**
053: * This static code initialize the value of the variable defined into
054: * BasicLevel.
055: */
056: static {
057: BasicLevel.INHERIT = -1;
058: debug("INHERIT= " + BasicLevel.INHERIT);
059: BasicLevel.DEBUG = java.util.logging.Level.FINEST.intValue();
060: debug("DEBUG= " + BasicLevel.DEBUG);
061: BasicLevel.INFO = java.util.logging.Level.INFO.intValue();
062: debug("INFO= " + BasicLevel.INFO);
063: BasicLevel.WARN = java.util.logging.Level.WARNING.intValue();
064: debug("WARN= " + BasicLevel.WARN);
065: BasicLevel.ERROR = java.util.logging.Level.SEVERE.intValue();
066: debug("ERROR= " + BasicLevel.ERROR);
067: BasicLevel.FATAL = java.util.logging.Level.SEVERE.intValue();
068: debug("FATAL= " + BasicLevel.FATAL);
069:
070: BasicLevel.LEVEL_INHERIT = new LevelImpl("INHERIT",
071: BasicLevel.INHERIT);
072: BasicLevel.LEVEL_DEBUG = new LevelImpl("DEBUG",
073: BasicLevel.DEBUG);
074: BasicLevel.LEVEL_INFO = new LevelImpl("INFO", BasicLevel.INFO);
075: BasicLevel.LEVEL_WARN = new LevelImpl("WARN", BasicLevel.WARN);
076: BasicLevel.LEVEL_ERROR = new LevelImpl("ERROR",
077: BasicLevel.ERROR);
078: BasicLevel.LEVEL_FATAL = new LevelImpl("FATAL",
079: BasicLevel.FATAL);
080:
081: manager = LogManager.getLogManager();
082: if (classLoaderIsoltion) {
083: int i = 0;
084: while (rootLogger == null) {
085: rootLoggerName = "root" + i;
086: synchronized (manager) {
087: // the manager object is shared between the multiple instances of this class
088: if (manager.getLogger(rootLoggerName) == null) {
089: rootLogger = new Logger(rootLoggerName, null);
090: manager.addLogger(rootLogger);
091: } else {
092: i++;
093: }
094: }
095: }
096: rootLogger.setUseParentHandlers(false);
097: Monolog.debug("Instanciate the root logger "
098: + rootLoggerName);
099: rootLoggerPrefix = rootLoggerName + '.';
100: } else {
101: rootLogger = new Logger(manager.getLogger(""));
102: }
103: }
104:
105: /**
106: * This constant is used to initialize the factory with the configure method
107: */
108: public final static String JAVALOG_CONFIGURATION = "javaLogConfiguration";
109:
110: /**
111: * This constant means that this java log system must be initialize with
112: * the default configuration
113: */
114: public final static String DEFAULT = "default";
115:
116: /**
117: * This constant means that this java log system must be initialize with
118: * a property file
119: */
120: public final static String PROPERTY = "property";
121:
122: /**
123: * This constant means that this java log system must be initialize with
124: * a xml file
125: */
126: public final static String CLASS = "class";
127:
128: /**
129: * This constant is the properties file name with wich the java log system
130: * must be initialized.
131: */
132: public final static String JAVALOG_CONFIGURATION_FILE = "javaLogConfigurationFile";
133:
134: /**
135: * This constant is the properties class name with wich the java log system
136: * must be initialized.
137: */
138: public final static String JAVALOG_CONFIGURATION_CLASS = "javaLogConfigurationClass";
139:
140: public LoggerFactory() {
141: super ();
142: }
143:
144: public String getWrapperName() {
145: return "javaLog";
146: }
147:
148: protected String[][] getDefaultHandlerType2className() {
149: return new String[][] {
150: { handlerTypes[0],
151: "org.objectweb.util.monolog.wrapper.javaLog.GenericHandler" },
152: { handlerTypes[1],
153: "org.objectweb.util.monolog.wrapper.javaLog.GenericHandler" },
154: { handlerTypes[2],
155: "org.objectweb.util.monolog.wrapper.javaLog.GenericHandler" },
156: { handlerTypes[3],
157: "org.objectweb.util.monolog.wrapper.javaLog.GenericHandler" },
158: { handlerTypes[4],
159: "org.objectweb.util.monolog.wrapper.javaLog.JMXGenericHandler" } };
160: }
161:
162: /**
163: * This method allocates org.objectweb.util.monolog.wrapper.javaLog.Logger
164: * objects whic are also java.util.logging.Logger and
165: * org.objectweb.util.monolog.api.Logger.
166: */
167: protected synchronized Logger getMonoLogger(String name,
168: String resName) {
169: if (name == null)
170: throw new IllegalArgumentException(
171: "The specified Logger name is null");
172: if (name.equals("root") || name.length() == 0) {
173: return rootLogger;
174: }
175: // isolates the logger hierarchy
176: name = monoLoggerName(name);
177: // Search if the logger already exist.
178: Object o = manager.getLogger(name);
179: if (o == null) {
180: // It doesn't exist => creates and adds it
181: Logger result = new Logger(name, resName);
182: manager.addLogger(result);
183: Monolog.debug("Instanciate the logger " + name);
184:
185: // In the javadoc of the LogManager, it is said that the caller
186: // need to keep a reference to this logger to avoid GC.
187: // Because LogManager could use some weak reference on it
188: loggers.add(result);
189: return result;
190: } else if (!o.getClass().equals(Logger.class)) {
191: throw new RuntimeException(
192: "Bad logger class (logger name: '" + name
193: + "'), expected: " + Logger.class
194: + ", found: " + o.getClass());
195: } else {
196: return (Logger) o;
197: }
198: }
199:
200: // IMPLEMENTATION OF THE Configurable INTERFACE //
201: //----------------------------------------------//
202:
203: /**
204: * This method permits to configure the factory.
205: * The properties parameter must contains the JAVALOG_CONFIGURATION property.
206: * Its value can be DEFAULT, PROPERTY or XML.
207: * In the PROPERTY case of the properties parameter must also contain the
208: * JAVALOG_CONFIGURATION_FILE property which the value is the configuration
209: * file name.
210: * In the CLASS case of the properties parameter must also contain the
211: * JAVALOG_CONFIGURATION_CLASS property which the value is the configuration
212: * class name which will initialized the java log system..
213: *
214: */
215: public void configure(Properties prop) throws Exception {
216: if (prop == null)
217: return;
218: String confMode = prop.getProperty(JAVALOG_CONFIGURATION, null);
219: if (confMode == null)
220: return;
221:
222: String param = null;
223: if (confMode.equals(PROPERTY)) {
224: param = prop.getProperty(JAVALOG_CONFIGURATION_FILE, null);
225: if (param != null)
226: System.setProperty("java.util.logging.config.file",
227: param);
228: manager.readConfiguration();
229: } else if (confMode.equals(CLASS)) {
230: param = prop.getProperty(JAVALOG_CONFIGURATION_CLASS, null);
231: if (param != null)
232: System.setProperty("java.util.logging.config.class",
233: param);
234: manager.readConfiguration();
235: }
236: }
237:
238: // IMPLEMENTATION OF LoggerFactory INTERFACE //
239: //-------------------------------------------//
240:
241: public org.objectweb.util.monolog.api.Logger getLogger(String key) {
242: return getMonoLogger(key, resourceBundleName);
243: }
244:
245: public org.objectweb.util.monolog.api.Logger getLogger(String key,
246: String rbn) {
247: return getMonoLogger(key, rbn);
248: }
249:
250: /**
251: * It retrieves a list of all loggers.
252: */
253: public org.objectweb.util.monolog.api.Logger[] getLoggers() {
254: ArrayList res = new ArrayList();
255: for (Enumeration e = manager.getLoggerNames(); e
256: .hasMoreElements();) {
257: Object o = manager.getLogger((String) e.nextElement());
258: if (o instanceof Logger) {
259: res.add(o);
260: }
261: }
262: return (Logger[]) res.toArray(new Logger[0]);
263: }
264: }
|