1 23 24 25 package com.sun.enterprise.management.offline; 26 27 import java.util.Set ; 28 import java.util.Map ; 29 import java.util.HashMap ; 30 import java.util.List ; 31 import java.util.ArrayList ; 32 33 import java.io.IOException ; 34 35 36 import javax.management.ObjectName ; 37 import javax.management.MBeanInfo ; 38 import javax.management.MBeanAttributeInfo ; 39 import javax.management.MBeanOperationInfo ; 40 import javax.management.Attribute ; 41 import javax.management.AttributeList ; 42 import javax.management.AttributeNotFoundException ; 43 import javax.management.InvalidAttributeValueException ; 44 45 import com.sun.appserv.management.base.AMXDebug; 46 47 import com.sun.appserv.management.util.misc.GSetUtil; 48 import com.sun.appserv.management.util.misc.StringUtil; 49 import com.sun.appserv.management.util.misc.ListUtil; 50 51 import com.sun.enterprise.management.support.oldconfig.OldProps; 52 53 import com.sun.enterprise.config.ConfigContext; 54 import com.sun.enterprise.config.ConfigBean; 55 import com.sun.enterprise.config.ConfigException; 56 57 58 import com.sun.enterprise.config.util.ConfigXPathHelper; 59 60 62 abstract class ConfigBeanHelper 63 64 { 65 private final ConfigBean mConfigBean; 66 private final String mXPath; 67 private final ConfigContext mConfigContext; 68 69 private final String mType; 70 private final String mName; 72 static private final String NAME_ATTR = "Name"; 73 static private final String VALUE_ATTR = "Value"; 74 static private final String DESCRIPTION_ATTR = "Description"; 75 76 static protected final String [] EMPTY_STRING_ARRAY = new String [0]; 77 78 ConfigBeanHelper( 79 final ConfigContext configContext, 80 final ConfigBean configBean ) 81 { 82 if ( configBean == null ) 83 { 84 throw new IllegalArgumentException ( "null configBean" ); 85 } 86 if ( configContext == null ) 87 { 88 throw new IllegalArgumentException ( "null configContext" ); 89 } 90 91 mConfigContext = configContext; 93 94 mConfigBean = configBean; 95 mXPath = mConfigBean.getXPath(); 96 assert( mXPath != null ); 97 98 mType = _getType( mXPath ); 99 mName = _getName(); 100 101 } 102 103 protected void 104 debug( Object o ) 105 { 106 sdebug( o ); 107 108 final String xpath = "" + mXPath; 109 AMXDebug.getInstance().getOutput( "ConfigBeanHelper" ).println( xpath + ": " + o ); 110 } 111 112 protected void 113 sdebug( Object o ) 114 { 115 System.out.println( "" + o ); 116 } 117 118 119 public ConfigBean 120 getConfigBean() 121 { 122 return mConfigBean; 123 } 124 125 public ConfigContext 126 getConfigContext() 127 { 128 return mConfigContext; 129 } 130 131 public Object 132 getAttribute( final String attrName ) 133 throws AttributeNotFoundException 134 { 135 Object result = null; 136 137 if ( DESCRIPTION_ATTR.equals( attrName ) ) 138 { 139 result = getDescription(); 140 } 141 else 142 { 143 try 144 { 145 result = mConfigBean.getAttributeValue( attrName ); 146 } 147 catch( RuntimeException e ) 148 { 149 debug( "ATTR FAILED: " + attrName ); 150 throw e; 151 } 152 } 153 return result; 154 } 155 156 public String 157 getXPath() 158 { 159 return mXPath; 160 } 161 162 public String 163 getParentXPath() 164 { 165 return ConfigXPathHelper.getParentXPath( getXPath() ); 166 } 167 168 171 public String 172 getType() 173 { 174 return mType; 175 } 176 177 static String 178 _getType( final String xPath ) 179 { 180 return ConfigXPathHelper.getLastNodeName( xPath ); 181 } 182 183 186 public String 187 getName() 188 { 189 return mName; 190 } 191 192 195 public String [] 196 getTypeAndName() 197 throws ConfigException 198 { 199 return new String [] { getType(), getName() }; 200 } 201 202 private String 203 _getName() 204 { 205 String name = null; 206 207 final Set <String > attrNames = GSetUtil.newStringSet( getAttributeNames() ); 208 209 for( final String nameKey : OldProps.getPossibleNameKeys() ) 210 { 211 final String camelized = mConfigBean.camelize( nameKey ); 212 213 if ( attrNames.contains( camelized ) ) 214 { 215 try 216 { 217 name = (String )getAttribute( camelized ); 218 if ( name != null ) 219 { 220 break; 221 } 222 } 223 catch( AttributeNotFoundException e ) 224 { 225 throw new RuntimeException ( e ); 226 } 227 } 228 } 229 230 return name; 231 } 232 233 234 public List <String []> 235 getAllObjectNameProps( final Set <String > ignoreTypes ) 236 throws ConfigException 237 { 238 final List <String []> props = new ArrayList <String []>(); 239 240 String [] pair = getTypeAndName(); 243 props.add( pair ); 244 245 String curXPath = ConfigXPathHelper.getParentXPath( getXPath() ); 246 String lastXPath = null; 247 while ( ! curXPath.equals( lastXPath ) ) 248 { 249 final ConfigBeanHelper helper = 250 ConfigBeanHelperFactory.getInstance( getConfigContext() ).getHelper( curXPath ); 251 252 pair = helper.getTypeAndName(); 253 254 if ( ! ignoreTypes.contains( pair[ 0 ] ) ) 255 { 256 props.add( pair ); 257 } 258 259 lastXPath = curXPath; 260 curXPath = helper.getParentXPath(); 261 } 262 263 return props; 264 } 265 266 267 public void 268 setAttribute( final Attribute attr ) 269 throws AttributeNotFoundException , InvalidAttributeValueException 270 { 271 final Object value = attr.getValue(); 272 273 setAttribute( attr.getName(), value ); 274 } 275 276 public void 277 setAttribute( final String name, final Object value ) 278 throws AttributeNotFoundException , InvalidAttributeValueException 279 { 280 if ( DESCRIPTION_ATTR.equals( name ) ) 281 { 282 setDescription( (String )value ); 283 } 284 else 285 { 286 final String valueString = (value == null) ? null : ("" + value); 288 mConfigBean.setAttributeValue( name, valueString ); 289 } 290 } 291 292 293 protected boolean 294 hasValue( final String valueName ) 295 { 296 boolean hasValue = false; 297 298 try 299 { 300 final Object value = mConfigBean.getValue( valueName ); 301 hasValue = true; 303 } 305 catch( Exception e ) 306 { 307 } 308 309 return hasValue; 310 } 311 312 313 private boolean 314 hasDescription() 315 { 316 return hasValue( DESCRIPTION_ATTR ); 317 } 318 319 public final String [] 320 getAttributeNames() 321 { 322 return GSetUtil.toStringArray( _getAttributeNames() ); 323 } 324 325 protected Set <String > 326 _getAttributeNames() 327 { 328 final Set <String > attrNames = 329 GSetUtil.newSet( mConfigBean.getAttributeNames() ); 330 331 assert( ! attrNames.contains( null ) ); 333 attrNames.remove( null ); 334 335 if ( hasDescription() ) 336 { 337 attrNames.add( DESCRIPTION_ATTR ); 338 } 339 340 return attrNames; 341 } 342 343 346 protected Class 347 getAttributeClass( final String attrName ) 348 { 349 return String .class; 350 } 351 352 356 protected MBeanAttributeInfo 357 getMBeanAttributeInfo( final String attrName ) 358 { 359 final String description = ""; 360 final boolean isReadable = true; 361 final boolean isWriteable = true; 362 final boolean isIs = false; 363 364 assert( attrName != null ); 365 final MBeanAttributeInfo info = new MBeanAttributeInfo ( 366 attrName, 367 getAttributeClass( attrName ).getName(), 368 description, 369 isReadable, 370 isWriteable, 371 isIs ); 372 373 return info; 374 } 375 376 public MBeanInfo 377 getMBeanInfo() 378 { 379 final List <String > attrNames = ListUtil.newListFromArray( getAttributeNames() ); 380 381 final MBeanOperationInfo [] operationInfos = new MBeanOperationInfo [0]; 382 final MBeanAttributeInfo [] attributeInfos = new MBeanAttributeInfo [ attrNames.size() ]; 383 384 int i = 0; 385 for( final String name : attrNames ) 386 { 387 assert( name != null ); 388 attributeInfos[ i ] = getMBeanAttributeInfo( name ); 389 assert attributeInfos[ i ].getName() != null; 390 391 ++i; 392 } 393 394 final MBeanInfo info = 395 new MBeanInfo ( this.getClass().getName(), 396 "exposes Attributes from ConfigBean", 397 attributeInfos, 398 null, 399 operationInfos, 400 null ); 401 402 for( final MBeanAttributeInfo xxx : info.getAttributes() ) 403 { 404 assert( xxx.getName() != null ); 405 } 406 407 return info; 408 } 409 410 public List <ConfigBeanHelper> 411 getAllChildren() 412 { 413 final List <ConfigBeanHelper> children = new ArrayList <ConfigBeanHelper>(); 414 415 final ConfigBean[] configBeans = mConfigBean.getAllChildBeans(); 416 if ( configBeans != null ) 417 { 418 for( final ConfigBean configBean : configBeans ) 419 { 420 if ( configBean != null ) 421 { 422 final ConfigBeanHelper helper = 423 ConfigBeanHelperFactory.getInstance( getConfigContext() ).getHelper( configBean ); 424 425 children.add( helper ); 426 } 427 } 428 } 429 430 return children; 431 } 432 433 public List <ConfigBeanHelper> 434 getAllChildrenOfType( final String desiredType ) 435 { 436 final List <ConfigBeanHelper> children = getAllChildren(); 437 final List <ConfigBeanHelper> propertyChildren = new ArrayList <ConfigBeanHelper>(); 438 439 for( final ConfigBeanHelper helper : children ) 440 { 441 if ( desiredType.equals( helper.getType() ) ) 442 { 443 propertyChildren.add( helper ); 444 } 445 } 446 447 return propertyChildren; 448 } 449 450 451 452 public Map <String ,ConfigBeanHelper> 453 getSpecialChildMap( final String type ) 454 { 455 if ( ! ("element-property".equals( type ) || 456 "system-property".equals( type )) || 457 "jvm-option".equals( type ) ) 458 { 459 throw new IllegalArgumentException ( type ); 460 } 461 462 final List <ConfigBeanHelper> children = getAllChildrenOfType( type ); 463 final Map <String ,ConfigBeanHelper> m = new HashMap <String ,ConfigBeanHelper>(); 464 465 for( final ConfigBeanHelper helper : children ) 466 { 467 try 468 { 469 final String name = (String )helper.getAttribute( NAME_ATTR ); 470 471 m.put( name, helper ); 472 } 473 catch( Exception e ) 474 { 475 throw new RuntimeException ( 476 "FAILURE getting Name Attribute for property element", e ); 477 } 478 } 479 480 return m; 481 } 482 483 484 487 public AttributeList 488 getProperties() 489 { 490 final Map <String ,ConfigBeanHelper> children = 491 getSpecialChildMap( "element-property" ); 492 493 final AttributeList attrs = new AttributeList (); 494 495 for( final String name : children.keySet() ) 496 { 497 final ConfigBeanHelper helper = children.get( name ); 498 499 final String value = getValueAttributeValue( helper ); 500 501 attrs.add( new Attribute ( name, value ) ); 502 } 503 504 return attrs; 505 } 506 507 508 511 public AttributeList 512 getSystemProperties() 513 { 514 final Map <String ,ConfigBeanHelper> children = 515 getSpecialChildMap( "system-property" ); 516 517 final AttributeList attrs = new AttributeList (); 518 for( final String name : children.keySet() ) 519 { 520 final ConfigBeanHelper helper = children.get( name ); 521 522 final String value = getValueAttributeValue( helper ); 523 524 attrs.add( new Attribute ( name, value ) ); 525 } 526 527 return attrs; 528 } 529 530 532 protected static String 533 getValueAttributeValue( final ConfigBeanHelper helper ) 534 { 535 try 536 { 537 final String value = (String )helper.getAttribute( VALUE_ATTR ); 538 return value; 539 } 540 catch( Exception e ) 541 { 542 throw new RuntimeException ( 543 "FAILURE getting Value Attribute for property element ", e ); 544 } 545 } 546 547 550 public String 551 getPropertyValue( final String propertyName ) 552 { 553 final Map <String ,ConfigBeanHelper> children = 554 getSpecialChildMap( "element-property" ); 555 if ( ! children.containsKey( propertyName ) ) 556 { 557 final String msg = "No such property: " + 558 StringUtil.quote( propertyName ); 559 560 throw new IllegalArgumentException ( msg ); 561 } 562 563 return getValueAttributeValue( children.get( propertyName ) ); 564 } 565 566 569 public String 570 getSystemPropertyValue( final String propertyName ) 571 { 572 final Map <String ,ConfigBeanHelper> children = 573 getSpecialChildMap( "system-property" ); 574 if ( ! children.containsKey( propertyName ) ) 575 { 576 throw new IllegalArgumentException ( propertyName ); 577 } 578 579 return getValueAttributeValue( children.get( propertyName ) ); 580 } 581 582 583 private static void 584 setValueAttributeValue( 585 final ConfigBeanHelper helper, 586 final String value ) 587 { 588 try 589 { 590 helper.setAttribute( VALUE_ATTR, value ); 591 } 592 catch( Exception e ) 593 { 594 throw new RuntimeException ( 595 "FAILURE setting Value Attribute for property element ", e ); 596 } 597 } 598 599 602 public void 603 setProperty( final Attribute attr ) 604 { 605 final Map <String ,ConfigBeanHelper> children = 606 getSpecialChildMap( "element-property" ); 607 608 final ConfigBeanHelper helper = children.get( attr.getName() ); 610 if ( helper == null ) 611 { 612 throw new IllegalArgumentException ( attr.getName() ); 613 } 614 615 setValueAttributeValue( helper, (String )attr.getValue() ); 616 } 617 618 621 public void 622 setSystemProperty( final Attribute attr ) 623 { 624 final Map <String ,ConfigBeanHelper> children = 625 getSpecialChildMap( "system-property" ); 626 627 final ConfigBeanHelper helper = children.get( attr.getName() ); 629 if ( helper == null ) 630 { 631 throw new IllegalArgumentException ( attr.getName() ); 632 } 633 634 setValueAttributeValue( helper, (String )attr.getValue() ); 635 } 636 637 public String 638 getDescription() 639 { 640 String result = (String )getValue( DESCRIPTION_ATTR ); 641 642 return result; 643 } 644 645 public void 646 setDescription( final String description ) 647 { 648 mConfigBean.setValue( DESCRIPTION_ATTR, description ); 649 } 650 651 665 protected Object 666 getValue( final String valueName ) 667 { 668 Object value = null; 669 670 try 671 { 672 value = mConfigBean.getValue( valueName ); 673 } 674 catch( Exception e ) 675 { 676 debug( "ConfigBeanHelper.getValue: Exception accessing value: " + valueName ); 677 } 679 680 return value; 681 } 682 683 public abstract Object 684 handleInvoke( 685 String operationName, 686 Object [] args, 687 String [] types ); 688 689 public void 690 unsupportedOperation( 691 String operationName, 692 Object [] args, 693 String [] types ) 694 { 695 throw new IllegalArgumentException ( 696 "invoke() unknown operation " + operationName + "()" ); 697 } 698 } 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 | Popular Tags |