1 61 62 package org.logicalcobwebs.logging.impl; 63 64 import org.logicalcobwebs.logging.Log; 65 import org.logicalcobwebs.logging.LogConfigurationException; 66 import org.logicalcobwebs.logging.LogFactory; 67 68 import java.lang.reflect.Constructor ; 69 import java.lang.reflect.Method ; 70 import java.security.AccessController ; 71 import java.util.Enumeration ; 72 import java.util.Hashtable ; 73 import java.util.Vector ; 74 75 108 109 public class LogFactoryImpl extends LogFactory { 110 111 113 114 117 public LogFactoryImpl () { 118 super (); 119 guessConfig (); 120 } 121 122 123 125 126 132 public static final String LOG_DEFAULT = 133 "org.logicalcobwebs.logging.impl.SimpleLog"; 134 135 139 public static final String LOG_PROPERTY = 140 "org.logicalcobwebs.logging.Log"; 141 142 146 protected static final String LOG_PROPERTY_OLD = 147 "org.apache.commons.logging.log"; 148 149 150 152 153 156 private Hashtable attributes = new Hashtable (); 157 158 162 private Hashtable instances = new Hashtable (); 163 164 171 private Constructor logConstructor = null; 172 173 private LogFactory proxyFactory = null; 174 175 178 private Class logConstructorSignature[] = 179 {java.lang.String .class}; 180 181 185 private Method logMethod = null; 186 187 190 private Class logMethodSignature[] = 191 {LogFactory.class}; 192 193 194 196 197 203 public Object getAttribute (String name) { 204 if (proxyFactory != null) { 205 return proxyFactory.getAttribute (name); 206 } 207 208 return (attributes.get (name)); 209 210 } 211 212 217 public String [] getAttributeNames () { 218 if (proxyFactory != null) { 219 return proxyFactory.getAttributeNames (); 220 } 221 222 Vector names = new Vector (); 223 Enumeration keys = attributes.keys (); 224 while (keys.hasMoreElements ()) { 225 names.addElement (keys.nextElement ()); 226 } 227 String results[] = new String [names.size ()]; 228 for (int i = 0; i < results.length; i++) { 229 results[i] = (String ) names.elementAt (i); 230 } 231 return (results); 232 233 } 234 235 244 public Log getInstance (Class clazz) 245 throws LogConfigurationException { 246 if (proxyFactory != null) { 247 return proxyFactory.getInstance (clazz); 248 } 249 250 return (getInstance (clazz.getName ())); 251 252 } 253 254 271 public Log getInstance (String name) 272 throws LogConfigurationException { 273 if (proxyFactory != null) { 274 return proxyFactory.getInstance (name); 275 } 276 277 Log instance = (Log) instances.get (name); 278 if (instance == null) { 279 instance = newInstance (name); 280 instances.put (name, instance); 281 } 282 return (instance); 283 284 } 285 286 294 public void release () { 295 if (proxyFactory != null) { 296 proxyFactory.release (); 297 } 298 299 instances.clear (); 300 301 } 302 303 309 public void removeAttribute (String name) { 310 if (proxyFactory != null) { 311 proxyFactory.removeAttribute (name); 312 } 313 314 attributes.remove (name); 315 } 316 317 326 public void setAttribute (String name, Object value) { 327 if (proxyFactory != null) { 328 proxyFactory.setAttribute (name, value); 329 } 330 331 if (value == null) { 332 attributes.remove (name); 333 } else { 334 attributes.put (name, value); 335 } 336 337 } 338 339 340 342 343 355 protected Constructor getLogConstructor () 356 throws LogConfigurationException { 357 358 if (logConstructor != null) { 360 return (logConstructor); 361 } 362 363 String logClassName = null; 365 if (logClassName == null) { 366 logClassName = (String ) getAttribute (LOG_PROPERTY); 367 } 368 if (logClassName == null) { logClassName = (String ) getAttribute (LOG_PROPERTY_OLD); 370 } 371 if (logClassName == null) { 372 try { 373 logClassName = System.getProperty (LOG_PROPERTY); 374 } catch (SecurityException e) { 375 ; 376 } 377 } 378 if (logClassName == null) { try { 380 logClassName = System.getProperty (LOG_PROPERTY_OLD); 381 } catch (SecurityException e) { 382 ; 383 } 384 } 385 if ((logClassName == null) && isLog4JAvailable ()) { 386 logClassName = 387 "org.logicalcobwebs.logging.impl.Log4JCategoryLog"; 388 } 389 if ((logClassName == null) && isJdk14Available ()) { 390 logClassName = 391 "org.logicalcobwebs.logging.impl.Jdk14Logger"; 392 } 393 if (logClassName == null) { 394 logClassName = LOG_DEFAULT; 395 } 396 397 Class logClass = null; 399 try { 400 logClass = loadClass (logClassName); 401 if (logClass == null) { 402 throw new LogConfigurationException 403 ("No suitable Log implementation for " + logClassName); 404 } 405 if (!Log.class.isAssignableFrom (logClass)) { 406 throw new LogConfigurationException 407 ("Class " + logClassName + " does not implement Log"); 408 } 409 } catch (Throwable t) { 410 throw new LogConfigurationException (t); 411 } 412 413 try { 415 logMethod = logClass.getMethod ("setLogFactory", 416 logMethodSignature); 417 } catch (Throwable t) { 418 logMethod = null; 419 } 420 421 try { 423 logConstructor = logClass.getConstructor (logConstructorSignature); 424 return (logConstructor); 425 } catch (Throwable t) { 426 throw new LogConfigurationException ("No suitable Log constructor " 427 + logConstructorSignature + " for " + logClassName, t); 428 } 429 430 } 431 432 442 private static Class loadClass (final String name) 443 throws ClassNotFoundException { 444 ClassLoader threadCL = LogFactoryImpl.getContextClassLoader (); 445 Object result = AccessController.doPrivileged ( 446 new MyPrivilegedAction (threadCL, name)); 447 448 if (result instanceof Class ) { 449 return (Class ) result; 450 } 451 452 throw (ClassNotFoundException ) result; 453 } 454 455 protected void guessConfig () { 456 if (isLog4JAvailable ()) { 457 proxyFactory = null; 458 try { 459 Class proxyClass = 460 loadClass ("org.logicalcobwebs.logging.impl.Log4jFactory"); 461 if (proxyClass != null) { 462 proxyFactory = (LogFactory) proxyClass.newInstance (); 463 } 464 } catch (Throwable t) { 465 ; } 467 } 468 } 471 472 475 protected boolean isJdk14Available () { 476 477 try { 478 loadClass ("java.util.logging.Logger"); 479 loadClass ("org.logicalcobwebs.logging.impl.Jdk14Logger"); 480 return (true); 481 } catch (Throwable t) { 482 return (false); 483 } 484 485 } 486 487 490 protected boolean isLog4JAvailable () { 491 492 try { 493 loadClass ("org.apache.log4j.Category"); 494 loadClass ("org.logicalcobwebs.logging.impl.Log4JCategoryLog"); 495 return (true); 496 } catch (Throwable t) { 497 return (false); 498 } 499 500 } 501 502 511 protected Log newInstance (String name) 512 throws LogConfigurationException { 513 514 Log instance = null; 515 516 try { 517 Object params[] = new Object [1]; 518 params[0] = name; 519 instance = (Log) getLogConstructor ().newInstance (params); 520 if (logMethod != null) { 521 params[0] = this; 522 logMethod.invoke (instance, params); 523 } 524 return (instance); 525 } catch (Throwable t) { 526 throw new LogConfigurationException (t); 527 } 528 529 } 530 } 531 532 | Popular Tags |