rwaldhoff 01/08/17 08:56:05 Modified: logging/src/java/org/apache/commons/logging LogSource.java Log: * add try/catch blocks in case the security restrictions in the VM are tight * add support for "propertyless' configuration of log factory * drop Iterator from the public interface of this class (using String[] instead) Revision Changes Path 1.5 +65 -13 jakarta-commons-sandbox/logging/src/java/org/apache/commons/logging/LogSource.java Index: LogSource.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/logging/src/java/org/apache/commons/logging/LogSource.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- LogSource.java 2001/08/15 21:23:23 1.4 +++ LogSource.java 2001/08/17 15:56:04 1.5 @@ -11,12 +11,13 @@ import java.util.HashMap; import java.lang.reflect.Constructor; import java.util.Iterator; +import java.lang.reflect.InvocationTargetException; /** * Factory for creating {@link Log} instances. * * @author Rod Waldhoff - * @version $Id: LogSource.java,v 1.4 2001/08/15 21:23:23 rwaldhoff Exp $ + * @version $Id: LogSource.java,v 1.5 2001/08/17 15:56:04 rwaldhoff Exp $ */ public class LogSource { static protected HashMap _logs = new HashMap(); @@ -30,12 +31,62 @@ } } catch(ClassNotFoundException e) { _log4jIsAvailable = false; + } catch(ExceptionInInitializerError e) { + _log4jIsAvailable = false; + } catch(LinkageError e) { + _log4jIsAvailable = false; + } + } + + static protected Constructor _logimplctor = null; + static { + try { + setLogImplementation(System.getProperty("org.apache.commons.logging.log","org.apache.commons.logging.NoOpLog")); + } catch(SecurityException e) { + _logimplctor = null; + } catch(LinkageError e) { + _logimplctor = null; + } catch(NoSuchMethodException e) { + _logimplctor = null; + } catch(ClassNotFoundException e) { + _logimplctor = null; } } private LogSource() { } + /** + * Set the log implementation/log implementation factory + * by the name of the class. The given class + * must implement {@link Log}, and provide a constructor that + * takes a single {@link String} argument (containing the name + * of the log). + */ + static public void setLogImplementation(String classname) throws + LinkageError, ExceptionInInitializerError, + NoSuchMethodException, SecurityException, + ClassNotFoundException { + Class logclass = Class.forName(classname); + Class[] argtypes = new Class[1]; + argtypes[0] = "".getClass(); + _logimplctor = logclass.getConstructor(argtypes); + } + + /** + * Set the log implementation/log implementation factory + * by class. The given class must implement {@link Log}, + * and provide a constructor that takes a single {@link String} + * argument (containing the name of the log). + */ + static public void setLogImplementation(Class logclass) throws + LinkageError, ExceptionInInitializerError, + NoSuchMethodException, SecurityException { + Class[] argtypes = new Class[1]; + argtypes[0] = "".getClass(); + _logimplctor = logclass.getConstructor(argtypes); + } + static public Log getInstance(String name) { Log log = (Log)(_logs.get(name)); if(null == log) { @@ -74,18 +125,19 @@ */ static public Log makeNewLogInstance(String name) { Log log = null; - String logclassname = - System.getProperty("org.apache.commoons.logging.log", - "org.apache.commons.logging.NoOpLog"); try { - Class logclass = Class.forName(logclassname); - Class[] argtypes = new Class[1]; - argtypes[0] = "".getClass(); - Constructor ctor = logclass.getConstructor(argtypes); Object[] args = new Object[1]; args[0] = name; - log = (Log)(ctor.newInstance(args)); - } catch(Exception e) { + log = (Log)(_logimplctor.newInstance(args)); + } catch (InstantiationException e) { + log = null; + } catch (IllegalAccessException e) { + log = null; + } catch (IllegalArgumentException e) { + log = null; + } catch (InvocationTargetException e) { + log = null; + } catch (NullPointerException e) { log = null; } if(null == log) { @@ -111,11 +163,11 @@ } /** - * Returns an iterator over the names of + * Returns a {@link String} array containing the names of * all logs known to me. */ - static public Iterator getLogNames() { - return _logs.keySet().iterator(); + static public String[] getLogNames() { + return (String[])(_logs.keySet().toArray(new String[_logs.size()])); } }