1 23 package com.sun.appserv.management.base; 24 25 import java.util.Set ; 26 import java.util.HashSet ; 27 import java.util.Map ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Collections ; 31 import java.io.Serializable ; 32 33 import java.lang.reflect.Proxy ; 34 35 import javax.management.ObjectName ; 36 import javax.management.Notification ; 37 38 import com.sun.appserv.management.base.AMX; 39 import com.sun.appserv.management.util.jmx.JMXUtil; 40 import com.sun.appserv.management.util.misc.MapUtil; 41 import com.sun.appserv.management.util.misc.GSetUtil; 42 import com.sun.appserv.management.util.misc.TypeCast; 43 44 47 public final class Util 48 { 49 private Util() {} 50 51 59 public static ObjectName 60 newObjectName( String name ) 61 { 62 return( JMXUtil.newObjectName( name ) ); 63 } 64 65 66 72 public static ObjectName 73 newObjectName( String domain, String props ) 74 { 75 return( newObjectName( domain + ":" + props ) ); 76 } 77 78 84 public static ObjectName 85 newObjectNamePattern( 86 final String domain, 87 final String props ) 88 { 89 return( JMXUtil.newObjectNamePattern( domain, props ) ); 90 } 91 92 97 public static ObjectName 98 newObjectNamePattern( ObjectName objectName ) 99 { 100 final String props = objectName.getKeyPropertyListString(); 101 102 return( newObjectNamePattern( objectName.getDomain(), props) ); 103 } 104 105 106 112 public static String 113 makeProp( 114 final String name, 115 final String value ) 116 { 117 return( JMXUtil.makeProp( name, value ) ); 118 } 119 120 125 public static String 126 makeJ2EETypeProp( final String value ) 127 { 128 return( makeProp( AMX.J2EE_TYPE_KEY, value ) ); 129 } 130 131 136 public static String 137 makeNameProp( final String value ) 138 { 139 return( makeProp( AMX.NAME_KEY, value ) ); 140 } 141 142 146 public static String 147 makeRequiredProps( 148 final String j2eeType, 149 final String name ) 150 { 151 final String j2eeTypeProp = Util.makeJ2EETypeProp( j2eeType ); 152 final String nameProp = Util.makeNameProp( name ); 153 154 final String props = Util.concatenateProps( j2eeTypeProp, nameProp ); 155 156 return( props ); 157 } 158 159 165 public static String 166 getSelfProp( final ObjectName objectName ) 167 { 168 final String j2eeType = objectName.getKeyProperty( AMX.J2EE_TYPE_KEY ); 169 final String name = objectName.getKeyProperty( AMX.NAME_KEY ); 170 171 return( Util.makeProp( j2eeType, name ) ); 172 } 173 174 175 184 public static String 185 getFullTypeProps( 186 final ObjectName objectName, 187 final String fullType ) 188 { 189 final String selfProp = Util.getSelfProp( objectName ); 190 191 String ancestorProps = ""; 194 final String [] types = Util.getTypeArray( fullType ); 195 for( int i = 0; i < types.length - 1; ++i ) 196 { 197 final String key = types[ i ]; 198 final String value = objectName.getKeyProperty( key ); 199 final String prop = Util.makeProp( key, value ); 200 201 ancestorProps = Util.concatenateProps( ancestorProps, prop ); 202 } 203 204 final String props = Util.concatenateProps( selfProp, ancestorProps ); 205 206 return( props ); 207 } 208 209 215 public static String 216 getName( final ObjectName objectName ) 217 { 218 String name = objectName.getKeyProperty( AMX.NAME_KEY ); 219 220 if ( name == null ) 221 { 222 name = AMX.NO_NAME; 223 } 224 225 return( name ); 226 } 227 228 public static String 229 getJ2EEType( final ObjectName objectName ) 230 { 231 return( objectName.getKeyProperty( AMX.J2EE_TYPE_KEY ) ); 232 } 233 234 239 public static String [] 240 getTypeArray( final String fullType ) 241 { 242 if ( fullType == null ) 243 { 244 throw new IllegalArgumentException (); 245 } 246 247 assert( AMX.FULL_TYPE_DELIM.equals( "." ) ); 248 return( fullType.split( "\\." ) ); 249 } 250 251 252 256 private static final Set <String > PATTERN_PROPS = 257 GSetUtil.newUnmodifiableStringSet( 258 AMX.J2EE_TYPE_KEY, 259 AMX.NAME_KEY ); 260 261 262 266 public static Set <String > 267 getPatternKeys( 268 final String fullType ) 269 { 270 final Set <String > requiredKeys = GSetUtil.copySet( PATTERN_PROPS ); 271 272 final String [] types = Util.getTypeArray( fullType ); 275 for( int i = 0; i < types.length - 1; ++i ) 276 { 277 requiredKeys.add( types[ i ] ); 278 } 279 280 return TypeCast.checkedStringSet( requiredKeys ); 281 } 282 283 public static String 284 concatenateProps( 285 final String props1, 286 final String props2 ) 287 { 288 return( JMXUtil.concatenateProps( props1, props2 ) ); 289 } 290 291 public static String 292 concatenateProps( 293 final String props1, 294 final String props2, 295 final String props3 ) 296 { 297 return( concatenateProps( concatenateProps( props1, props2), props3) ); 298 } 299 300 303 public static Set <ObjectName > 304 toObjectNames( final Set <? extends AMX> amxs ) 305 { 306 final Set <ObjectName > objectNames = new HashSet <ObjectName >(); 307 for( final AMX next : amxs ) 308 { 309 objectNames.add( getObjectName( next ) ); 310 } 311 return( Collections.checkedSet(objectNames, ObjectName .class) ); 312 } 313 314 317 public static Map <String ,ObjectName > 318 toObjectNames( final Map <String ,? extends AMX> amxMap ) 319 { 320 final Map <String ,ObjectName > m = new HashMap <String ,ObjectName >(); 321 322 for( final String key : amxMap.keySet() ) 323 { 324 final AMX value = amxMap.get( key ); 325 m.put( key, getExtra( value ).getObjectName() ); 326 } 327 return( Collections.checkedMap( m, String .class, ObjectName .class) ); 328 } 329 330 333 public static ObjectName [] 334 toObjectNames( final AMX[] amx ) 335 { 336 final ObjectName [] objectNames = new ObjectName [ amx.length ]; 337 for( int i = 0; i < objectNames.length; ++i ) 338 { 339 objectNames[ i ] = amx[ i ] == null ? null : getExtra( amx[ i ] ).getObjectName(); 340 } 341 342 return( objectNames ); 343 } 344 345 353 public static Set <String > 354 getNames( final Set <? extends AMX> amxs ) 355 { 356 return getNamesSet( Util.toObjectNames( amxs ) ); 357 } 358 359 367 public static Set <String > 368 getNamesSet( final Set <ObjectName > objectNames ) 369 { 370 return TypeCast.checkedStringSet( 371 JMXUtil.getKeyPropertySet( AMX.NAME_KEY, objectNames ) ); 372 } 373 374 379 public static String [] 380 getNamesArray( final Set <ObjectName > objectNames ) 381 { 382 return( JMXUtil.getKeyProperty( AMX.NAME_KEY, objectNames ) ); 383 } 384 385 393 public static final Map <String ,ObjectName > 394 createObjectNameMap( final Set <ObjectName > objectNames ) 395 { 396 final Map <String ,ObjectName > m = new HashMap <String ,ObjectName >(); 397 398 for( final ObjectName objectName : objectNames ) 399 { 400 final String name = getName( objectName ); 401 402 assert( ! m.containsKey( name ) ) : 403 "createObjectNameMap: key already present: " + name + " in " + objectName; 404 m.put( name, objectName ); 405 } 406 407 assert( m.keySet().size() == objectNames.size() ); 408 409 return( Collections.checkedMap(m, String .class, ObjectName .class) ); 410 } 411 412 413 419 public static <T extends AMX> Map <String ,T> 420 createNameMap( final Set <T> amxs ) 421 { 422 final Map <String ,T> m = new HashMap <String ,T>(); 423 424 for( final T amx : amxs ) 425 { 426 final String name = amx.getName(); 427 m.put( name, amx ); 428 } 429 430 return( m ); 431 } 432 433 439 public static Extra 440 getExtra( final AMX proxy ) 441 { 442 return( (Extra)Proxy.getInvocationHandler( proxy ) ); 443 } 444 445 446 451 public static <T extends AMX> ObjectName 452 getObjectName( final T proxy ) 453 { 454 return( getExtra( proxy ).getObjectName() ); 455 } 456 457 458 467 public static ObjectName 468 getObjectName( 469 final Map <String ,ObjectName > candidates, 470 final String name ) 471 { 472 final Object item = candidates.get( name ); 473 if ( item == null ) 474 { 475 throw new IllegalArgumentException ( "Not found: " + name ); 476 } 477 478 return (ObjectName )item; 479 } 480 481 488 public static Map <String ,Serializable > 489 getAMXNotificationData( final Notification notif ) 490 { 491 return Collections.unmodifiableMap( 492 JMXUtil.getUserDataMapString_Serializable( notif ) ); 493 } 494 495 498 public static Serializable 499 getAMXNotificationValue( final Notification notif, final String key ) 500 { 501 final Map <String ,Serializable > data = 502 getAMXNotificationData( notif ); 503 504 if ( data == null ) 505 { 506 throw new IllegalArgumentException ( notif.toString() ); 507 } 508 509 if ( ! data.containsKey( key ) ) 510 { 511 throw new IllegalArgumentException ( "Value not found for " + key + 512 " in " + notif ); 513 } 514 515 return data.get( key ); 516 } 517 518 523 public static <T extends Serializable > T 524 getAMXNotificationValue( 525 final Notification notif, 526 final String key, 527 final Class <T> theClass) 528 { 529 final Serializable value = getAMXNotificationValue( notif, key ); 530 531 return theClass.cast( value ); 532 } 533 534 public static void 535 sleep( final long millis ) 536 { 537 try 538 { 539 Thread.sleep( millis ); 540 } 541 catch( InterruptedException e ) 542 { 543 } 544 } 545 546 547 550 public static AMX 551 asAMX( final Object o) 552 { 553 return AMX.class.cast( o ); 554 } 555 } 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 | Popular Tags |