1 23 package com.sun.enterprise.management.support; 24 25 import java.io.Serializable ; 26 27 import java.util.Set ; 28 import java.util.HashSet ; 29 import java.util.Map ; 30 import java.util.HashMap ; 31 import java.util.Collection ; 32 import java.util.Collections ; 33 34 import javax.management.MBeanInfo ; 35 import javax.management.MBeanAttributeInfo ; 36 import javax.management.MBeanOperationInfo ; 37 38 import com.sun.appserv.management.util.misc.StringUtil; 39 import com.sun.appserv.management.util.misc.CollectionUtil; 40 import com.sun.appserv.management.util.misc.ExceptionUtil; 41 import com.sun.appserv.management.util.misc.GSetUtil; 42 import com.sun.appserv.management.util.jmx.JMXUtil; 43 import com.sun.appserv.management.util.jmx.ReadWriteAttributeFilter; 44 45 46 50 public final class CoverageInfoImpl implements Serializable , CoverageInfo 51 { 52 public static final long serialVersionUID = 0xABCDEF; 53 54 private final Set <String > mAttributesRead; 55 private final Set <String > mAttributesWritten; 56 private final Set <String > mOperationsInvoked; 57 58 private final Map <String ,Integer > mAttributeGetFailures; 59 private final Map <String ,Integer > mAttributeSetFailures; 60 private final Map <String ,Integer > mUnknownAttributes; 61 private final Map <String ,Integer > mUnknownOperations; 62 private final Map <String ,Integer > mInvocationFailures; 63 64 private Set <String > mLegalReadableAttributes; 65 private Set <String > mLegalWriteableAttributes; 66 private Set <String > mLegalOperations; 67 68 private MBeanInfo mMBeanInfo; 69 70 public 71 CoverageInfoImpl( final MBeanInfo mbeanInfo ) 72 { 73 mLegalReadableAttributes = null; 74 mLegalWriteableAttributes = null; 75 mLegalOperations = null; 76 mMBeanInfo = mbeanInfo; 77 setMBeanInfo( mMBeanInfo ); 78 79 mAttributesRead = new HashSet <String >(); 80 mAttributesWritten = new HashSet <String >(); 81 mOperationsInvoked = new HashSet <String >(); 82 83 mAttributeGetFailures = new HashMap <String ,Integer >(); 84 mAttributeSetFailures = new HashMap <String ,Integer >(); 85 mUnknownAttributes = new HashMap <String ,Integer >(); 86 mUnknownOperations = new HashMap <String ,Integer >(); 87 mInvocationFailures = new HashMap <String ,Integer >(); 88 89 } 90 91 public void 92 clear() 93 { 94 mAttributesRead.clear(); 95 mAttributesWritten.clear(); 96 mOperationsInvoked.clear(); 97 98 mAttributeGetFailures.clear(); 99 mAttributeSetFailures.clear(); 100 mUnknownAttributes.clear(); 101 mUnknownOperations.clear(); 102 mInvocationFailures.clear(); 103 } 104 105 public MBeanInfo 106 getMBeanInfo() 107 { 108 return mMBeanInfo; 109 } 110 111 public void 112 setMBeanInfo( final MBeanInfo mbeanInfo ) 113 { 114 mLegalOperations = new HashSet <String >(); 115 mLegalReadableAttributes = new HashSet <String >(); 116 mLegalWriteableAttributes = new HashSet <String >(); 117 118 if ( mbeanInfo != null ) try 119 { 120 final MBeanOperationInfo [] ops = mbeanInfo.getOperations(); 121 for( final MBeanOperationInfo opInfo : ops ) 122 { 123 final String [] sig = JMXUtil.getSignature( opInfo.getSignature() ); 124 final String fullName = 125 getFullOperationName( opInfo.getName(), sig ); 126 mLegalOperations.add( fullName ); 127 } 128 mLegalOperations = Collections.unmodifiableSet( mLegalOperations ); 129 130 final MBeanAttributeInfo [] allAttrInfos = getMBeanInfo().getAttributes(); 131 132 final MBeanAttributeInfo [] readables = 133 JMXUtil.filterAttributeInfos( allAttrInfos, 134 ReadWriteAttributeFilter.READABLE_FILTER ); 135 136 final MBeanAttributeInfo [] writeables = 137 JMXUtil.filterAttributeInfos( allAttrInfos, 138 ReadWriteAttributeFilter.WRITEABLE_FILTER ); 139 140 mLegalReadableAttributes = 141 GSetUtil.newUnmodifiableStringSet( JMXUtil.getAttributeNames( readables ) ); 142 143 mLegalWriteableAttributes = 144 GSetUtil.newUnmodifiableStringSet( JMXUtil.getAttributeNames( writeables ) ); 145 } 146 catch( Exception e ) 147 { 148 System.out.println( ExceptionUtil.toString( e ) ); 149 throw new RuntimeException ( e ); 150 } 151 } 152 153 154 private void 155 mergeCounts( 156 final Map <String ,Integer > src, 157 final Map <String ,Integer > dest ) 158 { 159 for( final String key : src.keySet() ) 160 { 161 final Integer srcValue = src.get( key ); 162 final Integer destValue = dest.get( key ); 163 164 final int sum = srcValue.intValue() + 165 (destValue == null ? 0 : destValue.intValue()); 166 167 dest.put( key, new Integer ( sum ) ); 168 } 169 } 170 171 public void 172 merge( final CoverageInfo info ) 173 { 174 mAttributesRead.addAll( info.getAttributesRead() ); 175 mAttributesWritten.addAll( info.getAttributesWritten() ); 176 mOperationsInvoked.addAll( info.getOperationsInvoked() ); 177 178 mergeCounts( info.getAttributeGetFailures(), mAttributeGetFailures ); 179 mergeCounts( info.getAttributeSetFailures(), mAttributeSetFailures ); 180 mergeCounts( info.getUnknownAttributes(), mUnknownAttributes ); 181 mergeCounts( info.getUnknownOperations(), mUnknownOperations ); 182 mergeCounts( info.getInvocationFailures(), mInvocationFailures ); 183 } 184 185 public Set <String > 186 getAttributesRead() 187 { 188 return new HashSet <String >( mAttributesRead ); 189 } 190 191 public Set <String > 192 getAttributesNotRead() 193 { 194 checkHaveMBeanInfo(); 195 196 final Set <String > notRead = new HashSet <String >( mLegalReadableAttributes ); 198 notRead.removeAll( mAttributesRead ); 199 200 return notRead; 201 } 202 203 public Set <String > 204 getAttributesWritten() 205 { 206 return new HashSet <String >( mAttributesWritten ); 207 } 208 209 public Set <String > 210 getAttributesNotWritten() 211 { 212 checkHaveMBeanInfo(); 213 214 final Set <String > notWritten = new HashSet <String >( mLegalWriteableAttributes ); 216 notWritten.removeAll( mAttributesWritten ); 217 218 return notWritten; 219 } 220 221 public Set <String > 222 getOperationsInvoked() 223 { 224 return new HashSet <String >( mOperationsInvoked ); 225 } 226 227 public Set <String > 228 getOperationsNotInvoked() 229 { 230 checkHaveMBeanInfo(); 231 232 final Set <String > notInvoked = new HashSet <String >( mLegalOperations ); 234 notInvoked.removeAll( getOperationsInvoked() ); 235 236 return notInvoked; 237 } 238 239 private void 240 checkHaveMBeanInfo() 241 { 242 if ( getMBeanInfo() == null ) 243 { 244 throw new IllegalArgumentException ( 245 "MBeanInfo must be set using setMBeanInfo() prior to call" ); 246 } 247 } 248 249 public Map <String ,Integer > 250 getAttributeGetFailures() 251 { 252 return new HashMap <String ,Integer >( mAttributeGetFailures ); 253 } 254 255 public Map <String ,Integer > 256 getAttributeSetFailures() 257 { 258 return new HashMap <String ,Integer >( mAttributeSetFailures ); 259 } 260 261 public Map <String ,Integer > 262 getUnknownAttributes() 263 { 264 return new HashMap <String ,Integer >( mUnknownAttributes ); 265 } 266 267 268 public void 269 ignoreUnknownAttribute( final String name ) 270 { 271 mUnknownAttributes.remove( name ); 272 mAttributeGetFailures.remove( name ); 273 mAttributeSetFailures.remove( name ); 274 } 275 276 277 public Map <String ,Integer > 278 getUnknownOperations() 279 { 280 return new HashMap <String ,Integer >( mUnknownOperations ); 281 } 282 283 public Map <String ,Integer > 284 getInvocationFailures() 285 { 286 return new HashMap <String ,Integer >( mInvocationFailures ); 287 } 288 289 private void 290 unknownAttribute( final String name ) 291 { 292 Integer count = mUnknownAttributes.get( name ); 293 count = new Integer ( count == null ? 1 : 1 + count.intValue() ); 294 295 mUnknownAttributes.put( name, count ); 296 } 297 298 public void 299 attributeWasRead( final String name ) 300 { 301 if ( mLegalReadableAttributes.contains( name ) ) 302 { 303 mAttributesRead.add( name ); 304 } 305 else 306 { 307 unknownAttribute( name ); 308 } 309 } 310 311 public void 312 attributesWereRead( final String [] names ) 313 { 314 for( final String name : names ) 315 { 316 attributeWasRead( name ); 317 } 318 } 319 320 public void 321 attributeSetFailure( final String name ) 322 { 323 Integer count = mAttributeSetFailures.get( name ); 324 count = new Integer ( count == null ? 1 : 1 + count.intValue() ); 325 326 mAttributeSetFailures.put( name, count ); 327 } 328 329 330 public void 331 attributeGetFailure( final String name ) 332 { 333 Integer count = mAttributeGetFailures.get( name ); 334 count = new Integer ( count == null ? 1 : 1 + count.intValue() ); 335 336 mAttributeGetFailures.put( name, count ); 337 } 338 339 public void 340 attributeWasWritten( final String name ) 341 { 342 if ( mLegalWriteableAttributes.contains( name ) ) 343 { 344 mAttributesWritten.add( name ); 345 } 346 else 347 { 348 unknownAttribute( name ); 349 } 350 } 351 352 private String 353 getFullOperationName( 354 final String name, 355 final String [] sig) 356 { 357 final String sigString = StringUtil.toString( ",", sig == null ? EMPTY_SIG : sig ); 358 359 final String s = name + "(" + sigString + ")"; 360 361 return s; 362 } 363 364 private static final String [] EMPTY_SIG = new String [0]; 365 366 private void sdebug( final Object o ) 367 { 368 System.out.println( "" + o ); 369 } 370 public void 371 operationWasInvoked( 372 final String name, 373 final String [] sig) 374 { 375 final String fullName = getFullOperationName( name, sig ); 376 377 if ( mLegalOperations.contains( fullName ) ) 378 { 379 mOperationsInvoked.add( fullName ); 380 } 381 else 382 { 383 unknownOperation( fullName, sig ); 384 assert( ! mOperationsInvoked.contains( fullName ) ); 385 } 386 } 387 388 private void 389 unknownOperation( 390 final String name, 391 final String [] sig) 392 { 393 final String fullName = 394 getFullOperationName( name, sig ); 395 396 Integer count = mUnknownOperations.get( fullName ); 397 count = new Integer ( count == null ? 1 : 1 + count.intValue() ); 398 399 mUnknownOperations.put( fullName, count ); 400 } 401 402 public void 403 markAsInvoked(final String fullName ) 404 { 405 if ( (! fullName.endsWith( ")" )) || fullName.indexOf( "(" ) < 0 ) 406 { 407 throw new IllegalArgumentException ( fullName ); 408 } 409 410 mOperationsInvoked.add( fullName ); 411 } 412 413 public void 414 operationFailed( 415 final String name, 416 final String [] sig) 417 { 418 final String fullName = getFullOperationName( name, sig ); 419 if ( ! getOperations().contains( fullName ) ) 420 { 421 throw new IllegalArgumentException ( fullName ); 422 } 423 424 Integer count = mInvocationFailures.get( name ); 425 count = new Integer ( count == null ? 1 : 1 + count.intValue() ); 426 427 mInvocationFailures.put( fullName, count ); 428 } 429 430 431 public String 432 toString() 433 { 434 return toString( true ); 435 } 436 437 private String 438 toString( final Collection c) 439 { 440 return CollectionUtil.toString( c ); 441 } 442 443 private String 444 toString( final Collection c, final String sep ) 445 { 446 return CollectionUtil.toString( c, sep ); 447 } 448 449 public int 450 getNumReadableAttributes() 451 { 452 checkHaveMBeanInfo(); 453 454 return mLegalReadableAttributes.size(); 455 } 456 457 public Set <String > 458 getReadableAttributes() 459 { 460 return mLegalReadableAttributes; 461 } 462 463 public Set <String > 464 getWriteableAttributes() 465 { 466 return mLegalWriteableAttributes; 467 } 468 469 470 public Set <String > 471 getOperations() 472 { 473 return mLegalOperations; 474 } 475 476 public int 477 getNumWriteableAttributes() 478 { 479 checkHaveMBeanInfo(); 480 481 return mLegalWriteableAttributes.size(); 482 } 483 484 public int 485 getNumOperations() 486 { 487 return mLegalOperations.size(); 488 } 489 490 public int 491 getAttributeReadCoverage() 492 { 493 return percent( mAttributesRead.size(), mLegalReadableAttributes.size() ); 494 } 495 496 public int 497 getAttributeWriteCoverage() 498 { 499 return percent( mAttributesWritten.size(), getNumWriteableAttributes() ); 500 } 501 502 505 public int 506 getOperationCoverage() 507 { 508 checkHaveMBeanInfo(); 509 510 final int numOperations = mLegalOperations.size(); 511 512 final Set <String > remaining = new HashSet <String >( mLegalOperations ); 514 for( final String invoked : mOperationsInvoked ) 515 { 516 remaining.remove( invoked ); 517 } 518 519 final int numInvoked = numOperations - remaining.size(); 520 521 return percent( numInvoked, numOperations ); 522 } 523 524 public boolean 525 getFullCoverage() 526 { 527 return getAttributeReadCoverage() == 100 && 528 getAttributeWriteCoverage() == 100 && 529 getOperationCoverage() == 100; 530 } 531 532 private int 533 percent( final int numerator, final int denominator ) 534 { 535 return (int)((((float)numerator / (float)denominator))*100.0); 536 } 537 538 public String 539 toString( final boolean verbose ) 540 { 541 final String NEWLINE = System.getProperty( "line.separator" ); 542 final String INDENT = " "; 543 final String ITEM_SEP = NEWLINE + INDENT; 544 545 final StringBuilder b = new StringBuilder (); 546 547 b.append( "Attribute read coverage " + 548 getAttributesRead().size() + "/" + getNumReadableAttributes() + 549 " = " + getAttributeReadCoverage() + "%" + NEWLINE ); 550 551 b.append( "Attribute write coverage " + 552 getAttributesWritten().size() + "/" + getNumWriteableAttributes() + 553 " = " + getAttributeWriteCoverage() + "%" + NEWLINE ); 554 555 b.append( "Operation invocation coverage " + 556 getOperationsInvoked().size() + "/" + getNumOperations() + 557 " = " + getOperationCoverage() + "%" + NEWLINE ); 558 559 if ( verbose ) 560 { 561 b.append( mAttributesRead.size() + 562 " Attributes read: " + ITEM_SEP + 563 toString( getAttributesRead(), ITEM_SEP) + NEWLINE ); 564 565 b.append( mAttributesWritten.size() + 566 " Attributes written: " + ITEM_SEP + 567 toString( getAttributesWritten(), ITEM_SEP) + NEWLINE ); 568 569 b.append( mOperationsInvoked.size() + 570 " operations invoked: " + ITEM_SEP + 571 toString( getOperationsInvoked(), ITEM_SEP) + NEWLINE ); 572 } 573 574 if ( getMBeanInfo() != null ) 575 { 576 Set <String > not = null; 577 578 not = getAttributesNotRead(); 579 if ( not.size() != 0 || verbose ) 580 { 581 b.append( not.size() + 582 " Attributes NOT read:" + ITEM_SEP + toString( not, ITEM_SEP ) + NEWLINE ); 583 } 584 585 not = getAttributesNotWritten(); 586 if ( not.size() != 0 || verbose ) 587 { 588 b.append( not.size() + 589 " Attributes NOT written:" + ITEM_SEP + toString( not, ITEM_SEP) + NEWLINE ); 590 } 591 592 not = getOperationsNotInvoked(); 593 if ( not.size() != 0 || verbose ) 594 { 595 b.append( not.size() + 596 " operations NOT invoked:" + ITEM_SEP + 597 CollectionUtil.toString( not, ITEM_SEP ) + NEWLINE ); 598 } 599 } 600 else 601 { 602 b.append( "WARNING: MBeanInfo not supplied, " + 603 "can't emit Attributes/operations not read/written/invoked" + NEWLINE ); 604 } 605 606 Map <String ,Integer > failures = null; 607 608 failures = getAttributeGetFailures(); 609 if ( failures.size() != 0 || verbose ) 610 { 611 b.append( failures.keySet().size() + 612 " getAttribute failures: " + ITEM_SEP + 613 toString( failures.keySet(), ITEM_SEP) + NEWLINE ); 614 } 615 616 failures = getAttributeSetFailures(); 617 if ( failures.size() != 0 || verbose ) 618 { 619 b.append( failures.keySet().size() + 620 " setAttribute failures: " + ITEM_SEP + 621 toString( failures.keySet(), ITEM_SEP) + NEWLINE ); 622 } 623 624 failures = getUnknownAttributes(); 625 if ( failures.size() != 0 || verbose ) 626 { 627 b.append( failures.keySet().size() + 628 " unknown Attributes: " + ITEM_SEP + 629 toString( failures.keySet(), ITEM_SEP) + NEWLINE ); 630 } 631 632 failures = getUnknownOperations(); 633 if ( failures.size() != 0 || verbose ) 634 { 635 b.append( failures.keySet().size() + 636 " unknown operations: " + ITEM_SEP + 637 toString( failures.keySet(), ITEM_SEP) + NEWLINE ); 638 } 639 640 failures = getInvocationFailures(); 641 if ( failures.size() != 0 || verbose ) 642 { 643 b.append( failures.keySet().size() + 644 " invoke() failures: " + ITEM_SEP + 645 CollectionUtil.toString( failures.keySet(), ITEM_SEP) + NEWLINE ); 646 } 647 648 return b.toString(); 649 } 650 } 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 | Popular Tags |