1 23 24 package com.sun.enterprise.admin.mbeans; 25 26 import com.sun.enterprise.admin.dottedname.*; 27 import com.sun.enterprise.admin.dottedname.valueaccessor.*; 28 29 import javax.management.*; 30 import java.util.*; 31 import java.util.regex.Pattern ; 32 33 34 35 45 abstract public class DottedNameGetSetMBeanBase{ 46 final MBeanServerConnection mConn; 47 final DottedNameRegistry mRegistry; 48 protected final ValueAccessor mValueAccessor; 49 final DottedNameServerInfo mServerInfo; 50 51 public final static char ASSIGNMENT_DELIM = '='; 52 53 63 public 64 DottedNameGetSetMBeanBase( 65 final MBeanServerConnection conn, 66 final DottedNameRegistry registry, 67 final DottedNameServerInfo serverInfo ) 68 { 69 mConn = conn; 70 71 mRegistry = registry; 72 73 mValueAccessor = new AnyValueAccessor( mConn ); 74 75 mServerInfo = serverInfo; 76 } 77 78 79 abstract DottedNameQuery createQuery( ); 80 abstract DottedNameResolver getResolver(); 81 82 83 84 protected MBeanServerConnection 85 getMBS() 86 { 87 return( mConn ); 88 } 89 90 protected DottedNameRegistry 91 getRegistry( ) 92 { 93 return( mRegistry ); 94 } 95 96 protected static ObjectName 97 getTarget( final DottedNameForValue dottedName, final DottedNameResolver resolver) 98 throws Exception 99 { 100 final DottedName prefixDN = dottedName.getPrefix(); 101 102 final ObjectName theObject = resolver.resolveDottedName( prefixDN.toString() ); 103 104 if ( theObject == null ) 105 { 106 final String msg = DottedNameStrings.getString( 107 DottedNameStrings.OBJECT_INSTANCE_NOT_FOUND_KEY, dottedName.toString() ); 108 109 throw new InstanceNotFoundException( msg ); 110 } 111 112 return( theObject ); 113 } 114 115 116 128 129 protected static void 130 logException( final Exception e ) 131 { 132 DottedNameLogger.logException( e ); 133 } 134 135 136 139 Attribute 140 formAttribute( final DottedName prefix, final String valueName, Object value ) 141 { 142 return( new Attribute( prefix + "." + valueName, value ) ); 143 } 144 145 146 149 protected void 150 doGet( final String dottedNameString, final AttributeList attrsOut ) 151 throws Exception 152 { 153 final DottedName dn = getDottedName( dottedNameString ); 155 final DottedNameForValue dnv = new DottedNameForValue( dn ); 156 157 final ObjectName target = getTarget( dnv, getResolver( ) ); 158 final String valueName = dnv.getValueName(); 159 160 final Attribute attr = mValueAccessor.getValue( target, valueName); 161 162 if ( attr != null ) 163 { 164 attrsOut.add( formAttribute( dnv.getPrefix(), valueName, attr.getValue() ) ); 166 } 167 } 168 169 public void 170 doGet( final Set dottedNames, final AttributeList attrsOut ) throws Exception 171 { 172 final Iterator iter = dottedNames.iterator(); 173 174 while ( iter.hasNext() ) 175 { 176 try 177 { 178 doGet( (String )iter.next(), attrsOut ); 179 } 180 catch( Exception e ) 181 { 182 if ( dottedNames.size() == 1 ) 184 { 185 throw e; 186 } 187 else 188 { 189 if ( ! (e instanceof AttributeNotFoundException) ) 192 { 193 logException( e ); 194 } 195 } 196 } 197 } 198 } 199 200 static private final char BACKSLASH = '\\'; 201 204 protected static String 205 convertWildcardStringToJavaFormat( final String wildcardString ) 206 { 207 final int length = wildcardString.length(); 208 final StringBuffer buf = new StringBuffer (); 209 210 for( int i = 0; i < length; ++i ) 211 { 212 final char theChar = wildcardString.charAt( i ); 213 214 if ( theChar == '.' ) 215 { 216 buf.append( "[.]" ); 217 } 218 else if ( theChar == '*' ) 219 { 220 buf.append( ".*" ); 221 } 222 else if ( theChar == BACKSLASH ) 223 { 224 buf.append( "" + BACKSLASH + BACKSLASH ); 225 } 226 else 227 { 228 buf.append( theChar ); 229 } 230 } 231 return( buf.toString() ); 232 } 233 234 235 private boolean 236 startsWithDomain( final String dottedNameExpr ) 237 { 238 return( dottedNameExpr.startsWith( DottedNameAliasSupport.DOMAIN_SCOPE ) ); 239 } 240 241 protected boolean 242 startsWithConfigName( final String dottedNameExpr ) 243 { 244 boolean startsWithConfig = false; 245 246 try 247 { 248 final Iterator iter = mServerInfo.getConfigNames().iterator(); 249 while ( iter.hasNext() ) 250 { 251 final String configName = (String )iter.next(); 252 253 if ( dottedNameExpr.startsWith( configName ) ) 254 { 255 startsWithConfig = true; 256 break; 257 } 258 } 259 } 260 catch( DottedNameServerInfo.UnavailableException e ) 261 { 262 logException( e ); 263 } 264 265 return( startsWithConfig ); 266 } 267 268 281 protected Set 282 getSearchSet( final String dottedNameExpr ) 283 { 284 final DottedName dn = getDottedName( dottedNameExpr ); 285 final String scope = dn.getScope(); 286 Set s = null; 287 288 final boolean isDomain = scope.startsWith( DottedNameAliasSupport.DOMAIN_SCOPE ); 290 291 boolean isConfig = startsWithConfigName( dottedNameExpr ); 293 294 if ( isDomain || isConfig ) 295 { 296 s = getRegistry().allDottedNameStrings(); 297 } 298 else 299 { 300 s = createQuery().allDottedNameStrings(); 302 } 303 304 return( s ); 305 } 306 307 310 protected Set 311 resolveWildcardPrefix( final String dottedNamePrefix) 312 throws DottedNameServerInfo.UnavailableException 313 { 314 Set resolvedSet = Collections.EMPTY_SET; 315 316 if ( DottedName.isWildcardName( dottedNamePrefix ) ) 317 { 318 if ( dottedNamePrefix.equals( "*" ) ) { 320 resolvedSet = createQuery().allDottedNameStrings(); 321 } 322 else 323 { 324 final Set searchSet = getSearchSet( dottedNamePrefix ); 325 326 final String regex = convertWildcardStringToJavaFormat( dottedNamePrefix ); 327 328 final DottedNameWildcardMatcher matcher = 329 new DottedNameWildcardMatcherImpl( searchSet ); 330 331 resolvedSet = matcher.matchDottedNames( regex ); 332 } 333 } 334 else 335 { 336 resolvedSet = Collections.singleton( dottedNamePrefix ); 337 } 338 339 return( resolvedSet ); 340 } 341 342 346 protected Set 347 prefixToValueDottedNamesWild( 348 final DottedNameResolver resolver, 349 final String prefix, 350 final String suffix) 351 { 352 final Set all = new HashSet(); 353 354 try 355 { 356 final ObjectName objectName = resolver.resolveDottedName( prefix ); 357 358 if ( objectName != null ) 359 { 360 Set allValueNames = null; 361 PropertyValueAccessorBase prop_accessor = null; 362 if ( suffix.equals( "*" ) ) 365 { 366 allValueNames = getAllPropertyNames( new PropertyValueAccessor(getMBS()), objectName ); 368 allValueNames.addAll(getAllPropertyNames( new SystemPropertyValueAccessor(getMBS()), objectName )); 369 allValueNames.addAll( getAllValueNames( getMBS(), objectName ) ); 370 } 371 else if ((prop_accessor=(new PrefixedValueSupport(getMBS()).getPrefixedValueAccessor(suffix)))!=null) 372 { 373 allValueNames = getAllPropertyNames( prop_accessor, objectName ); 374 } 375 else 376 { 377 allValueNames = getAllValueNames( getMBS(), objectName ); 379 } 380 381 final Set valuesDottedNames = generateDottedNamesForValues( allValueNames, prefix, suffix ); 382 383 all.addAll( valuesDottedNames ); 384 } 385 } 386 catch( Exception e ) 387 { 388 logException( e ); 389 } 390 391 return( all ); 392 } 393 394 398 Set 399 prefixesToValueDottedNames( 400 final DottedNameResolver resolver, 401 final Set prefixes, 402 final String suffix ) 403 { 404 final Set all = new HashSet(); 405 final Iterator iter = prefixes.iterator(); 406 407 while ( iter.hasNext() ) 408 { 409 final String prefix = (String )iter.next(); 410 411 if ( DottedName.isWildcardName( suffix ) ) 412 { 413 all.addAll( prefixToValueDottedNamesWild( resolver, prefix, suffix ) ); 414 } 415 else 416 { 417 final String dottedName = prefix + "." + suffix; 419 all.add( dottedName ); 420 421 } 422 } 423 424 return( all ); 425 } 426 427 String 428 setToString( final Set s ) 429 { 430 final Iterator iter = s.iterator(); 431 final StringBuffer buf = new StringBuffer (); 432 433 while ( iter.hasNext() ) 434 { 435 buf.append( (String )iter.next() + "\n" ); 436 } 437 438 return( buf.toString() ); 439 } 440 441 442 String 443 normalizeWildcardName( final String name ) 444 { 445 String normalizedName = name; 446 447 if ( name.equals( "*" ) ) 448 { 449 normalizedName = "*.*"; 450 } 451 return( normalizedName ); 452 } 453 454 protected Set 455 resolveInputNames( final String [] names ) 456 throws DottedNameServerInfo.UnavailableException 457 { 458 final Set all = new HashSet(); 459 460 for( int i = 0; i < names.length; ++i ) 461 { 462 String name = names[ i ]; 463 464 if ( DottedName.isWildcardName( name ) ) 465 { 466 name = normalizeWildcardName( name ); 467 468 final DottedName dn = getDottedName( name ); 469 final DottedNameForValue dnv = new DottedNameForValue( dn ); 470 471 final String prefix = dnv.getPrefix().toString(); 472 final String valueName = dnv.getValueName(); 473 474 final Set resolvedPrefixes = resolveWildcardPrefix( prefix ); 475 476 477 final Set newDottedNames = prefixesToValueDottedNames( getResolver( ), 478 resolvedPrefixes, valueName ); 479 480 481 all.addAll( newDottedNames ); 482 } 483 else 484 { 485 all.add( name ); 486 } 487 488 } 489 490 return( all ); 491 } 492 493 protected DottedName 494 getDottedName( final String s ) 495 { 496 return( DottedNameFactory.getInstance().get( s ) ); 497 } 498 499 private final class AttributeComparator implements java.util.Comparator 500 { 501 public int 502 compare( Object o1, Object o2 ) 503 { 504 final Attribute attr1 = (Attribute)o1; 505 final Attribute attr2 = (Attribute)o2; 506 507 return( attr1.getName().compareTo( attr2.getName() ) ); 508 } 509 510 public boolean 511 equals( Object other ) 512 { 513 return( other instanceof AttributeComparator ); 514 } 515 } 516 517 protected Attribute [] 518 sortAttributeList( final AttributeList attrsIn ) 519 { 520 final Attribute [] attrs = new Attribute[ attrsIn.size() ]; 521 attrsIn.toArray( attrs ); 522 523 Arrays.sort( attrs, new AttributeComparator() ); 524 525 return( attrs ); 526 } 527 528 529 Object [] 530 dottedNameGet( final String [] names ) 531 { 532 final Object [] results = new Object [ names.length ]; 533 534 for( int i = 0; i < names.length; ++i ) 535 { 536 results[ i ] = dottedNameGet( names[ i ] ); 537 } 538 539 return( results ); 540 } 541 542 Object 543 dottedNameGet( final String name ) 544 { 545 Object result = null; 546 547 try 548 { 549 final Set all = resolveInputNames( new String [] { name } ); 550 551 final AttributeList attrs = new AttributeList(); 552 doGet( all, attrs ); 553 554 if ( ! DottedName.isWildcardName( name ) ) 555 { 556 assert( attrs.size() == 1 ); 558 result = (Attribute)attrs.get( 0 ); 559 } 560 else 561 { 562 result = sortAttributeList( attrs ); 563 } 564 } 565 catch( Exception e ) 566 { 567 logException( e ); 568 result = e; 570 } 571 572 assert( result != null ); 573 return( result ); 574 } 575 576 577 581 protected static Set 582 getAllPropertyNames( PropertyValueAccessorBase accessor, final ObjectName objectName ) 583 throws Exception 584 { 585 final Set allNames = new HashSet(); 586 587 final String [] propNames = accessor.getAllPropertyNames( objectName, true ); 589 for( int i = 0; i < propNames.length; ++i ) 590 { 591 allNames.add( propNames[ i ] ); 593 } 594 595 return( allNames ); 596 } 597 598 599 604 protected static Set 605 getAllValueNames( final MBeanServerConnection conn, final ObjectName objectName ) 606 throws Exception 607 { 608 final Set allNames = new HashSet(); 609 610 allNames.addAll(getAllPropertyNames( new PropertyValueAccessor(conn), objectName )); 611 allNames.addAll(getAllPropertyNames( new SystemPropertyValueAccessor(conn), objectName )); 612 allNames.addAll( AttributeValueAccessor.getAllAttributeNames( conn, objectName ) ); 613 614 return( allNames ); 615 } 616 617 620 protected static Set 621 generateDottedNamesForValues( 622 final Set valueNames, 623 final String prefix, 624 final String suffix ) 625 { 626 final Iterator iter = valueNames.iterator(); 627 final Set allDottedNameStrings = new HashSet(); 628 629 final Pattern pattern = Pattern.compile( convertWildcardStringToJavaFormat( suffix ) ); 630 631 while ( iter.hasNext() ) 632 { 633 final String valueName = (String )iter.next(); 634 635 if ( pattern.matcher( valueName ).matches() ) 636 { 637 allDottedNameStrings.add( prefix + "." + valueName ); 638 } 639 } 640 641 return( allDottedNameStrings ); 642 } 643 644 645 648 protected Set 649 getAllDescendants( final String namePrefix ) 650 { 651 final Set searchSet = getSearchSet( namePrefix ); 652 653 final String searchPrefix = namePrefix + "."; 655 656 final Set resultSet = new HashSet(); 657 final Iterator iter = searchSet.iterator(); 658 while ( iter.hasNext() ) 659 { 660 final String candidateString = (String )iter.next(); 661 662 if ( candidateString.startsWith( searchPrefix ) ) 663 { 664 resultSet.add( candidateString ); 665 } 666 } 667 668 return( resultSet ); 669 } 670 671 672 676 protected Set 677 getAllImmediateChildren( final String namePrefix ) 678 { 679 final Set allChildren = getAllDescendants( namePrefix ); 680 681 final int numParentParts = getDottedName( namePrefix ).getParts().size(); 682 final Iterator iter = allChildren.iterator(); 683 684 final Set resultSet = new HashSet(); 685 while ( iter.hasNext() ) 686 { 687 final String descendant = (String )iter.next(); 688 689 if ( getDottedName( descendant ).getParts().size() == numParentParts + 1 ) 690 { 691 resultSet.add( descendant ); 692 } 693 } 694 695 return( resultSet ); 696 } 697 698 protected Set 699 getAllTopLevelNames() 700 { 701 final Set all = new HashSet(); 702 final Set searchSet = createQuery().allDottedNameStrings(); 703 704 final Iterator iter = searchSet.iterator(); 706 while ( iter.hasNext() ) 707 { 708 final String candidateString = (String )iter.next(); 709 final DottedName dn = getDottedName( candidateString ); 710 711 if ( dn.getParts().size() == 0 ) 712 { 713 all.add( candidateString ); 714 } 715 } 716 return( all ); 717 } 718 719 protected Set 720 doList( final String [] namePrefixes ) 721 throws DottedNameServerInfo.UnavailableException 722 { 723 final Set all = new HashSet(); 724 725 for( int i = 0; i < namePrefixes.length; ++i ) 726 { 727 final String dottedNamePrefix = namePrefixes[ i ]; 728 729 Set resolved = null; 730 if ( DottedName.isWildcardName( dottedNamePrefix ) ) 731 { 732 resolved = resolveWildcardPrefix( dottedNamePrefix ); 733 } 734 else 735 { 736 resolved = getAllImmediateChildren( dottedNamePrefix ); 738 } 739 740 all.addAll( resolved ); 741 } 742 743 return( all ); 744 } 745 746 public String [] 747 dottedNameList( final String [] namePrefixes ) 748 { 749 Set all = Collections.EMPTY_SET; 750 751 try 752 { 753 all = (namePrefixes.length == 0) ? 755 getAllTopLevelNames() : doList( namePrefixes ); 756 } 757 catch( Exception e ) 758 { 759 logException( e ); 760 } 761 762 final String [] allArray = new String [ all.size() ]; 763 all.toArray( allArray ); 764 765 Arrays.sort( allArray ); 766 return( allArray ); 767 } 768 769 770 protected DottedNameServerInfo 771 getServerInfo( ) 772 { 773 return( mServerInfo ); 774 } 775 } 776 777 778 779 780 781 782 | Popular Tags |