1 23 package com.sun.appserv.management.util.jmx; 24 25 import java.io.IOException ; 26 import java.io.Serializable ; 27 28 import java.util.Set ; 29 import java.util.List ; 30 import java.util.Collection ; 31 import java.util.ArrayList ; 32 import java.util.HashSet ; 33 import java.util.Iterator ; 34 import java.util.ArrayList ; 35 import java.util.HashMap ; 36 import java.util.Map ; 37 import java.util.Arrays ; 38 import java.util.regex.Pattern ; 39 import java.util.Hashtable ; 40 41 import java.lang.reflect.Method ; 42 43 import javax.management.*; 44 45 import com.sun.appserv.management.util.stringifier.ArrayStringifier; 46 import com.sun.appserv.management.util.misc.ArrayUtil; 47 import com.sun.appserv.management.util.misc.GSetUtil; 48 import com.sun.appserv.management.util.misc.MapUtil; 49 import com.sun.appserv.management.util.misc.TypeCast; 50 import com.sun.appserv.management.util.misc.RegexUtil; 51 52 import com.sun.appserv.management.util.jmx.stringifier.MBeanOperationInfoStringifier; 53 import com.sun.appserv.management.util.jmx.stringifier.MBeanFeatureInfoStringifierOptions; 54 import com.sun.appserv.management.util.jmx.stringifier.MBeanAttributeInfoStringifier; 55 import com.sun.appserv.management.util.jmx.stringifier.ObjectNameStringifier; 56 import com.sun.appserv.management.util.misc.ArrayConversion; 57 58 import com.sun.appserv.management.util.stringifier.SmartStringifier; 59 60 61 62 64 public final class JMXUtil 65 { 66 67 public final static String MBEAN_SERVER_DELEGATE = 68 "JMImplementation:type=MBeanServerDelegate"; 69 70 public final static String MBEAN_SERVER_ID_ATTRIBUTE_NAME = 71 "MBeanServerId"; 72 73 77 public final static String WILD_PROP = ",*"; 78 79 83 public final static String WILD_ALL = "*"; 84 85 86 public static ObjectName 87 getMBeanServerDelegateObjectName() 88 { 89 return( newObjectName( "JMImplementation:type=MBeanServerDelegate" ) ); 90 } 91 92 public static void 93 listenToMBeanServerDelegate( 94 final MBeanServerConnection conn, 95 final NotificationListener listener, 96 final NotificationFilter filter, 97 final Object handback) 98 throws IOException , InstanceNotFoundException 99 { 100 conn.addNotificationListener( 101 getMBeanServerDelegateObjectName(), listener, filter, handback ); 102 } 103 104 public static String 105 getMBeanServerID( final MBeanServerConnection conn ) 106 throws IOException , 107 ReflectionException, InstanceNotFoundException, AttributeNotFoundException, 108 MBeanException 109 { 110 return( (String )conn.getAttribute( getMBeanServerDelegateObjectName(), 111 MBEAN_SERVER_ID_ATTRIBUTE_NAME ) ); 112 } 113 114 115 122 public static ObjectName 123 newObjectName( final String name ) 124 { 125 try 126 { 127 return( new ObjectName( name ) ); 128 } 129 catch( Exception e ) 130 { 131 throw new RuntimeException ( e.getMessage(), e ); 132 } 133 } 134 135 public static ObjectName 136 newObjectName( 137 final ObjectName objectName, 138 final String props ) 139 { 140 final String domain = objectName.getDomain(); 141 final String existingProps = objectName.getKeyPropertyListString(); 142 final String allProps = concatenateProps( existingProps, props ); 143 return( newObjectName( domain, allProps ) ); 144 } 145 146 public static ObjectName 147 newObjectName( 148 final String domain, 149 final String props ) 150 { 151 return( newObjectName( domain + ":" + props ) ); 152 } 153 154 160 public static ObjectName 161 newObjectNamePattern( 162 final String domain, 163 final String props ) 164 { 165 String actualProps = null; 166 167 if ( props.endsWith( JMXUtil.WILD_PROP ) || 168 props.equals( JMXUtil.WILD_ALL ) ) 169 { 170 actualProps = props; 171 } 172 else if ( props.length() == 0 ) 173 { 174 actualProps = "*"; 175 } 176 else 177 { 178 actualProps = props + WILD_PROP; 179 } 180 181 return( newObjectName( domain + ":" + actualProps ) ); 182 } 183 184 185 191 public static ObjectName 192 newObjectNamePattern( 193 final String domain, 194 final Map <String ,String > props ) 195 { 196 final String propsString = mapToProps( props ); 197 198 return( JMXUtil.newObjectNamePattern( domain, propsString ) ); 199 } 200 201 public static String 202 mapToProps( final Map <String ,String > propsMap ) 203 { 204 return( MapUtil.toString( propsMap, "," ) ); 205 } 206 207 public static ObjectName 208 removeProperty( 209 final ObjectName objectName, 210 final String key ) 211 { 212 ObjectName nameWithoutKey = objectName; 213 214 if ( objectName.getKeyProperty( key ) != null ) 215 { 216 final String domain = objectName.getDomain(); 217 final Hashtable <String ,String > props = 218 TypeCast.asHashtable( objectName.getKeyPropertyList() ); 219 220 props.remove( key ); 221 222 if ( objectName.isPropertyPattern() ) 223 { 224 final String propsString = mapToProps( props ); 225 226 nameWithoutKey = newObjectNamePattern( domain, 227 nameWithoutKey.getKeyPropertyListString() ); 228 } 229 else 230 { 231 try 232 { 233 nameWithoutKey = new ObjectName( domain, props ); 234 } 235 catch( Exception e ) 236 { 237 throw new RuntimeException ( e ); 238 } 239 } 240 } 241 242 return( nameWithoutKey ); 243 } 244 245 246 private 247 JMXUtil() 248 { 249 } 251 252 public static final String GET = "get"; 253 public static final String SET = "set"; 254 public static final String IS = "is"; 255 256 public static String 257 makeProp( String name, String value ) 258 { 259 return( name + "=" + value ); 260 } 261 262 public static String 263 concatenateProps( String props1, String props2 ) 264 { 265 String result = null; 266 267 if ( props1.length() == 0 ) 268 { 269 result = props2; 270 } 271 else if ( props2.length() == 0 ) 272 { 273 result = props1; 274 } 275 else 276 { 277 result = props1 + "," + props2; 278 } 279 280 return( result ); 281 } 282 283 public static String 284 concatenateProps( String props1, String props2, String props3 ) 285 { 286 return( concatenateProps( concatenateProps( props1, props2), props3) ); 287 } 288 289 290 291 297 public static ObjectName[] 298 objectNameSetToArray( final Set <ObjectName> objectNameSet ) 299 { 300 final ObjectName[] objectNames = new ObjectName[ objectNameSet.size() ]; 301 objectNameSet.toArray( objectNames ); 302 303 return( objectNames ); 304 } 305 306 307 312 public static String [] 313 getKeyProperty( String key, ObjectName[] objectNames ) 314 { 315 final String [] values = new String [ objectNames.length ]; 316 317 for( int i = 0; i < objectNames.length; ++i ) 318 { 319 values[ i ] = objectNames[ i ].getKeyProperty( key ); 320 } 321 322 return( values ); 323 } 324 325 330 public static String 331 getProp( 332 final ObjectName objectName, 333 final String key ) 334 { 335 final String value = objectName.getKeyProperty( key ); 336 if ( value == null ) 337 { 338 return( null ); 339 } 340 341 return( makeProp( key, value ) ); 342 } 343 344 public static String 345 getProps( 346 final ObjectName objectName, 347 final Set <String > propKeys ) 348 { 349 return( getProps( objectName, propKeys, false ) ); 350 } 351 352 public static String 353 getProps( 354 final ObjectName objectName, 355 final Set <String > propKeys, 356 final boolean ignoreMissing ) 357 { 358 String props = ""; 359 360 final Iterator iter = propKeys.iterator(); 361 while( iter.hasNext() ) 362 { 363 final String key = (String )iter.next(); 364 365 final String pair = getProp( objectName, key ); 366 if ( pair != null ) 367 { 368 props = concatenateProps( props, pair ); 369 } 370 else if ( ! ignoreMissing ) 371 { 372 throw new IllegalArgumentException ( 373 "key not found: " + key + " in " + objectName ); 374 } 375 } 376 return( props ); 377 } 378 379 384 public static String [] 385 getKeyProperty( String key, Set <ObjectName> objectNameSet ) 386 { 387 final ObjectName[] objectNames = 388 JMXUtil.objectNameSetToArray( objectNameSet ); 389 390 return( getKeyProperty( key, objectNames ) ); 391 } 392 393 394 399 public static Set <String > 400 getKeyPropertySet( String key, Set <ObjectName> objectNameSet ) 401 { 402 final ObjectName[] objectNames = 403 JMXUtil.objectNameSetToArray( objectNameSet ); 404 405 final String [] values = getKeyProperty( key, objectNames ); 406 407 return( ArrayConversion.arrayToSet( values ) ); 408 } 409 410 417 public static String 418 findKey( 419 final Set <String > candidateKeys, 420 final ObjectName objectName ) 421 { 422 final Iterator iter = candidateKeys.iterator(); 423 424 String match = null; 425 426 while ( iter.hasNext() ) 427 { 428 final String key = (String )iter.next(); 429 430 if ( objectName.getKeyProperty( key ) != null ) 431 { 432 match = key; 433 break; 434 } 435 } 436 437 return( match ); 438 } 439 440 441 449 public static Set <ObjectName> 450 findByProperty( 451 final Set <ObjectName> objectNames, 452 final String propertyKey, 453 final String propertyValue ) 454 { 455 final Set <ObjectName> result = new HashSet <ObjectName>(); 456 457 final Iterator iter = objectNames.iterator(); 458 while ( iter.hasNext() ) 459 { 460 final ObjectName objectName = (ObjectName)iter.next(); 461 462 final String value = objectName.getKeyProperty( propertyKey ); 463 if ( propertyValue.equals( value ) ) 464 { 465 result.add( objectName ); 466 } 467 } 468 469 return( result ); 470 } 471 472 475 public static ObjectName 476 setKeyProperty( final ObjectName objectName, final String key, final String value ) 477 { 478 final String domain = objectName.getDomain(); 479 final Hashtable <String ,String > props = TypeCast.asHashtable( objectName.getKeyPropertyList() ); 480 481 props.put( key, value ); 482 483 ObjectName newObjectName = null; 484 try 485 { 486 newObjectName = new ObjectName( domain, props ); 487 } 488 catch( MalformedObjectNameException e ) 489 { 490 throw new RuntimeException ( e ); 491 } 492 493 return( newObjectName ); 494 } 495 496 private static String 497 toString( Object o ) 498 { 499 return( SmartStringifier.toString( o ) ); 500 } 501 502 public static void 503 unregisterAll( final MBeanServerConnection conn, final Set <ObjectName> allNames) 504 throws IOException , MalformedObjectNameException, MBeanRegistrationException 505 { 506 for( final ObjectName name : allNames ) 507 { 508 try 509 { 510 conn.unregisterMBean( name ); 511 } 512 catch( Exception e ) 513 { 514 } 516 } 517 } 518 519 public static void 520 unregisterAll( final MBeanServerConnection conn ) 521 throws IOException , MalformedObjectNameException, MBeanRegistrationException 522 { 523 unregisterAll( conn, queryNames( conn, new ObjectName( "*:*" ), null ) ); 524 } 525 526 public static String [] 527 getAllAttributeNames( 528 final MBeanServerConnection conn, 529 final ObjectName objectName ) 530 throws IOException , 531 ReflectionException, IntrospectionException, InstanceNotFoundException 532 { 533 return( getAttributeNames( getAttributeInfos( conn, objectName ) ) ); 534 } 535 536 public static MBeanAttributeInfo[] 537 filterAttributeInfos( 538 final MBeanAttributeInfo[] infos, 539 final AttributeFilter filter ) 540 { 541 final ArrayList <MBeanAttributeInfo> matches = new ArrayList <MBeanAttributeInfo>(); 542 for( int i = 0; i < infos.length; ++i ) 543 { 544 if ( filter.filterAttribute( infos[ i ] ) ) 545 { 546 matches.add( infos[ i ] ); 547 } 548 } 549 550 final MBeanAttributeInfo[] results = new MBeanAttributeInfo[ matches.size() ]; 551 matches.toArray( results ); 552 553 return( results ); 554 } 555 556 561 public static String [] 562 getAttributeNames( final MBeanAttributeInfo[] infos ) 563 { 564 final String [] names = new String [ infos.length ]; 565 566 for( int i = 0; i < infos.length; ++i ) 567 { 568 names[ i ] = infos[ i ].getName(); 569 } 570 571 return( names ); 572 } 573 574 578 public static MBeanAttributeInfo 579 getMBeanAttributeInfo( 580 final MBeanAttributeInfo[] infos, 581 final String attrName ) 582 { 583 MBeanAttributeInfo info = null; 584 585 for( int i = 0; i < infos.length; ++i ) 586 { 587 if ( infos[ i ].getName().equals( attrName ) ) 588 { 589 info = infos[ i ]; 590 break; 591 } 592 } 593 594 return( info ); 595 } 596 597 601 public static MBeanAttributeInfo 602 getMBeanAttributeInfo( 603 final MBeanInfo mbeanInfo, 604 final String attrName ) 605 { 606 return( getMBeanAttributeInfo( mbeanInfo.getAttributes(), attrName ) ); 607 } 608 609 610 614 public static MBeanAttributeInfo [] 615 getAttributeInfos( 616 final MBeanServerConnection conn, 617 final ObjectName objectName ) 618 throws IOException , 619 ReflectionException, IntrospectionException, InstanceNotFoundException 620 { 621 final MBeanAttributeInfo [] infos = conn.getMBeanInfo( objectName ).getAttributes(); 622 623 return( infos ); 624 } 625 626 627 633 public static Map <String ,Attribute> 634 attributeListToAttributeMap( final AttributeList attrs ) 635 { 636 final HashMap <String ,Attribute> map = new HashMap <String ,Attribute>(); 637 638 for( int i = 0; i < attrs.size(); ++i ) 639 { 640 final Attribute attr = (Attribute)attrs.get( i ); 641 642 map.put( attr.getName(), attr ); 643 } 644 645 return( map ); 646 } 647 648 654 public static Map <String ,Object > 655 attributeListToValueMap( final AttributeList attrs ) 656 { 657 final Map <String ,Object > map = new HashMap <String ,Object >(); 658 659 for( int i = 0; i < attrs.size(); ++i ) 660 { 661 final Attribute attr = (Attribute)attrs.get( i ); 662 663 final Object value = attr.getValue(); 664 665 map.put( attr.getName(), value ); 666 } 667 668 return( map ); 669 } 670 671 677 public static Map <String ,String > 678 attributeListToStringMap( final AttributeList attrs ) 679 { 680 final Map <String ,String > map = new HashMap <String ,String >(); 681 682 for( int i = 0; i < attrs.size(); ++i ) 683 { 684 final Attribute attr = (Attribute)attrs.get( i ); 685 686 final Object value = attr.getValue(); 687 final String s = (String )(value == null ? value : "" + value); 688 map.put( attr.getName(), s ); 689 } 690 691 return( map ); 692 } 693 694 695 696 702 public static Map <String ,MBeanAttributeInfo> 703 attributeInfosToMap( final MBeanAttributeInfo[] attrInfos ) 704 { 705 final Map <String ,MBeanAttributeInfo> map = new HashMap <String ,MBeanAttributeInfo>(); 706 707 for( int i = 0; i < attrInfos.length; ++i ) 708 { 709 final MBeanAttributeInfo attrInfo = attrInfos[ i ]; 710 711 map.put( attrInfo.getName(), attrInfo ); 712 } 713 714 return( map ); 715 } 716 717 718 public static MBeanInfo 719 removeAttributes( 720 final MBeanInfo origInfo, 721 final String [] attributeNames ) 722 { 723 MBeanInfo result = origInfo; 724 725 if ( attributeNames.length != 0 ) 726 { 727 final Map <String ,MBeanAttributeInfo> infos = 728 JMXUtil.attributeInfosToMap( origInfo.getAttributes() ); 729 730 for( int i = 0; i < attributeNames.length; ++i ) 731 { 732 infos.remove( attributeNames[ i ] ); 733 } 734 735 final MBeanAttributeInfo[] newInfos = new MBeanAttributeInfo[ infos.keySet().size() ]; 736 infos.values().toArray( newInfos ); 737 738 result = new MBeanInfo( 739 origInfo.getClassName(), 740 origInfo.getDescription(), 741 newInfos, 742 origInfo.getConstructors(), 743 origInfo.getOperations(), 744 origInfo.getNotifications() ); 745 } 746 747 return( result ); 748 } 749 750 758 public static Set <MBeanFeatureInfo> 759 findInfoByName( 760 final MBeanFeatureInfo[] infos, 761 final String name ) 762 { 763 final Set <MBeanFeatureInfo> s = new HashSet <MBeanFeatureInfo>(); 764 765 for( int i = 0; i < infos.length; ++i ) 766 { 767 final MBeanFeatureInfo info = infos[ i ]; 768 769 if ( info.getName().equals( name ) ) 770 { 771 s.add( info ); 772 } 773 } 774 775 return( s ); 776 } 777 778 779 780 786 public static AttributeList 787 mapToAttributeList( final Map <String ,Object > m ) 788 { 789 final AttributeList attrList = new AttributeList(); 790 791 for( final String key : m.keySet() ) 792 { 793 final Object value = m.get( key ); 794 795 final Attribute attr = new Attribute( key, value ); 796 797 attrList.add( attr); 798 } 799 800 return( attrList ); 801 } 802 803 804 809 public static List <String > 810 objectNamesToStrings( final Collection <ObjectName> objectNames ) 811 { 812 final List <String > result = new ArrayList <String >(); 814 815 for( final ObjectName objectName : objectNames ) 816 { 817 result.add( ObjectNameStringifier.DEFAULT.stringify( objectName ) ); 818 } 819 820 return( result ); 821 } 822 823 826 public static String [] 827 objectNamesToStrings( final ObjectName[] objectNames ) 828 { 829 final String [] strings = new String [ objectNames.length ]; 830 831 for( int i = 0; i < strings.length; ++i ) 832 { 833 strings[ i ] = objectNames[ i ].toString(); 834 } 835 836 return( strings ); 837 } 838 839 private static boolean 840 connectionIsDead( final MBeanServerConnection conn ) 841 { 842 boolean isDead = false; 843 844 try 846 { 847 conn.isRegistered( new ObjectName( MBEAN_SERVER_DELEGATE ) ); 848 } 849 catch( MalformedObjectNameException e ) 850 { 851 assert( false ); 852 } 853 catch( IOException e ) 854 { 855 isDead = true; 856 } 857 858 return( isDead ); 859 } 860 861 private static AttributeList 862 getAttributesSingly( 863 MBeanServerConnection conn, 864 ObjectName objectName, 865 String [] attrNames, 866 Set <String > problemNames ) 867 throws InstanceNotFoundException 868 { 869 AttributeList attrs = new AttributeList(); 870 871 for( int i = 0; i < attrNames.length; ++i ) 872 { 873 final String name = attrNames[ i ]; 874 875 try 876 { 877 final Object value = conn.getAttribute( objectName, name ); 878 879 attrs.add( new Attribute( name, value ) ); 880 } 881 catch( Exception e ) 882 { 883 if ( e instanceof InstanceNotFoundException ) 886 { 887 throw (InstanceNotFoundException)e; 888 } 889 890 if ( problemNames != null ) 891 { 892 problemNames.add( name ); 893 } 894 } 895 } 896 897 return( attrs ); 898 } 899 900 901 911 public static AttributeList 912 getAttributesRobust( 913 MBeanServerConnection conn, 914 ObjectName objectName, 915 String [] attrNames, 916 Set <String > problemNames ) 917 throws InstanceNotFoundException, IOException 918 { 919 AttributeList attrs = null; 920 921 if ( problemNames != null ) 922 { 923 problemNames.clear(); 924 } 925 926 try 927 { 928 attrs = conn.getAttributes( objectName, attrNames ); 929 if ( attrs == null ) 930 { 931 attrs = new AttributeList(); 932 } 933 } 934 catch( InstanceNotFoundException e ) 935 { 936 throw e; 938 } 939 catch( IOException e ) 940 { 941 if ( connectionIsDead( conn ) ) 942 { 943 throw e; 944 } 945 946 948 attrs = getAttributesSingly( conn, objectName, attrNames, problemNames ); 949 } 950 catch( Exception e ) 951 { 952 attrs = getAttributesSingly( conn, objectName, attrNames, problemNames ); 953 } 954 955 return( attrs ); 956 } 957 958 959 963 boolean 964 sameAttributes( MBeanAttributeInfo[] infos1, MBeanAttributeInfo[] infos2 ) 965 { 966 boolean equal = false; 967 968 if( infos1.length == infos2.length ) 969 { 970 equal = ArrayUtil.arraysEqual( infos1, infos2 ); 971 if ( ! equal ) 972 { 973 Arrays.sort( infos1, MBeanAttributeInfoComparator.INSTANCE ); 975 Arrays.sort( infos2, MBeanAttributeInfoComparator.INSTANCE ); 976 977 equal = true; for( int i = 0; i < infos1.length; ++i ) 979 { 980 if ( ! infos1[ i ].equals( infos2[ i ] ) ) 981 { 982 equal = false; 983 break; 984 } 985 } 986 } 987 else 988 { 989 equal = true; 990 } 991 } 992 return( equal ); 993 } 994 995 996 1000 boolean 1001 sameOperations( final MBeanOperationInfo[] infos1, final MBeanOperationInfo[] infos2 ) 1002 { 1003 boolean equal = false; 1004 1005 if ( infos1.length == infos2.length ) 1006 { 1007 equal = ArrayUtil.arraysEqual( infos1, infos2 ); 1009 if ( ! equal ) 1010 { 1011 Arrays.sort( infos1, MBeanOperationInfoComparator.INSTANCE ); 1013 Arrays.sort( infos2, MBeanOperationInfoComparator.INSTANCE ); 1014 1015 equal = true; for( int i = 0; i < infos1.length; ++i ) 1017 { 1018 if ( ! infos1[ i ].equals( infos2[ i ] ) ) 1019 { 1020 equal = false; 1021 break; 1022 } 1023 } 1024 } 1025 } 1026 return( equal ); 1027 } 1028 1029 1034 boolean 1035 sameInterface( MBeanInfo info1, MBeanInfo info2 ) 1036 { 1037 return( sameAttributes( info1.getAttributes(), info2.getAttributes() ) && 1038 sameOperations( info1.getOperations(), info2.getOperations() ) ); 1039 } 1040 1041 public static boolean 1042 isIs( final Method method ) 1043 { 1044 return( method.getName().startsWith( IS ) && method.getParameterTypes().length == 0 ); 1045 } 1046 1047 1051 public static boolean 1052 isGetter( Method method ) 1053 { 1054 return( method.getName().startsWith( GET ) && method.getParameterTypes().length == 0 ); 1055 } 1056 1057 1058 public static boolean 1059 isGetter( final MBeanOperationInfo info ) 1060 { 1061 return ( info.getName().startsWith( GET ) && 1062 info.getSignature().length == 0 && 1063 ! info.getReturnType().equals( "void" ) ); 1064 } 1065 1066 1067 public static MBeanOperationInfo[] 1068 findOperations( 1069 final MBeanOperationInfo[] operations, 1070 final String operationName) 1071 { 1072 final Set <MBeanOperationInfo> items = new HashSet <MBeanOperationInfo>(); 1073 for( int i = 0; i < operations.length; ++i ) 1074 { 1075 if ( operations[ i ].getName().equals( operationName ) ) 1076 { 1077 items.add( operations[ i ] ); 1078 } 1079 } 1080 1081 final MBeanOperationInfo[] itemsArray = new MBeanOperationInfo[ items.size() ]; 1082 items.toArray( itemsArray ); 1083 return itemsArray; 1084 } 1085 1086 public static MBeanOperationInfo 1087 findOperation( 1088 final MBeanOperationInfo[] operations, 1089 final String operationName, 1090 final String [] types ) 1091 { 1092 MBeanOperationInfo result = null; 1093 1094 for( int i = 0; i < operations.length; ++i ) 1095 { 1096 final MBeanOperationInfo info = operations[ i ]; 1097 1098 if ( info.getName().equals( operationName ) ) 1099 { 1100 final MBeanParameterInfo[] sig = info.getSignature(); 1101 1102 if ( sig.length == types.length ) 1103 { 1104 result = info; for( int j = 0; j < sig.length; ++j ) 1106 { 1107 if ( ! types[ j ].equals( sig[ j ].getType() ) ) 1108 { 1109 result = null; break; 1111 } 1112 } 1113 } 1114 } 1115 } 1116 1117 return( result ); 1118 } 1119 1120 1121 1125 public static boolean 1126 isIsOrGetter( Method method ) 1127 { 1128 return( isGetter( method ) || isIs( method ) ); 1129 } 1130 1131 public static String 1132 getAttributeName( final Method method ) 1133 { 1134 final String methodName = method.getName(); 1135 String attrName = null; 1136 1137 int prefixLength = 0; 1138 1139 if ( methodName.startsWith( GET ) || methodName.startsWith( SET ) ) 1140 { 1141 prefixLength = 3; 1142 } 1143 else 1144 { 1145 prefixLength = 2; 1146 } 1147 1148 return( methodName.substring( prefixLength, methodName.length() ) ); 1149 } 1150 1151 1152 public static boolean 1153 isSetter( Method method ) 1154 { 1155 return( method.getName().startsWith( SET ) && 1156 method.getParameterTypes().length == 1 && 1157 method.getParameterTypes()[ 0 ] != Attribute.class && 1158 method.getReturnType().getName().equals( "void" ) ); 1159 } 1160 1161 public static boolean 1162 isGetAttribute( Method m ) 1163 { 1164 return( m.getName().equals( "getAttribute" ) && 1165 m.getParameterTypes().length == 1 && m.getParameterTypes()[ 0 ] == String .class ); 1166 1167 } 1168 1169 public static boolean 1170 isGetAttributes( Method m ) 1171 { 1172 return( m.getName().equals( "getAttributes" ) && 1173 m.getParameterTypes().length == 1 && m.getParameterTypes()[ 0 ] == String [].class ); 1174 1175 } 1176 1177 public static boolean 1178 isSetAttribute( Method m ) 1179 { 1180 return( m.getName().equals( "setAttribute" ) && 1181 m.getParameterTypes().length == 1 && m.getParameterTypes()[ 0 ] == Attribute.class ); 1182 1183 } 1184 1185 public static boolean 1186 isSetAttributes( Method m ) 1187 { 1188 return( m.getName().equals( "setAttributes" ) && 1189 m.getParameterTypes().length == 1 && m.getParameterTypes()[ 0 ] == AttributeList.class ); 1190 1191 } 1192 1193 1194 public static ArrayList <MBeanAttributeInfo> 1195 generateAttributeInfos( 1196 final Collection <Method > methodSet, 1197 final boolean read, 1198 final boolean write) 1199 { 1200 final ArrayList <MBeanAttributeInfo> infos = new ArrayList <MBeanAttributeInfo>(); 1201 1202 assert( methodSet != null ); 1203 1204 for( final Method m : methodSet ) 1205 { 1206 final String methodName = m.getName(); 1207 1208 assert( read || ( write && methodName.startsWith( SET )) ); 1209 final MBeanAttributeInfo info = new MBeanAttributeInfo( 1210 getAttributeName( m ), 1211 m.getReturnType().getName(), 1212 methodName, 1213 read, 1214 write, 1215 methodName.startsWith( "is" ) 1216 ); 1217 1218 infos.add( info ); 1219 } 1220 1221 return( infos ); 1222 } 1223 1224 public static MBeanAttributeInfo[] 1225 generateMBeanAttributeInfos( 1226 final Collection <Method > getterSetters, 1227 final Collection <Method > getters, 1228 final Collection <Method > setters ) 1229 { 1230 final ArrayList <MBeanAttributeInfo> attrsList = new ArrayList <MBeanAttributeInfo>(); 1231 1232 attrsList.addAll( generateAttributeInfos( getterSetters, true, true ) ); 1233 attrsList.addAll( generateAttributeInfos( getters, true, false ) ); 1234 attrsList.addAll( generateAttributeInfos( setters, false, true ) ); 1235 1236 final MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[ attrsList.size() ]; 1237 attrsList.toArray( attrs ); 1238 1239 return( attrs ); 1240 } 1241 1242 1243 public static String [] 1244 getSignature( final MBeanParameterInfo[]infos ) 1245 { 1246 final String [] sig = new String [ infos.length ]; 1247 1248 int i = 0; 1249 for( final MBeanParameterInfo info : infos ) 1250 { 1251 sig[ i ] = info.getType(); 1252 ++i; 1253 } 1254 return sig; 1255 } 1256 1257 public static MBeanParameterInfo[] 1258 generateSignature( final Class [] sig ) 1259 { 1260 final MBeanParameterInfo[] infos = new MBeanParameterInfo[ sig.length ]; 1261 1262 for( int i = 0; i < sig.length; ++i ) 1263 { 1264 final Class paramClass = sig[ i ]; 1265 1266 final String name = "p" + i; 1267 final String type = paramClass.getName(); 1268 final String description = paramClass.getName(); 1269 1270 final MBeanParameterInfo info = 1271 new MBeanParameterInfo( name, type, description ); 1272 infos[ i ] = info; 1273 } 1274 1275 return( infos ); 1276 } 1277 1278 public static MBeanOperationInfo[] 1279 generateMBeanOperationInfos( 1280 final Collection <Method > methodSet ) 1281 { 1282 final MBeanOperationInfo[] infos = new MBeanOperationInfo[ methodSet.size() ]; 1283 1284 final Iterator iter = methodSet.iterator(); 1285 1286 int i = 0; 1287 while ( iter.hasNext() ) 1288 { 1289 final Method m = (Method )iter.next(); 1290 final String methodName = m.getName(); 1291 1292 final MBeanOperationInfo info = new MBeanOperationInfo( 1293 methodName, 1294 methodName, 1295 generateSignature( m.getParameterTypes() ), 1296 m.getReturnType().getName(), 1297 MBeanOperationInfo.UNKNOWN 1298 ); 1299 1300 infos[ i ] = info; 1301 ++i; 1302 1303 } 1304 1305 return( infos ); 1306 } 1307 1308 1309 public static MBeanInfo 1310 interfaceToMBeanInfo( final Class theInterface ) 1311 { 1312 final Method [] methods = theInterface.getMethods(); 1313 1314 final Map <String ,Method > getters = new HashMap <String ,Method >(); 1315 final Map <String ,Method > setters = new HashMap <String ,Method >(); 1316 final Map <String ,Method > getterSetters = new HashMap <String ,Method >(); 1317 final Set <Method > operations = new HashSet <Method >(); 1318 1319 for( int i = 0; i < methods.length; ++i ) 1320 { 1321 final Method method = methods[ i ]; 1322 1323 final String methodName = method.getName(); 1324 1325 String attrName = null; 1326 if ( isIsOrGetter( method ) ) 1327 { 1328 attrName = getAttributeName( method ); 1329 getters.put( attrName, method ); 1330 } 1331 else if ( isSetter( method ) ) 1332 { 1333 attrName = getAttributeName( method ); 1334 setters.put( attrName, method ); 1335 } 1336 else 1337 { 1338 operations.add( method ); 1339 } 1340 1341 if ( (attrName != null) && 1342 getters.containsKey( attrName ) && 1343 setters.containsKey( attrName ) ) 1344 { 1345 final Method getter = (Method )getters.get( attrName ); 1346 1347 final Class getterType = getter.getReturnType(); 1348 final Class setterType = ((Method )setters.get( attrName )).getParameterTypes()[ 0 ]; 1349 1350 if ( getterType == setterType ) 1351 { 1352 getters.remove( attrName ); 1353 setters.remove( attrName ); 1354 getterSetters.put( attrName, getter ); 1355 } 1356 else 1357 { 1358 throw new IllegalArgumentException ( "Attribute " + attrName + 1359 "has type " + getterType.getName() + " as getter but type " + 1360 setterType.getName() + " as setter" ); 1361 } 1362 } 1363 } 1364 1365 1386 1387 final MBeanAttributeInfo[] attrInfos = 1388 generateMBeanAttributeInfos( getterSetters.values(), 1389 getters.values(), setters.values() ); 1390 1391 final MBeanOperationInfo[] operationInfos = 1392 generateMBeanOperationInfos( operations ); 1393 1394 final MBeanConstructorInfo[] constructorInfos = null; 1395 final MBeanNotificationInfo[] notificationInfos = null; 1396 1397 final MBeanInfo mbeanInfo = new MBeanInfo( 1398 theInterface.getName(), 1399 theInterface.getName(), 1400 attrInfos, 1401 constructorInfos, 1402 operationInfos, 1403 notificationInfos ); 1404 1405 return( mbeanInfo ); 1406 } 1407 1408 1414 public static MBeanAttributeInfo[] 1415 mergeMBeanAttributeInfos( 1416 final MBeanAttributeInfo[] infos1, 1417 final MBeanAttributeInfo[] infos2 ) 1418 { 1419 final Set <String > names = new HashSet <String >(); 1421 for( final MBeanAttributeInfo info : infos1 ) 1422 { 1423 names.add( info.getName() ); 1424 } 1425 1426 final Set <MBeanAttributeInfo> merged = GSetUtil.newSet( infos1 ); 1427 1428 for( final MBeanAttributeInfo info2 : infos2 ) 1429 { 1430 final String info2Name = info2.getName(); 1431 1432 if ( ! names.contains( info2Name ) ) 1433 { 1434 merged.add( info2 ); 1435 } 1436 } 1437 1438 final MBeanAttributeInfo[] infosArray = 1439 new MBeanAttributeInfo[ merged.size() ]; 1440 merged.toArray( infosArray ); 1441 1442 return( infosArray ); 1443 } 1444 1445 1451 public static MBeanNotificationInfo[] 1452 mergeMBeanNotificationInfos( 1453 final MBeanNotificationInfo[] infos1, 1454 final MBeanNotificationInfo[] infos2 ) 1455 { 1456 if ( infos1 == null ) 1457 { 1458 return infos2; 1459 } 1460 else if ( infos2 == null ) 1461 { 1462 return( infos1 ); 1463 } 1464 1465 final Set <MBeanNotificationInfo> all = GSetUtil.newSet( infos1 ); 1466 all.addAll( GSetUtil.newSet( infos2 ) ); 1467 1468 final MBeanNotificationInfo[] merged = new MBeanNotificationInfo[ all.size() ]; 1469 return all.toArray( merged ); 1470 } 1471 1472 1478 public static MBeanInfo 1479 addNotificationInfos( 1480 final MBeanInfo origInfo, 1481 final MBeanNotificationInfo[] notifs ) 1482 { 1483 MBeanInfo result = origInfo; 1484 1485 if ( notifs != null && notifs.length != 0 ) 1486 { 1487 result = new MBeanInfo( 1488 origInfo.getClassName(), 1489 origInfo.getDescription(), 1490 origInfo.getAttributes(), 1491 origInfo.getConstructors(), 1492 origInfo.getOperations(), 1493 mergeMBeanNotificationInfos( origInfo.getNotifications(), notifs ) 1494 ); 1495 } 1496 return result; 1497 } 1498 1499 1500 1506 public static MBeanOperationInfo[] 1507 mergeMBeanOperationInfos( 1508 final MBeanOperationInfo[] infos1, 1509 final MBeanOperationInfo[] infos2 ) 1510 { 1511 if ( infos1 == null ) 1512 { 1513 return infos2; 1514 } 1515 else if ( infos2 == null ) 1516 { 1517 return( infos1 ); 1518 } 1519 1520 final Set <MBeanOperationInfo> all = GSetUtil.newSet( infos1 ); 1521 all.addAll( GSetUtil.newSet( infos2 ) ); 1522 1523 final MBeanOperationInfo[] merged = new MBeanOperationInfo[ all.size() ]; 1524 return all.toArray( merged ); 1525 } 1526 1527 1533 public static MBeanConstructorInfo[] 1534 mergeMBeanConstructorInfos( 1535 final MBeanConstructorInfo[] infos1, 1536 final MBeanConstructorInfo[] infos2 ) 1537 { 1538 if ( infos1 == null ) 1539 { 1540 return infos2; 1541 } 1542 else if ( infos2 == null ) 1543 { 1544 return( infos1 ); 1545 } 1546 1547 final Set <MBeanConstructorInfo> all = GSetUtil.newSet( infos1 ); 1548 all.addAll( GSetUtil.newSet( infos2 ) ); 1549 1550 final MBeanConstructorInfo[] merged = new MBeanConstructorInfo[ all.size() ]; 1551 return all.toArray( merged ); 1552 } 1553 1554 1555 1561 public static MBeanInfo 1562 mergeMBeanInfos( 1563 final MBeanInfo info1, 1564 final MBeanInfo info2 ) 1565 { 1566 if ( info1 == null ) 1567 { 1568 return info2; 1569 } 1570 else if ( info2 == null ) 1571 { 1572 return( info1 ); 1573 } 1574 1575 return( new MBeanInfo( 1576 info1.getClassName(), 1577 info1.getDescription(), 1578 mergeMBeanAttributeInfos( info1.getAttributes(), info2.getAttributes() ), 1579 mergeMBeanConstructorInfos( info1.getConstructors(), info2.getConstructors() ), 1580 mergeMBeanOperationInfos( info1.getOperations(), info2.getOperations() ), 1581 mergeMBeanNotificationInfos( info1.getNotifications(), info2.getNotifications() ) 1582 ) ); 1583 1584 } 1585 1586 1587 1593 public static MBeanInfo 1594 newMBeanInfo( 1595 final MBeanInfo origMBeanInfo, 1596 final MBeanAttributeInfo[] newAttrInfos ) 1597 { 1598 final MBeanInfo info = new MBeanInfo( origMBeanInfo.getClassName(), 1599 origMBeanInfo.getDescription(), 1600 newAttrInfos, 1601 origMBeanInfo.getConstructors(), 1602 origMBeanInfo.getOperations(), 1603 origMBeanInfo.getNotifications() ); 1604 return( info ); 1605 } 1606 1607 1613 public static MBeanInfo 1614 newMBeanInfo( 1615 final MBeanInfo origMBeanInfo, 1616 final MBeanOperationInfo[] newOps ) 1617 { 1618 final MBeanInfo info = new MBeanInfo( origMBeanInfo.getClassName(), 1619 origMBeanInfo.getDescription(), 1620 origMBeanInfo.getAttributes(), 1621 origMBeanInfo.getConstructors(), 1622 newOps, 1623 origMBeanInfo.getNotifications() ); 1624 return( info ); 1625 } 1626 1627 1628 1638 public static int 1639 findMBeanOperationInfo( 1640 final MBeanInfo info, 1641 final String methodName, 1642 final String [] parameterTypes ) 1643 { 1644 int resultIdx = -1; 1645 1646 final MBeanOperationInfo[] ops = info.getOperations(); 1647 for( int i = 0; i < ops.length; ++i ) 1648 { 1649 final MBeanOperationInfo op = ops[i]; 1650 1651 if ( op.getName().equals( methodName ) && 1652 ( parameterTypes == null || 1653 ArrayUtil.arraysEqual( parameterTypes, op.getSignature() ) ) 1654 ) 1655 { 1656 resultIdx = i; 1657 break; 1658 } 1659 } 1660 1661 return resultIdx; 1662 } 1663 1664 1665 1666 public static boolean 1667 domainMatches( 1668 final String defaultDomain, 1669 final ObjectName pattern, 1670 final ObjectName candidate ) 1671 { 1672 boolean matches = false; 1673 1674 final String candidateDomain = candidate.getDomain(); 1675 if ( pattern.isDomainPattern() ) 1676 { 1677 final String regex = 1678 RegexUtil.wildcardToJavaRegex( pattern.getDomain() ); 1679 1680 matches = Pattern.matches( regex, candidateDomain); 1681 } 1682 else 1683 { 1684 1686 String patternDomain = pattern.getDomain(); 1687 if ( patternDomain.length() == 0 ) 1688 { 1689 patternDomain = defaultDomain; 1690 } 1691 1692 matches = patternDomain.equals( candidateDomain ); 1693 } 1694 1695 1697 return( matches ); 1698 } 1699 1700 public static boolean 1701 matchesPattern( 1702 final String defaultDomain, 1703 final ObjectName pattern, 1704 final ObjectName candidate ) 1705 { 1706 boolean matches = false; 1707 1708 if ( domainMatches( defaultDomain, pattern, candidate ) ) 1709 { 1710 final String patternProps = pattern.getCanonicalKeyPropertyListString(); 1711 final String candidateProps = candidate.getCanonicalKeyPropertyListString(); 1712 assert( patternProps.indexOf( "*" ) < 0 ); 1713 assert( candidateProps.indexOf( "*" ) < 0 ); 1714 1715 if ( candidateProps.indexOf( patternProps ) >= 0 ) 1718 { 1719 matches = true; 1720 } 1721 } 1722 1723 return( matches ); 1724 } 1725 1726 public static String 1727 toString( final ObjectName objectName ) 1728 { 1729 return ObjectNameStringifier.DEFAULT.stringify( objectName ); 1730 } 1731 1732 1733 public static Notification 1734 cloneNotification( 1735 final Notification in, 1736 final Object source ) 1737 { 1738 Notification out = null; 1739 1740 if ( in.getClass() == AttributeChangeNotification.class ) 1741 { 1742 final AttributeChangeNotification a = (AttributeChangeNotification)in; 1743 1744 out = new AttributeChangeNotification( 1745 source, 1746 a.getSequenceNumber(), 1747 a.getTimeStamp(), 1748 a.getMessage(), 1749 a.getAttributeName(), 1750 a.getAttributeType(), 1751 a.getOldValue(), 1752 a.getNewValue() ); 1753 } 1754 else if ( in.getClass() == Notification.class ) 1755 { 1756 out = new Notification( 1757 in.getType(), 1758 source, 1759 in.getSequenceNumber(), 1760 in.getTimeStamp(), 1761 in.getMessage() ); 1762 } 1763 else 1764 { 1765 throw new IllegalArgumentException ( "Not supporting cloning of: " + in.getClass() ); 1766 } 1767 1768 return out; 1769 } 1770 1771 1776 public static Set <ObjectName> 1777 queryNames( 1778 final MBeanServerConnection conn, 1779 final ObjectName pattern, 1780 final QueryExp exp) 1781 throws java.io.IOException 1782 { 1783 return TypeCast.asSet( conn.queryNames( pattern, exp ) ); 1784 } 1785 1786 1791 public static Set <ObjectName> 1792 queryNames( 1793 final MBeanServer server, 1794 final ObjectName pattern, 1795 final QueryExp exp) 1796 { 1797 try 1798 { 1799 return queryNames( (MBeanServerConnection)server, pattern, exp ); 1800 } 1801 catch( final IOException e ) 1802 { 1803 } 1805 return null; 1806 } 1807 1808 1809 1810 1811 1815 public static final <T extends Serializable > Map <String , T> 1816 getUserDataMapString_Serializable( final Notification notif ) 1817 { 1818 final Object userData = notif.getUserData(); 1819 if ( ! (userData instanceof Map ) ) 1820 { 1821 throw new IllegalArgumentException (); 1822 } 1823 1824 final Map <String ,T> result = TypeCast.asMap( (Map )userData ); 1825 if ( result != null ) 1826 { 1827 for ( final String testKey : result.keySet() ) 1829 { 1830 final T testValue = result.get( testKey ); 1831 1832 result.put( testKey, testValue ); 1833 } 1834 } 1835 1836 return result; 1837 } 1838} 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 | Popular Tags |