1 61 62 package org.logicalcobwebs.logging; 63 64 import org.logicalcobwebs.logging.impl.NoOpLog; 65 66 import java.lang.reflect.Constructor ; 67 import java.util.Hashtable ; 68 69 100 public class LogSource { 101 102 104 private static Hashtable logs = new Hashtable (); 105 106 107 private static boolean log4jIsAvailable = false; 108 109 110 private static boolean jdk14IsAvailable = false; 111 112 113 private static Constructor logImplctor = null; 114 115 116 118 static { 119 120 try { 122 if (null != Class.forName ("org.apache.log4j.Category")) { 123 log4jIsAvailable = true; 124 } else { 125 log4jIsAvailable = false; 126 } 127 } catch (Throwable t) { 128 log4jIsAvailable = false; 129 } 130 131 try { 133 if ((null != Class.forName ("java.util.logging.Logger")) 134 && (null != Class.forName ("org.logicalcobwebs.logging.impl.Jdk14Logger"))) { 135 jdk14IsAvailable = true; 136 } else { 137 jdk14IsAvailable = false; 138 } 139 } catch (Throwable t) { 140 jdk14IsAvailable = false; 141 } 142 143 String name = null; 145 try { 146 name = System.getProperty ("org.apache.commons.logging.log"); 147 if (name == null) { 148 name = System.getProperty ("org.logicalcobwebs.logging.Log"); 149 } 150 } catch (Throwable t) { 151 } 153 if (name != null) { 154 try { 155 setLogImplementation (name); 156 } catch (Throwable t) { 157 try { 158 setLogImplementation 159 ("org.logicalcobwebs.logging.impl.NoOpLog"); 160 } catch (Throwable u) { 161 ; 162 } 163 } 164 } else { 165 try { 166 if (log4jIsAvailable) { 167 setLogImplementation 168 ("org.logicalcobwebs.logging.impl.Log4JCategoryLog"); 169 } else if (jdk14IsAvailable) { 170 setLogImplementation 171 ("org.logicalcobwebs.logging.impl.Jdk14Logger"); 172 } else { 173 setLogImplementation 174 ("org.logicalcobwebs.logging.impl.NoOpLog"); 175 } 176 } catch (Throwable t) { 177 try { 178 setLogImplementation 179 ("org.logicalcobwebs.logging.impl.NoOpLog"); 180 } catch (Throwable u) { 181 ; 182 } 183 } 184 } 185 186 } 187 188 189 191 192 193 private LogSource () { 194 } 195 196 197 199 200 207 public static void setLogImplementation (String classname) throws 208 LinkageError , ExceptionInInitializerError , 209 SecurityException { 210 try { 211 Class logclass = Class.forName (classname); 212 Class [] argtypes = new Class [1]; 213 argtypes[0] = "".getClass (); 214 logImplctor = logclass.getConstructor (argtypes); 215 } catch (Throwable t) { 216 logImplctor = null; 217 } 218 } 219 220 226 public static void setLogImplementation (Class logclass) throws 227 LinkageError , ExceptionInInitializerError , 228 NoSuchMethodException , SecurityException { 229 Class [] argtypes = new Class [1]; 230 argtypes[0] = "".getClass (); 231 logImplctor = logclass.getConstructor (argtypes); 232 } 233 234 235 public static Log getInstance (String name) { 236 Log log = (Log) (logs.get (name)); 237 if (null == log) { 238 log = makeNewLogInstance (name); 239 logs.put (name, log); 240 } 241 return log; 242 } 243 244 245 public static Log getInstance (Class clazz) { 246 return getInstance (clazz.getName ()); 247 } 248 249 273 public static Log makeNewLogInstance (String name) { 274 275 Log log = null; 276 try { 277 Object [] args = new Object [1]; 278 args[0] = name; 279 log = (Log) (logImplctor.newInstance (args)); 280 } catch (Throwable t) { 281 log = null; 282 } 283 if (null == log) { 284 log = new NoOpLog (name); 285 } 286 return log; 287 288 } 289 290 294 public static String [] getLogNames () { 295 return (String []) (logs.keySet ().toArray (new String [logs.size ()])); 296 } 297 298 } 299 300 | Popular Tags |