1 23 24 package com.sun.enterprise.management.ext.logging; 25 26 import java.util.Set ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.List ; 30 import java.util.ArrayList ; 31 import java.util.Collections ; 32 import java.util.logging.Level ; 33 import java.lang.reflect.Method ; 34 35 import java.io.Serializable ; 36 37 import javax.management.NotificationListener ; 38 import javax.management.Notification ; 39 import javax.management.Attribute ; 40 import javax.management.ObjectName ; 41 import javax.management.ListenerNotFoundException ; 42 43 import com.sun.appserv.management.base.AMX; 44 import com.sun.appserv.management.base.Util; 45 import com.sun.appserv.management.base.XTypes; 46 import com.sun.appserv.management.base.QueryMgr; 47 48 import com.sun.appserv.management.j2ee.J2EEServer; 49 50 import com.sun.appserv.management.ext.logging.Logging; 51 import static com.sun.appserv.management.ext.logging.Logging.*; 52 import com.sun.appserv.management.ext.logging.LogModuleNames; 53 import com.sun.appserv.management.ext.logging.LogQueryResult; 54 import com.sun.appserv.management.ext.logging.LogQueryResultImpl; 55 import com.sun.appserv.management.ext.logging.LogQueryEntry; 56 57 import com.sun.appserv.management.util.misc.GSetUtil; 58 import com.sun.appserv.management.util.misc.StringUtil; 59 import com.sun.appserv.management.util.misc.CollectionUtil; 60 import com.sun.appserv.management.util.stringifier.CollectionStringifier; 61 62 63 import com.sun.enterprise.management.AMXTestBase; 64 import com.sun.enterprise.management.Capabilities; 65 66 67 70 public final class LoggingTest extends AMXTestBase 71 { 72 public 73 LoggingTest( ) 74 { 75 } 76 77 public static Capabilities 78 getCapabilities() 79 { 80 return getOfflineCapableCapabilities( false ); 81 } 82 83 final Set <Logging> 84 getAllLogging() 85 { 86 return getQueryMgr().queryJ2EETypeSet( XTypes.LOGGING ); 87 } 88 89 90 public void 91 testGetModuleLogLevel() 92 { 93 final long start = now(); 94 final Set <Logging> loggings = getAllLogging(); 95 96 for( final Logging logging : loggings ) 97 { 98 for( final String moduleName : LogModuleNames.ALL_NAMES ) 99 { 100 final String level = logging.getModuleLogLevel( moduleName ); 101 } 102 } 103 printElapsed( "testGetModuleLogLevel", loggings.size(), start ); 104 } 105 106 public void 107 testSetModuleLogLevel() 108 { 109 final long start = now(); 110 final Set <Logging> loggings = getAllLogging(); 111 112 for( final Logging logging : loggings ) 113 { 114 for( final String moduleName : LogModuleNames.ALL_NAMES ) 115 { 116 final String level = logging.getModuleLogLevel( moduleName ); 117 logging.setModuleLogLevel( moduleName, "FINEST" ); 118 logging.setModuleLogLevel( moduleName, level ); 119 } 120 } 121 printElapsed( "testSetModuleLogLevel", loggings.size(), start ); 122 } 123 124 public void 125 testGetLogFileNames() 126 { 127 final long start = now(); 128 final Set <Logging> loggings = getAllLogging(); 129 130 for( final Logging logging : loggings ) 131 { 132 final String [] serverLogs = logging.getLogFileNames( SERVER_KEY ); 133 assert( serverLogs.length != 0 ); 134 135 } 138 printElapsed( "testGetLogFileNames", loggings.size(), start ); 139 } 140 141 public void 142 testGetLogFileKeys() 143 { 144 final long start = now(); 145 final Set <Logging> loggings = getAllLogging(); 146 147 for( final Logging logging : loggings ) 148 { 149 final String [] keys = logging.getLogFileKeys(); 150 final Set <String > keysSet = GSetUtil.newSet( keys ); 151 assert( keysSet.contains( SERVER_KEY ) ); 152 assert( keysSet.contains( ACCESS_KEY ) ); 153 assertEquals( 2, keysSet.size() ); 154 } 155 printElapsed( "testGetLogFileKeys", loggings.size(), start ); 156 } 157 158 178 179 public void 180 testGetLogFile() 181 { 182 final long start = now(); 183 final Set <Logging> loggings = getAllLogging(); 184 185 for( final Logging logging : loggings ) 186 { 187 final String [] serverLogs = logging.getLogFileNames( SERVER_KEY ); 188 assert( serverLogs.length != 0 ); 189 190 for( final String name : serverLogs ) 191 { 192 final long startGet = now(); 193 final String contents = logging.getLogFile( SERVER_KEY, name ); 194 assert( contents != null && contents.length() != 0 ); 195 printVerbose( "Log file " + StringUtil.quote( name ) + 196 " has size " + (contents.length() / 1024 ) + "K, ms = " + (now() - startGet) ); 197 } 198 } 199 printElapsed( "testGetLogFile", loggings.size(), start ); 200 } 201 202 203 public void 204 testGetErrorInfo() 205 { 206 final long start = now(); 207 final Set <Logging> loggings = getAllLogging(); 208 209 for( final Logging logging : loggings ) 210 { 211 final Map <String ,Number >[] infos = logging.getErrorInfo(); 212 assert( infos != null ); 213 for( final Map <String ,Number > info : infos ) 214 { 215 assert( info != null && info.keySet().size() >= 3 ); 216 assert( info.containsKey( TIMESTAMP_KEY ) ); 217 assert( info.containsKey( SEVERE_COUNT_KEY ) ); 218 assert( info.containsKey( WARNING_COUNT_KEY ) ); 219 } 220 } 221 printElapsed( "testGetErrorInfo", loggings.size(), start ); 222 } 223 224 225 public void 226 testGetErrorDistribution() 227 { 228 final long start = now(); 229 final Set <Logging> loggings = getAllLogging(); 230 231 for( final Logging logging : loggings ) 232 { 233 final Map <String ,Number >[] infos = logging.getErrorInfo(); 234 assert( infos != null ); 235 for( final Map <String ,Number > info : infos ) 236 { 237 final Long timeStamp = (Long )info.get( TIMESTAMP_KEY ); 238 assert( timeStamp != null ); 239 final Integer severeCount = (Integer )info.get( SEVERE_COUNT_KEY ); 240 assert( severeCount != null ); 241 final Integer warningCount = (Integer )info.get( WARNING_COUNT_KEY ); 242 assert( warningCount != null ); 243 244 final Map <String ,Integer > warnings = 245 logging.getErrorDistribution( timeStamp.longValue(), "WARNING" ); 246 assert( warnings != null ); 247 for( final String moduleID : warnings.keySet() ) 248 { 249 assert( warnings.get( moduleID) != null ); 250 } 251 252 final Map <String ,Integer > severes = 253 logging.getErrorDistribution( timeStamp, "SEVERE" ); 254 assert( severes != null ); 255 for( final String moduleID : severes.keySet() ) 256 { 257 assert( severes.get( moduleID) != null ); 258 } 259 final String [] loggerNames = logging.getLoggerNames(); 261 final Set <String > illegal = GSetUtil.copySet( severes.keySet() ); 262 illegal.removeAll( GSetUtil.newSet( loggerNames ) ); 263 assert( illegal.size() == 0 ) : 264 "Illegal logger names found in Map returned by getErrorDistribution: {" + 265 CollectionUtil.toString( illegal, ",") + "}"; 266 267 268 try 269 { 270 logging.getErrorDistribution( timeStamp, "INFO" ); 271 fail( "expecting failure from getErrorDistribution( timeStamp, 'INFO' )" ); 272 } 273 catch( Exception e ) 274 { 275 } 276 } 277 } 278 printElapsed( "testGetErrorDistribution", loggings.size(), start ); 279 } 280 281 282 public void 283 testGetLoggerNamesAndGetLoggerNamesUnder() 284 { 285 final Set <Logging> loggings = getAllLogging(); 286 287 for( final Logging logging : loggings ) 288 { 289 final String [] names = logging.getLoggerNames(); 290 assert( names != null && names.length != 0 ); 291 292 for( final String loggerName : names ) 293 { 294 final String [] under = logging.getLoggerNamesUnder( loggerName ); 295 assert( under != null ); 296 } 297 } 298 299 } 300 301 public void 302 testQuery() 303 { 304 final long start = now(); 305 final Set <Logging> loggings = getAllLogging(); 306 307 for( final Logging logging : loggings ) 308 { 309 final String filename = Logging.MOST_RECENT_NAME; 310 final int startRecord = FIRST_RECORD; 311 final boolean searchForward = true; 312 final int requestedCount = ALL_RECORDS; 313 final Long fromTime = now() - (24 * 60 * 60 * 1000); 314 final Long toTime = now(); 315 final Set <String > modules = LogModuleNames.ALL_NAMES; final List <Attribute > attrs = null; 317 318 final LogQueryResult result = logging.queryServerLog( 319 filename, 320 startRecord, 321 searchForward, 322 requestedCount, 323 fromTime, 324 toTime, 325 Level.WARNING.toString(), 326 modules, 327 attrs ); 328 329 final String [] fieldNames = result.getFieldNames(); 330 331 for( final LogQueryEntry entry : result.getEntries() ) 332 { 333 final String messageID = entry.getMessageID(); 334 335 final String [] causes = logging.getDiagnosticCauses( messageID ); 336 337 final String [] checks = logging.getDiagnosticChecks( messageID ); 338 339 final String uri = logging.getDiagnosticURI( messageID ); 340 } 341 } 342 printElapsed( "testQuery", loggings.size(), start ); 343 } 344 345 346 private Map <String ,J2EEServer> 347 getAllJ2EEServer() 348 { 349 final Map <String ,J2EEServer> servers = 350 getDomainRoot().getJ2EEDomain().getJ2EEServerMap(); 351 352 return servers; 353 } 354 355 public void 356 testHaveLoggingForEachRunningServer() 357 { 358 final long start = now(); 359 360 final Map <String ,J2EEServer> servers = getAllJ2EEServer(); 361 for( final J2EEServer server : servers.values() ) 362 { 363 final int state = server.getstate(); 364 final String serverName = server.getName(); 365 if ( state != J2EEServer.STATE_RUNNING ) 366 { 367 warning( "testHaveLoggingForEachRunningServer: server " + 368 serverName + " is not running" ); 369 } 370 else 371 { 372 final Set <Logging> loggings = getAllLogging(); 373 final Set <ObjectName > loggingsObjectNames = Util.toObjectNames( loggings ); 374 final Map <String ,Logging> all = 375 Util.createNameMap( loggings ); 376 assert( all.containsKey( serverName ) ) : 377 "Can't find Logging for server " + serverName + 378 ", have: {" + 379 CollectionStringifier.toString( loggingsObjectNames, ", ") + "}"; 380 } 381 } 382 printElapsed( "testHaveLoggingForEachRunningServer", servers.size(), start ); 383 } 384 385 private final class MyListener implements NotificationListener 386 { 387 private final String mID; 388 389 public MyListener( final String id) 390 { 391 mNotifs = Collections.synchronizedList( new ArrayList <Notification >() ); 392 mID = id; 393 } 394 395 private final List <Notification > mNotifs; 396 397 public void 398 handleNotification( 399 final Notification notif, 400 final Object handback ) 401 { 402 404 if ( notif.getMessage().indexOf( mID ) >= 0 ) 405 { 406 mNotifs.add( notif ); 407 } 408 } 409 410 public List <Notification > 411 getNotifications() 412 { 413 return Collections.unmodifiableList( mNotifs ); 414 } 415 416 public int 417 getNotificationCount() 418 { 419 return mNotifs.size(); 420 } 421 } 422 423 424 private void 425 testListening( final Logging logging ) 426 throws ListenerNotFoundException 427 { 428 final String message= "testListening:" + now(); 429 430 final MyListener listener = new MyListener( message ); 431 432 printVerbose( "Testing Logging=" + logging.getName() ); 433 434 logging.addNotificationListener( listener, null, null); 435 try 436 { 437 logging.testEmitLogMessage( Level.SEVERE.toString(), message); 438 logging.testEmitLogMessage( Level.WARNING.toString(), message); 439 logging.testEmitLogMessage( Level.CONFIG.toString(), message); 440 logging.testEmitLogMessage( Level.INFO.toString(), message); 441 logging.testEmitLogMessage( Level.FINE.toString(), message); 442 logging.testEmitLogMessage( Level.FINER.toString(), message); 443 logging.testEmitLogMessage( Level.FINEST.toString(), message); 444 445 int count =0; 446 mySleep( 200 ); 447 while ( (count = listener.getNotificationCount()) != 7 ) 448 { 449 mySleep( 500 ); 450 trace( "testListening: waiting for 7 Notifications, have: " + count ); 451 } 452 } 453 finally 454 { 455 logging.removeNotificationListener( listener); 456 } 457 } 458 459 public void 460 testListeningForAllLogging() 461 throws ListenerNotFoundException 462 { 463 final long start = now(); 464 final Set <Logging> loggings = getAllLogging(); 465 466 for( final Logging logging : loggings ) 467 { 468 testListening( logging ); 469 } 470 printElapsed( "testListeningForAllLogging", loggings.size(), start ); 471 } 472 473 474 public void 475 testNoStrayLogging() 476 throws ListenerNotFoundException 477 { 478 final Set <String > validNames = getAllJ2EEServer().keySet(); 479 final Set <Logging> loggings = getAllLogging(); 480 481 assert( validNames.size() >= loggings.size() ); 482 483 for( final Logging logging : loggings ) 484 { 485 assert validNames.contains( logging.getName() ) : 486 "Logging MBean name doesn't match any J2EEServer: " + logging.getName(); 487 } 488 } 489 490 } 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 | Popular Tags |