1 2 24 package com.sun.enterprise.server.logging; 25 26 27 28 import java.util.logging.Logger ; 29 import java.util.logging.Level ; 30 import java.util.logging.Handler ; 31 import java.util.logging.Filter ; 32 import java.util.logging.ConsoleHandler ; 33 import java.util.logging.ErrorManager ; 34 35 import java.util.List ; 36 import java.util.Iterator ; 37 38 import com.sun.enterprise.server.ServerContext; 39 import com.sun.enterprise.server.ApplicationServer; 40 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 41 import com.sun.enterprise.config.serverbeans.Server; 42 import com.sun.enterprise.config.serverbeans.Config; 43 import com.sun.enterprise.config.serverbeans.ElementProperty; 44 import com.sun.enterprise.config.serverbeans.ModuleLogLevels; 45 import com.sun.enterprise.config.serverbeans.LogService; 46 47 import com.sun.logging.LogDomains; 48 49 50 60 public class ServerLogManager extends BaseLogManager { 61 62 private static FileandSyslogHandler handlerSingleton; 65 private static ConsoleHandler consoleHandler; 66 private static SystemLogHandler syslogHandler; 67 private static DeploymentAuditLogHandler deploymentAuditHandler; 68 69 private static boolean syslogLibraryLoadError = false; 73 74 private static Handler customHandler = null; 76 private static Filter customFilter = null; 77 78 79 private static ServerLogManager thisInstance; 80 81 private static List listOfUnInitializedLoggers = new java.util.ArrayList (); 82 83 private static Boolean verboseMode = null; 85 86 private static boolean customFilterError = false; 87 88 private static boolean customHandlerError = false; 89 90 private static final String SUN_OS = "SunOS"; 91 92 private static final String LINUX_OS = "Linux"; 93 94 private static final String OS_NAME_PROPERTY = "os.name"; 95 96 private Object lockObj = new Object (); 97 98 private static final String ORG_APACHE_CATALINA = "org.apache.catalina."; 99 private static final String ORG_APACHE_COYOTE = "org.apache.coyote."; 100 private static final String ORG_APACHE_JASPER = "org.apache.jasper."; 101 private static final String SYNCHRONIZATION = 102 "javax.ee.enterprise.system.tools.synchronization"; 103 104 public ServerLogManager() { 105 super(); 106 thisInstance = this; 107 } 108 109 114 protected void initializeLogger(Logger logger) { 115 synchronized(lockObj) { 116 internalInitializeLogger( logger ); 117 118 if( getLogService() == null ) { 119 listOfUnInitializedLoggers.add( logger ); 120 } 121 } 122 } 123 124 127 private void internalInitializeLogger( final Logger logger ) { 128 java.security.AccessController.doPrivileged( 129 new java.security.PrivilegedAction () { 130 public Object run() { 131 Handler [] h = logger.getHandlers(); 133 for (int i = 0; i < h.length; i++) { 134 logger.removeHandler(h[i]); 135 } 136 137 if (logger.getName().equals(LogDomains.DPLAUDIT_LOGGER)) { 138 logger.addHandler(getDeploymentAuditHandler()); 140 logger.setUseParentHandlers(false); 141 } 142 143 144 if( logger.getName().intern() == "".intern() ) { 145 146 logger.addHandler(getFileandSyslogHandler() ); 147 if ( logToStderr() ) { 148 logger.addHandler(getConsoleHandler()); 149 } 150 if( logToSyslog() ) { 151 Handler syslogHandler = getSyslogHandler(); 152 if( syslogHandler != null ) { 153 logger.addHandler( syslogHandler ); 154 } 155 } 156 logger.setUseParentHandlers( false ); 157 158 } 159 160 Level logLevel = getConfiguredLogLevel(logger.getName()); 161 if( logLevel != null ) { 162 logger.setLevel( logLevel ); 163 } 164 postInitializeLogger( logger ); 165 return null; 166 } 167 } 168 ); 169 } 170 171 174 private void postInitializeLogger( final Logger logger ) { 175 final Handler customHandler = getCustomHandler( ); 176 final Filter customFilter = getCustomFilter( ); 177 if( ( customHandler == null) 178 &&( customFilter == null ) ) { 179 return; 180 } 181 java.security.AccessController.doPrivileged( 182 new java.security.PrivilegedAction () { 183 public Object run() { 184 if( customHandler != null ) { 185 logger.addHandler( customHandler ); 186 } 187 if( customFilter != null ) { 188 logger.setFilter( customFilter ); 189 } 190 return null; 191 } 192 } 193 ); 194 } 195 196 197 201 public static void initializeServerLogger( Logger logger ) { 202 } 203 204 205 209 private static synchronized Handler getFileandSyslogHandler ( ) { 210 if( handlerSingleton == null ) { 211 try { 212 handlerSingleton = FileandSyslogHandler.getInstance(); 213 handlerSingleton.setLevel( Level.ALL ); 214 } catch( Exception e ) { 215 new ErrorManager ().error( "Exception caught in getHandler ", 216 e, ErrorManager.GENERIC_FAILURE ); 217 } 218 } 219 return handlerSingleton; 220 } 221 222 223 227 private static synchronized Handler getDeploymentAuditHandler( ) { 228 if( deploymentAuditHandler == null ) { 229 try { 230 deploymentAuditHandler = DeploymentAuditLogHandler.getInstance(); 231 deploymentAuditHandler.setLevel(DeploymentAuditLogHandler.getConfiguredLevel()); 232 } catch( Exception e ) { 233 new ErrorManager ().error( "Exception caught in getHandler ", 234 e, ErrorManager.GENERIC_FAILURE ); 235 } 236 } 237 return deploymentAuditHandler; 238 } 239 240 241 244 private static synchronized Handler getConsoleHandler() { 245 if( consoleHandler == null ) { 246 try { 247 consoleHandler = new ConsoleHandler (); 248 consoleHandler.setLevel(Level.ALL); 249 consoleHandler.setFormatter(new UniformLogFormatter()); 250 251 } catch( Exception e ) { 252 new ErrorManager ().error( 253 "Exception caught in getConsoleHandler ", 254 e, ErrorManager.GENERIC_FAILURE ); 255 } 256 } 257 return consoleHandler; 258 } 259 260 261 private static synchronized Handler getSyslogHandler() { 262 if( syslogLibraryLoadError ) return null; 263 if( syslogHandler == null ) { 264 try { 265 syslogHandler = new SystemLogHandler(); 266 syslogHandler.setLevel(Level.ALL); 267 syslogHandler.setFormatter(new UniformLogFormatter()); 268 269 } catch( Exception e ) { 270 syslogLibraryLoadError = true; 271 } 274 } 275 return syslogHandler; 276 } 277 278 281 private static synchronized Handler getCustomHandler( ) { 282 if( (customHandler != null ) 283 ||(customHandlerError) ) 284 { 285 return customHandler; 286 } 287 LogService logService = getLogService( ); 288 if( logService == null ) { 289 return null; 290 } 291 String customHandlerClassName = null; 292 try { 293 customHandlerClassName = logService.getLogHandler( ); 294 295 customHandler = (Handler ) getInstance( customHandlerClassName ); 296 if( customHandler != null ) { 299 customHandler.setFormatter( new UniformLogFormatter( ) ); 300 } 301 } catch( Exception e ) { 302 customHandlerError = true; 303 new ErrorManager ().error( "Error In Initializing Custom Handler " + 304 customHandlerClassName, e, ErrorManager.GENERIC_FAILURE ); 305 } 306 return customHandler; 307 } 308 309 312 private static Filter getCustomFilter( ) { 313 if(( customFilter != null ) 314 ||( customFilterError ) ) 315 { 316 return customFilter; 317 } 318 LogService logService = getLogService( ); 319 if( logService == null ) { 320 return null; 321 } 322 String customFilterClassName = null; 323 try { 324 customFilterClassName = logService.getLogFilter( ); 325 customFilter = (Filter ) getInstance( customFilterClassName ); 326 } catch( Exception e ) { 327 customFilterError = true; 328 new ErrorManager ().error( "Error In Instantiating Custom Filter " + 329 customFilterClassName, e, ErrorManager.GENERIC_FAILURE ); 330 } 331 return customFilter; 332 } 333 334 335 338 static LogService getLogService( ) { 339 try { 340 ServerContext sc = ApplicationServer.getServerContext(); 341 if( sc == null ) { 342 return null; 343 } 344 return ServerBeansFactory.getConfigBean( 345 sc.getConfigContext()).getLogService( ); 346 } catch( Exception e ) { 347 new ErrorManager ().error( "Error In getLogService ", e, 348 ErrorManager.GENERIC_FAILURE ); 349 } 350 return null; 351 } 352 353 354 357 private static Object getInstance( final String className ) { 358 if( className == null ) return null; 359 return java.security.AccessController.doPrivileged( 360 new java.security.PrivilegedAction () { 361 public Object run() { 362 try { 363 ClassLoader cl = 364 Thread.currentThread().getContextClassLoader(); 365 if (cl == null) 366 cl = ClassLoader.getSystemClassLoader(); 367 return Class.forName( className, true,cl).newInstance(); 368 } catch( Exception e ) { 369 new ErrorManager ().error( 370 "Error In Instantiating Class " + className, e, 371 ErrorManager.GENERIC_FAILURE ); 372 } 373 return null; 374 } 375 } 376 ); 377 } 378 379 380 private static boolean logToSyslog() { 382 boolean sunOS = 383 System.getProperty( OS_NAME_PROPERTY ).equals( SUN_OS ); 384 boolean linuxOS = 385 System.getProperty( OS_NAME_PROPERTY ).equals( LINUX_OS ); 386 387 if( !sunOS && !linuxOS ) { 389 return false; 390 } 391 if( syslogLibraryLoadError ) 392 return false; 393 LogService logService = getLogService( ); 394 if( logService != null ) { 395 return logService.isUseSystemLogging( ); 396 } 397 return false; 398 } 399 400 401 private static boolean logToStderr() { 403 try { 404 if ( verboseMode == null ) { 407 verboseMode = Boolean.FALSE; 408 String verbose = System.getProperty("com.sun.aas.verboseMode"); 409 if ( verbose != null && verbose.equals("true") ) { 410 verboseMode = Boolean.TRUE; 411 } 412 } 413 if ( verboseMode.booleanValue() == true ) { 414 return true; 415 } 416 417 ServerContext sc = ApplicationServer.getServerContext(); 419 if (sc == null) { 420 return false; 423 } 424 425 Config cfg = 426 ServerBeansFactory.getConfigBean(sc.getConfigContext()); 427 return cfg.getLogService().isLogToConsole(); 428 429 } catch ( Exception ex ) { 430 new ErrorManager ().error( 431 "Error while geting echo-log-messages-to-stderr attribute of " + 432 " log-service ", ex, ErrorManager.GENERIC_FAILURE ); 433 return false; 434 } 435 } 436 437 438 444 public static Level getConfiguredLogLevel(String loggerName) { 445 ServerContext sc = ApplicationServer.getServerContext(); 446 if (sc == null) { 447 if (loggerName.startsWith(SYNCHRONIZATION)) { 448 try { 449 String level = System.getProperty(SYNCHRONIZATION, "INFO"); 450 return Level.parse(level); 451 } catch (Exception e) { 452 return Level.INFO; 453 } 454 } else { 455 return Level.INFO; 456 } 457 } 458 459 if (loggerName.equals(LogDomains.DPLAUDIT_LOGGER)) { 460 try { 461 Level level = DeploymentAuditLogHandler.getConfiguredLevel(); 462 return level; 463 } catch (Throwable thr) { 464 return Level.OFF; 465 } 466 } 467 468 Level logLevel = null; 469 try { 470 Config cfg = 471 ServerBeansFactory.getConfigBean(sc.getConfigContext()); 472 ModuleLogLevels allModulesLogLevels = 473 cfg.getLogService().getModuleLogLevels( ); 474 if( allModulesLogLevels == null ) { return Level.INFO; } 479 if( allModulesLogLevels.getRoot( ).equals( "OFF" ) ) { 480 return Level.OFF; 481 } 482 483 ElementProperty[] elementProperties = 486 cfg.getLogService().getModuleLogLevels().getElementProperty( ); 487 488 if( elementProperties != null ) { 489 for( int i = 0; i < elementProperties.length; i++ ) { 490 if( elementProperties[i].getName().equals(loggerName) ) { 491 return Level.parse( elementProperties[i].getValue()); 492 } 493 } 494 } 495 496 String logModName = ModuleToLoggerNameMapper.getModuleName(loggerName); 497 if (logModName!=null) { 498 try { 499 String val = allModulesLogLevels.getAttributeValue(logModName); 500 logLevel = Level.parse(val); 501 } catch (Exception noSuch) { } 503 } 504 } catch ( Exception e ) { 505 new ErrorManager ().error( "Error In Setting Initial Loglevel", e, 506 ErrorManager.GENERIC_FAILURE ); 507 } 508 return logLevel; 509 } 510 511 512 516 public static void reInitializeServerLoggers() { 517 try { 518 FileandSyslogHandler handler= 519 (FileandSyslogHandler)getFileandSyslogHandler(); 520 handler.changeFileName( getLogService().getFile() ); 525 Long rotationTimeLimitValue = new Long ( 526 getLogService().getLogRotationTimelimitInMinutes().trim() ); 527 if( rotationTimeLimitValue.longValue( ) != 0 ) { 528 LogRotationTimer.getInstance().startTimer( 532 new LogRotationTimerTask( 533 rotationTimeLimitValue.longValue( ) ) ); 534 handler.setLimitForRotation( 0 ); 537 } else { 538 Integer rotationLimitAttrValue = new Integer ( 539 getLogService().getLogRotationLimitInBytes().trim() ); 540 handler.setLimitForRotation( 544 rotationLimitAttrValue.intValue() ); 545 } 546 547 if( listOfUnInitializedLoggers.size() == 0 ) { 548 return; 549 } 550 Iterator listIterator = listOfUnInitializedLoggers.iterator( ); 551 while( listIterator.hasNext( ) ) { 552 thisInstance.initializeLogger( (Logger ) listIterator.next() ); 553 } 554 } catch( Exception e ) { 555 new ErrorManager ().error( 556 "Exception caught in reInitializeServerLoggers ", 557 e, ErrorManager.GENERIC_FAILURE ); 558 } 559 } 560 } 561 | Popular Tags |