1 61 62 package org.logicalcobwebs.logging; 63 64 import java.io.BufferedReader ; 65 import java.io.IOException ; 66 import java.io.InputStream ; 67 import java.io.InputStreamReader ; 68 import java.lang.reflect.InvocationTargetException ; 69 import java.lang.reflect.Method ; 70 import java.security.AccessController ; 71 import java.security.PrivilegedAction ; 72 import java.util.Enumeration ; 73 import java.util.Hashtable ; 74 import java.util.Properties ; 75 76 89 90 public abstract class LogFactory { 91 92 94 95 99 public static final String FACTORY_PROPERTY = 100 "org.logicalcobwebs.logging.LogFactory"; 101 102 106 public static final String FACTORY_DEFAULT = 107 "org.logicalcobwebs.logging.impl.LogFactoryImpl"; 108 109 112 public static final String FACTORY_PROPERTIES = 113 "commons-logging.properties"; 114 115 119 protected static final String SERVICE_ID = 120 "META-INF/services/org.logicalcobwebs.logging.LogFactory"; 121 122 123 125 126 129 protected LogFactory () { 130 } 131 132 133 135 136 142 public abstract Object getAttribute (String name); 143 144 149 public abstract String [] getAttributeNames (); 150 151 160 public abstract Log getInstance (Class clazz) 161 throws LogConfigurationException; 162 163 180 public abstract Log getInstance (String name) 181 throws LogConfigurationException; 182 183 190 public abstract void release (); 191 192 198 public abstract void removeAttribute (String name); 199 200 209 public abstract void setAttribute (String name, Object value); 210 211 212 214 215 219 private static Hashtable factories = new Hashtable (); 220 221 222 224 225 249 public static LogFactory getFactory () throws LogConfigurationException { 250 251 ClassLoader contextClassLoader = 253 (ClassLoader ) AccessController.doPrivileged ( 254 new PrivilegedAction () { 255 public Object run () { 256 return getContextClassLoader (); 257 } 258 }); 259 260 LogFactory factory = getCachedFactory (contextClassLoader); 262 if (factory != null) { 263 return factory; 264 } 265 266 267 try { 269 String factoryClass = System.getProperty (FACTORY_PROPERTY); 270 if (factoryClass != null) { 271 factory = newFactory (factoryClass, contextClassLoader); 272 } 273 } catch (SecurityException e) { 274 ; } 276 277 283 if (factory == null) { 284 try { 285 InputStream is = (contextClassLoader == null 286 ? ClassLoader.getSystemResourceAsStream (SERVICE_ID) 287 : contextClassLoader.getResourceAsStream (SERVICE_ID)); 288 289 if (is != null) { 290 BufferedReader rd; 293 try { 294 rd = new BufferedReader (new InputStreamReader (is, "UTF-8")); 295 } catch (java.io.UnsupportedEncodingException e) { 296 rd = new BufferedReader (new InputStreamReader (is)); 297 } 298 299 String factoryClassName = rd.readLine (); 300 rd.close (); 301 302 if (factoryClassName != null && !"".equals (factoryClassName)) { 303 factory = newFactory (factoryClassName, contextClassLoader); 304 } 305 } 306 } catch (Exception ex) { 307 ; 308 } 309 } 310 311 Properties props = null; 312 313 321 try { 322 InputStream stream = (contextClassLoader == null 323 ? ClassLoader.getSystemResourceAsStream (FACTORY_PROPERTIES) 324 : contextClassLoader.getResourceAsStream (FACTORY_PROPERTIES)); 325 if (stream != null) { 326 props = new Properties (); 327 props.load (stream); 328 stream.close (); 329 String factoryClass = props.getProperty (FACTORY_PROPERTY); 330 if (factory == null) { 331 if (factoryClass == null) { 332 factoryClass = FACTORY_DEFAULT; 333 } 334 factory = newFactory (factoryClass, contextClassLoader); 335 } 336 } 337 } catch (IOException e) { 339 } catch (SecurityException e) { 341 } 343 344 if (factory == null) { 346 factory = newFactory (FACTORY_DEFAULT, LogFactory.class.getClassLoader ()); 347 } 348 349 if (factory != null) { 350 353 cacheFactory (contextClassLoader, factory); 354 } 355 356 if (props != null) { 357 Enumeration names = props.propertyNames (); 358 while (names.hasMoreElements ()) { 359 String name = (String ) names.nextElement (); 360 String value = props.getProperty (name); 361 factory.setAttribute (name, value); 362 } 363 } 364 365 return factory; 366 } 367 368 377 public static Log getLog (Class clazz) 378 throws LogConfigurationException { 379 380 return (getFactory ().getInstance (clazz)); 381 382 } 383 384 395 public static Log getLog (String name) 396 throws LogConfigurationException { 397 398 return (getFactory ().getInstance (name)); 399 400 } 401 402 410 public static void releaseAll () { 411 412 synchronized (factories) { 413 Enumeration elements = factories.elements (); 414 while (elements.hasMoreElements ()) { 415 LogFactory element = (LogFactory) elements.nextElement (); 416 element.release (); 417 } 418 factories.clear (); 419 } 420 421 } 422 423 424 426 427 437 protected static ClassLoader getContextClassLoader () 438 throws LogConfigurationException { 439 ClassLoader classLoader = null; 440 441 try { 442 Method method = Thread .class.getMethod ("getContextClassLoader", null); 444 445 try { 447 classLoader = (ClassLoader ) method.invoke (Thread.currentThread (), null); 448 } catch (IllegalAccessException e) { 449 throw new LogConfigurationException 450 ("Unexpected IllegalAccessException", e); 451 } catch (InvocationTargetException e) { 452 468 if (e.getTargetException () instanceof SecurityException ) { 469 ; } else { 471 throw new LogConfigurationException 474 ("Unexpected InvocationTargetException", e.getTargetException ()); 475 } 476 } 477 } catch (NoSuchMethodException e) { 478 classLoader = LogFactory.class.getClassLoader (); 480 } 481 482 return classLoader; 484 } 485 486 489 private static LogFactory getCachedFactory (ClassLoader contextClassLoader) { 490 LogFactory factory = null; 491 492 if (contextClassLoader != null) { 493 factory = (LogFactory) factories.get (contextClassLoader); 494 } 495 return factory; 496 } 497 498 private static void cacheFactory (ClassLoader classLoader, LogFactory factory) { 499 if (classLoader != null && factory != null) { 500 factories.put (classLoader, factory); 501 } 502 503 } 504 505 518 protected static LogFactory newFactory (String factoryClass, 519 ClassLoader classLoader) 520 throws LogConfigurationException { 521 522 try { 523 if (classLoader == null) { 524 classLoader = LogFactory.class.getClassLoader (); 525 } 526 Class clazz = null; 527 try { 528 clazz = classLoader.loadClass (factoryClass); 530 } catch (ClassNotFoundException ex) { 531 if (classLoader != LogFactory.class.getClassLoader ()) { 535 classLoader = LogFactory.class.getClassLoader (); 536 clazz = classLoader.loadClass (factoryClass); 537 } 538 } 539 540 LogFactory factory = (LogFactory) clazz.newInstance (); 541 542 return factory; 543 } catch (Exception e) { 544 throw new LogConfigurationException (e); 545 } 546 547 } 548 } 549 550 | Popular Tags |