001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
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: */
018:
019: package org.mandarax.util.logging;
020:
021: /**
022: * A factory for loggers. Abstract class, uses the "Abstract Factory" pattern.
023: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
024: * @version 3.4 <7 March 05>
025: * @since 2.3.1
026: */
027: public abstract class LoggerFactory {
028: protected static LoggerFactory defaultInstance = null;
029: private Class provider = null;
030:
031: /**
032: * Get the default instance.
033: * @return a logger factory instance
034: */
035: public static LoggerFactory getDefaultFactory() {
036:
037: synchronized (LoggerFactory.class) {
038: if (defaultInstance == null) {
039: String className = System
040: .getProperty("org.mandarax.logger");
041: if (className != null)
042: getLoggerFactory(className).install();
043:
044: if (defaultInstance == null) {
045: if (isInClasspath("org.apache.log4j.Logger"))
046: (new Log4JLoggerFactory()).install();
047: else if (isInClasspath("java.util.logging.Logger"))
048: (new JDKLoggerFactory()).install();
049: else
050: (new SimpleLoggerFactory()).install();
051: }
052: }
053: }
054: return defaultInstance;
055: }
056:
057: /**
058: * Create (or get) a logger.
059: * @param loggerName The name of the logger.
060: */
061: public abstract Logger getLogger(String loggerName);
062:
063: /**
064: * Detect whether a class is in the class path.
065: * @param className a class name
066: * @return a boolean
067: */
068: protected static boolean isInClasspath(String className) {
069: try {
070: Class.forName(className);
071: return true;
072: } catch (ClassNotFoundException x) {
073: return false;
074: }
075: }
076:
077: /**
078: * Detect whether a class is in the class path, is instantiable and subclasses LoggerFactory.
079: * @param className a class name
080: * @return a logger factory if the class satisfies those conditions and null otherwise
081: */
082: protected static LoggerFactory getLoggerFactory(String className) {
083: try {
084: Class clazz = Class.forName(className);
085: Object inst = clazz.newInstance();
086: if (inst instanceof LoggerFactory)
087: return (LoggerFactory) inst;
088: else
089: return null;
090: } catch (Exception x) {
091: x.printStackTrace();
092: return null;
093: }
094: }
095:
096: /**
097: * Install an instance as default factory.
098: */
099: public void install() {
100: defaultInstance = this;
101: }
102:
103: }
|