1 23 package com.sun.enterprise.management.support; 24 25 import java.util.Map ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import java.util.List ; 29 import java.util.HashSet ; 30 import java.util.HashMap ; 31 import java.util.ArrayList ; 32 33 import javax.management.ObjectName ; 34 import javax.management.MBeanInfo ; 35 import javax.management.MBeanServer ; 36 import javax.management.MBeanAttributeInfo ; 37 import javax.management.MBeanOperationInfo ; 38 import javax.management.Attribute ; 39 import javax.management.AttributeList ; 40 import javax.management.AttributeChangeNotification ; 41 import javax.management.InstanceNotFoundException ; 42 import javax.management.ReflectionException ; 43 import javax.management.MBeanException ; 44 import javax.management.IntrospectionException ; 45 import javax.management.AttributeNotFoundException ; 46 import javax.management.InvalidAttributeValueException ; 47 48 import javax.management.MBeanServerInvocationHandler ; 49 50 51 52 import com.sun.appserv.management.util.misc.ClassUtil; 53 import com.sun.appserv.management.util.misc.GSetUtil; 54 import com.sun.appserv.management.util.jmx.JMXUtil; 55 56 import com.sun.appserv.management.config.PropertiesAccess; 57 import com.sun.appserv.management.base.XTypesMapper; 58 import com.sun.appserv.management.base.Util; 59 60 import com.sun.appserv.management.base.DottedNames; 61 import com.sun.appserv.management.base.AMX; 62 63 import com.sun.enterprise.management.support.ObjectNames; 64 65 66 67 72 public abstract class DottedNamesBase extends AMXImplBase 73 implements DottedNames 74 { 75 private static final String DOTTED_NAMES = 76 "com.sun.appserv:name=dotted-name-get-set,type=dotted-name-support"; 77 78 protected OldDottedNamesProxy mOldDottedNamesProxy = null; 79 80 private MBeanInfo mMBeanInfo; 81 private Map <String ,Attribute > mAttributes; 82 83 private Set <String > mParentAttributeNames; 84 85 public 86 DottedNamesBase() 87 { 88 mMBeanInfo = null; 89 90 mParentAttributeNames = null; 91 } 92 93 94 public abstract Object [] dottedNameGet( String [] names ); 95 public abstract Object dottedNameGet( String name ); 96 public abstract Object [] dottedNameList( String [] names ); 97 public abstract Object [] dottedNameSet( String [] nameValuePairs ); 98 99 protected abstract boolean isWriteableDottedName( String name ); 100 101 102 105 public boolean 106 getMBeanInfoIsInvariant() 107 { 108 return( false ); 109 } 110 111 115 public MBeanInfo 116 getMBeanInfo() 117 { 118 MBeanInfo mbeanInfo = null; 119 120 if ( getOldDottedNames() != null ) 121 { 122 ensureMBeanInfo(); 123 mbeanInfo = mMBeanInfo; 124 } 125 else 126 { 127 mbeanInfo = super.getMBeanInfo(); 128 } 129 130 return( mbeanInfo ); 131 } 132 133 public void 134 preRegisterDone() 135 throws Exception 136 { 137 super.preRegisterDone(); 138 139 setupOldDottedNamesProxy( ); 140 } 141 142 143 146 protected interface OldDottedNamesProxy 147 { 148 public Object [] dottedNameGet( String [] names ); 149 public Object dottedNameGet( String name ); 150 public Object [] dottedNameMonitoringGet( String [] names ); 151 public Object dottedNameMonitoringGet( String names ); 152 public String [] dottedNameList( String [] names ); 153 public String [] dottedNameMonitoringList( String [] names ); 154 public Object [] dottedNameSet( String [] names ); 155 } 156 157 protected void 158 setupOldDottedNamesProxy( ) 159 { 160 setupOldDottedNamesProxy( Util.newObjectName( DOTTED_NAMES ) ); 161 } 162 163 protected void 164 setupOldDottedNamesProxy( ObjectName target ) 165 { 166 final MBeanServer server = getMBeanServer(); 167 168 if ( ! server.isRegistered( target ) ) 169 { 170 logSevere( "setupOldDottedNamesProxy: old dotted names MBeans is not registered" ); 171 } 172 173 mOldDottedNamesProxy = (OldDottedNamesProxy) 174 MBeanServerInvocationHandler.newProxyInstance( 175 server, target, OldDottedNamesProxy.class, false ); 176 } 177 178 protected OldDottedNamesProxy 179 getOldDottedNames() 180 { 181 return( mOldDottedNamesProxy ); 182 } 183 184 protected final boolean 185 isDottedName( final String name ) 186 { 187 return( getAttributes().keySet().contains( name ) ); 188 } 189 190 protected final boolean 191 isParentAttributeName( final String name ) 192 { 193 return( mParentAttributeNames.contains( name ) ); 194 } 195 196 protected final void 197 filterNames( 198 final String [] in, 199 Set <String > dotted, 200 Set <String > parent ) 201 { 202 for( int i = 0; i < in.length; ++i ) 203 { 204 final String name = in[ i ]; 205 206 if ( isDottedName( name ) ) 207 { 208 dotted.add( name ); 209 } 210 else if ( isParentAttributeName( name ) ) 211 { 212 parent.add( name ); 213 } 214 } 215 } 216 217 public AttributeList 218 getAttributes( final String [] names ) 219 { 220 mCoverage.attributesWereRead( names ); 221 222 final Set <String > dotted = new HashSet <String >(); 223 final Set <String > parent = new HashSet <String >(); 224 filterNames( names, dotted, parent); 225 226 final Object [] dottedResults = 227 dottedNameGet( (String [])dotted.toArray( new String [ dotted.size() ] ) ); 228 229 final String [] namesForParent = new String [ parent.size() ]; 230 final AttributeList parentResults = 231 super.getAttributes( (String [])parent.toArray( namesForParent ) ); 232 233 final AttributeList successList = new AttributeList (); 234 successList.addAll( parentResults ); 235 236 for( int i = 0; i < dottedResults.length; ++i ) 238 { 239 if ( dottedResults[ i ] instanceof Attribute ) 240 { 241 successList.add( (Attribute )dottedResults[ i ] ); 242 } 243 else 244 { 245 assert( dottedResults[ i ] instanceof Exception ); 246 } 247 } 248 249 250 return( successList ); 251 } 252 253 public Object 254 getAttribute( final String name ) 255 throws AttributeNotFoundException 256 { 257 checkLegalName( name ); 258 mCoverage.attributeWasRead( name ); 259 260 Object result = null; 261 262 if ( isDottedName( name ) ) 263 { 264 result = dottedNameGet( name ); 265 assert( !(result instanceof Attribute ) ); 266 } 267 else if ( isParentAttributeName( name ) ) 268 { 269 result = super.getAttribute( name ); 270 } 271 else 272 { 273 throw new AttributeNotFoundException ( name ); 274 } 275 276 return( result ); 277 } 278 279 280 public void 281 setAttribute( final Attribute attr ) 282 throws AttributeNotFoundException , InvalidAttributeValueException 283 { 284 final String name = attr.getName(); 285 286 if ( isParentAttributeName( name ) ) 287 { 288 super.setAttribute( attr ); 289 } 290 else 291 { 292 checkLegalName( name ); 293 mCoverage.attributeWasWritten( name ); 294 295 final AttributeList inList = new AttributeList (); 296 inList.add( attr ); 297 final AttributeList result = setAttributes( inList ); 298 if ( result.size() != 1) 299 { 300 throw new InvalidAttributeValueException ( attr.getName() ); 301 } 302 } 303 } 304 305 public AttributeList 306 setAttributes( AttributeList attributes ) 307 { 308 309 313 final int numAttrsIn = attributes.size(); 314 final List <String > legalPairs = new ArrayList <String >(); 315 for( int i = 0; i < numAttrsIn; ++i ) 316 { 317 final Attribute attr = (Attribute )attributes.get( i ); 318 319 final String name = attr.getName(); 320 mCoverage.attributeWasWritten( name ); 321 322 if ( isLegalAttributeName( name ) ) 323 { 324 legalPairs.add( attributeToNamePair( attr ) ); 325 } 326 } 327 328 final String [] pairs = (String [])legalPairs.toArray( new String [ legalPairs.size() ] ); 329 330 final Object [] results = dottedNameSet( pairs ); 331 332 final AttributeList attributeList = new AttributeList (); 333 for( int i = 0; i < results.length; ++i ) 334 { 335 if ( results[ i ] instanceof Attribute ) 336 { 337 attributeList.add( (Attribute )results[ i ] ); 338 } 339 else 340 { 341 assert( results[ i ] instanceof Exception ); 342 } 344 } 345 346 return( attributeList ); 347 } 348 349 350 public Object 351 invokeManually( 352 String operationName, 353 Object [] params, 354 String [] types) 355 throws ReflectionException , MBeanException , NoSuchMethodException , 356 AttributeNotFoundException 357 { 358 final boolean noParams = params == null || params.length == 0; 359 Object result = null; 360 361 if ( operationName.equals( "refresh" ) && noParams ) 362 { 363 refresh(); 364 result = null; 365 } 366 else 367 { 368 result = super.invokeManually( operationName, params, types ); 369 } 370 return( result ); 371 } 372 373 private synchronized void 374 ensureMBeanInfo() 375 { 376 if ( mMBeanInfo == null ) 377 { 378 trace( "##### DottedNamesBase.ensureMBeanInfo" ); 379 refresh(); 380 assert( mMBeanInfo != null ); 381 } 382 } 383 384 385 protected MBeanAttributeInfo [] 386 buildAttributeInfos( final MBeanAttributeInfo [] parentAttributeInfos ) 387 { 388 final String [] parentAttributeNames = 389 JMXUtil.getAttributeNames( parentAttributeInfos ); 390 mParentAttributeNames = 391 GSetUtil.newUnmodifiableStringSet( parentAttributeNames ); 392 393 final Map <String ,Attribute > attributes = getAttributes(); 394 395 final MBeanAttributeInfo [] infos = 396 new MBeanAttributeInfo [ attributes.size() + parentAttributeInfos.length ]; 397 398 400 int i = 0; 401 for( final String name : attributes.keySet() ) 402 { 403 final Attribute attr = attributes.get( name ); 404 405 final Object value = attr.getValue(); 406 final Class theClass = value == null ? String .class : attr.getValue().getClass(); 407 408 infos[ i ] = new MBeanAttributeInfo ( name, theClass.getName(), "", 409 true, isWriteableDottedName( name ), false ); 410 ++i; 411 } 412 413 System.arraycopy( parentAttributeInfos, 0, 414 infos, attributes.size(), parentAttributeInfos.length ); 415 416 return( infos ); 417 } 418 419 protected MBeanOperationInfo [] 420 buildOperationInfos( final MBeanOperationInfo [] existing ) 421 { 422 final MBeanOperationInfo refreshInfo = new MBeanOperationInfo ( "refresh", 423 "update MBeanInfo to reflect all available dotted names", 424 null, 425 Void .class.getName(), 426 MBeanOperationInfo.ACTION ); 427 428 final MBeanOperationInfo [] infos = new MBeanOperationInfo [ existing.length + 1 ]; 429 System.arraycopy( existing, 0, infos, 0, existing.length ); 430 infos[ infos.length -1 ] = refreshInfo; 431 432 return( infos ); 433 } 434 435 protected MBeanInfo 436 buildMBeanInfo() 437 { 438 final MBeanInfo superMBeanInfo = super.getMBeanInfo(); 439 440 final MBeanAttributeInfo [] attributeInfos = 441 buildAttributeInfos( superMBeanInfo.getAttributes() ); 442 final MBeanOperationInfo [] operationInfos = 443 buildOperationInfos( superMBeanInfo.getOperations() ); 444 445 final MBeanInfo info = new MBeanInfo ( this.getClass().getName(), 446 "exposes dotted-names as Attributes", 447 attributeInfos, 448 superMBeanInfo.getConstructors(), 449 operationInfos, 450 superMBeanInfo.getNotifications() ); 451 452 return( info ); 453 } 454 455 456 457 458 private static Attribute 459 namePairToAttribute( String pair ) 460 { 461 final int delimIndex = pair.indexOf( "=" ); 462 assert( delimIndex >= 1 ); 463 final String name = pair.substring( 0, delimIndex ); 464 final String value = pair.substring( delimIndex + 1, pair.length() ); 465 466 return( new Attribute ( name, value ) ); 467 } 468 469 private static String 470 attributeToNamePair( Attribute attr ) 471 { 472 return( attr.getName() + "=" + attr.getValue() ); 473 } 474 475 protected final boolean 476 isLegalAttributeName( final String name ) 477 { 478 ensureAttributes(); 479 return( getAttributes().keySet().contains( name ) || 480 mParentAttributeNames.contains( name ) ); 481 } 482 483 protected final void 484 ensureAttributes() 485 { 486 if ( mAttributes == null ) 487 { 488 refreshAttributes(); 489 } 490 } 491 492 protected final Map <String ,Attribute > 493 getAttributes() 494 { 495 ensureAttributes(); 496 497 return( mAttributes ); 498 } 499 500 protected final String [] 501 getDottedNamesArray() 502 { 503 final Map <String ,Attribute > map = getAttributes(); 504 505 final Set <String > keySet = map.keySet(); 506 507 return( (String [])keySet.toArray( new String [ keySet.size() ] ) ); 508 } 509 510 511 private final void 512 checkLegalName( String name ) 513 throws AttributeNotFoundException 514 { 515 if ( ! isLegalAttributeName( name ) ) 516 { 517 throw new AttributeNotFoundException ( "illegal attribute name: " + name ); 518 } 519 } 520 521 522 525 private static final String ALL = "*"; 526 final void 527 refreshAttributes() 528 { 529 Object result = null; 530 531 result = dottedNameGet( ALL ); 533 534 final Attribute [] values = (Attribute [])result; 537 538 final Map <String ,Attribute > map = new HashMap <String ,Attribute >(); 540 for( final Attribute attr : values ) 541 { 542 map.put( attr.getName(), attr ); 543 } 544 545 mAttributes = map; 546 } 547 548 public final void 549 refresh() 550 { 551 trace( "##### DottedNamesBase.refresh" ); 552 refreshAttributes(); 553 mMBeanInfo = buildMBeanInfo(); 554 } 555 } 556 557 558 559 560 561 562 563 564 | Popular Tags |