1 16 17 package org.apache.commons.logging; 18 19 20 import java.lang.reflect.Constructor ; 21 import java.util.Hashtable ; 22 23 import org.apache.commons.logging.impl.NoOpLog; 24 25 26 57 public class LogSource { 58 59 61 static protected Hashtable logs = new Hashtable (); 62 63 64 static protected boolean log4jIsAvailable = false; 65 66 67 static protected boolean jdk14IsAvailable = false; 68 69 70 static protected Constructor logImplctor = null; 71 72 73 75 static { 76 77 try { 79 if (null != Class.forName("org.apache.log4j.Logger")) { 80 log4jIsAvailable = true; 81 } else { 82 log4jIsAvailable = false; 83 } 84 } catch (Throwable t) { 85 log4jIsAvailable = false; 86 } 87 88 try { 90 if ((null != Class.forName("java.util.logging.Logger")) && 91 (null != Class.forName("org.apache.commons.logging.impl.Jdk14Logger"))) { 92 jdk14IsAvailable = true; 93 } else { 94 jdk14IsAvailable = false; 95 } 96 } catch (Throwable t) { 97 jdk14IsAvailable = false; 98 } 99 100 String name = null; 102 try { 103 name = System.getProperty("org.apache.commons.logging.log"); 104 if (name == null) { 105 name = System.getProperty("org.apache.commons.logging.Log"); 106 } 107 } catch (Throwable t) { 108 } 109 if (name != null) { 110 try { 111 setLogImplementation(name); 112 } catch (Throwable t) { 113 try { 114 setLogImplementation 115 ("org.apache.commons.logging.impl.NoOpLog"); 116 } catch (Throwable u) { 117 ; 118 } 119 } 120 } else { 121 try { 122 if (log4jIsAvailable) { 123 setLogImplementation 124 ("org.apache.commons.logging.impl.Log4JLogger"); 125 } else if (jdk14IsAvailable) { 126 setLogImplementation 127 ("org.apache.commons.logging.impl.Jdk14Logger"); 128 } else { 129 setLogImplementation 130 ("org.apache.commons.logging.impl.NoOpLog"); 131 } 132 } catch (Throwable t) { 133 try { 134 setLogImplementation 135 ("org.apache.commons.logging.impl.NoOpLog"); 136 } catch (Throwable u) { 137 ; 138 } 139 } 140 } 141 142 } 143 144 145 147 148 149 private LogSource() { 150 } 151 152 153 155 156 163 static public void setLogImplementation(String classname) throws 164 LinkageError , ExceptionInInitializerError , 165 NoSuchMethodException , SecurityException , 166 ClassNotFoundException { 167 try { 168 Class logclass = Class.forName(classname); 169 Class [] argtypes = new Class [1]; 170 argtypes[0] = "".getClass(); 171 logImplctor = logclass.getConstructor(argtypes); 172 } catch (Throwable t) { 173 logImplctor = null; 174 } 175 } 176 177 178 184 static public void setLogImplementation(Class logclass) throws 185 LinkageError , ExceptionInInitializerError , 186 NoSuchMethodException , SecurityException { 187 Class [] argtypes = new Class [1]; 188 argtypes[0] = "".getClass(); 189 logImplctor = logclass.getConstructor(argtypes); 190 } 191 192 193 194 static public Log getInstance(String name) { 195 Log log = (Log) (logs.get(name)); 196 if (null == log) { 197 log = makeNewLogInstance(name); 198 logs.put(name, log); 199 } 200 return log; 201 } 202 203 204 205 static public Log getInstance(Class clazz) { 206 return getInstance(clazz.getName()); 207 } 208 209 210 234 static public Log makeNewLogInstance(String name) { 235 236 Log log = null; 237 try { 238 Object [] args = new Object [1]; 239 args[0] = name; 240 log = (Log) (logImplctor.newInstance(args)); 241 } catch (Throwable t) { 242 log = null; 243 } 244 if (null == log) { 245 log = new NoOpLog(name); 246 } 247 return log; 248 249 } 250 251 252 256 static public String [] getLogNames() { 257 return (String []) (logs.keySet().toArray(new String [logs.size()])); 258 } 259 260 261 } 262 | Popular Tags |