001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1999-2004 Gerald Brose
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: *
020: */
021:
022: package org.jacorb.notification.util;
023:
024: import java.lang.reflect.Constructor;
025: import java.lang.reflect.InvocationTargetException;
026: import java.util.Properties;
027:
028: import org.apache.avalon.framework.configuration.ConfigurationException;
029: import org.apache.avalon.framework.logger.Logger;
030: import org.jacorb.config.Configuration;
031: import org.jacorb.config.JacORBConfiguration;
032: import org.jacorb.config.LogKitLoggerFactory;
033: import org.jacorb.config.LoggerFactory;
034: import org.jacorb.util.ObjectUtil;
035:
036: /**
037: * @author Alphonse Bendt
038: * @version $Id: LogUtil.java,v 1.4 2005/11/11 19:38:10 alphonse.bendt Exp $
039: */
040: public class LogUtil {
041: private static final LoggerFactory sLoggerFactory;
042:
043: static {
044: sLoggerFactory = newLoggerFactory();
045: }
046:
047: private static LoggerFactory newLoggerFactory() {
048: try {
049: Configuration config = JacORBConfiguration
050: .getConfiguration(new Properties(), null, false);
051:
052: LoggerFactory factory = newLog4jLoggerFactory(config);
053:
054: if (factory == null) {
055: factory = newLogKitFactory(config);
056: }
057:
058: if (factory == null) {
059: throw new RuntimeException();
060: }
061:
062: return factory;
063: } catch (ConfigurationException e) {
064: throw new RuntimeException(
065: "unable to create LoggerFactory for class "
066: + LogUtil.class.getName());
067: }
068: }
069:
070: private static LoggerFactory newLog4jLoggerFactory(
071: Configuration config) {
072: String clazzName = "org.jboss.util.Log4jLoggerFactory";
073:
074: try {
075: // see if Log4j is available
076: ObjectUtil.classForName("org.apache.log4j.Level");
077: Class clazz = ObjectUtil.classForName(clazzName);
078:
079: Constructor ctor = clazz.getConstructor(new Class[0]);
080:
081: final LoggerFactory factory = (LoggerFactory) ctor
082: .newInstance(new Object[0]);
083:
084: factory.configure(config);
085:
086: return factory;
087: } catch (IllegalArgumentException e) {
088: return null;
089: } catch (ClassNotFoundException e) {
090: return null;
091: } catch (SecurityException e) {
092: return null;
093: } catch (NoSuchMethodException e) {
094: return null;
095: } catch (InstantiationException e) {
096: return null;
097: } catch (IllegalAccessException e) {
098: return null;
099: } catch (InvocationTargetException e) {
100: return null;
101: } catch (ConfigurationException e) {
102: return null;
103: }
104: }
105:
106: private static LoggerFactory newLogKitFactory(Configuration config) {
107: try {
108: LogKitLoggerFactory loggerFactory = new LogKitLoggerFactory();
109: loggerFactory.configure(config);
110:
111: return loggerFactory;
112: } catch (ConfigurationException e) {
113: throw new RuntimeException();
114: }
115: }
116:
117: public static Logger getLogger(
118: org.apache.avalon.framework.configuration.Configuration config,
119: String name) {
120: try {
121: return ((org.jacorb.config.Configuration) config)
122: .getNamedLogger(name);
123: } catch (ClassCastException e) {
124: return getLogger(name);
125: }
126: }
127:
128: public static Logger getLogger(String name) {
129: return sLoggerFactory.getNamedLogger(name);
130: }
131: }
|