1 50 package org.apache.excalibur.instrument.manager; 51 52 import java.util.Arrays ; 53 import java.util.Comparator ; 54 import java.util.HashMap ; 55 56 import org.apache.avalon.framework.configuration.Configurable; 57 import org.apache.avalon.framework.configuration.Configuration; 58 import org.apache.avalon.framework.configuration.ConfigurationException; 59 import org.apache.avalon.framework.configuration.DefaultConfiguration; 60 import org.apache.avalon.framework.logger.AbstractLogEnabled; 61 62 72 class InstrumentableProxy 73 extends AbstractLogEnabled 74 implements Configurable 75 { 76 77 private DefaultInstrumentManager m_instrumentManager; 78 79 81 private InstrumentableProxy m_parentInstrumentableProxy; 82 83 84 private boolean m_configured; 85 86 87 private boolean m_registered; 88 89 90 private String m_name; 91 92 93 private String m_description; 94 95 96 private InstrumentableDescriptorLocalImpl m_descriptor; 97 98 99 private HashMap m_childInstrumentableProxies = new HashMap (); 100 101 102 private InstrumentableProxy[] m_childInstrumentableProxyArray; 103 104 105 private InstrumentableDescriptorLocal[] m_childInstrumentableDescriptorArray; 106 107 108 private HashMap m_instrumentProxies = new HashMap (); 109 110 111 private InstrumentProxy[] m_instrumentProxyArray; 112 113 114 private InstrumentDescriptorLocal[] m_instrumentDescriptorArray; 115 116 117 private int m_stateVersion; 118 119 122 131 InstrumentableProxy( DefaultInstrumentManager instrumentManager, 132 InstrumentableProxy parentInstrumentableProxy, 133 String name, 134 String description ) 135 { 136 m_instrumentManager = instrumentManager; 137 m_parentInstrumentableProxy = parentInstrumentableProxy; 138 m_name = name; 139 m_description = description; 140 141 m_descriptor = new InstrumentableDescriptorLocalImpl( this ); 143 } 144 145 148 158 public void configure( Configuration configuration ) 159 throws ConfigurationException 160 { 161 synchronized( this ) 162 { 163 m_description = configuration.getAttribute( "description", m_description ); 165 166 if( getLogger().isDebugEnabled() ) 167 { 168 getLogger().debug( "Configuring Instrumentable: " + m_name + " as \"" + 169 m_description + "\"" ); 170 } 171 172 m_configured = true; 173 174 Configuration[] childConfs = configuration.getChildren( "instrumentable" ); 176 for( int i = 0; i < childConfs.length; i++ ) 177 { 178 Configuration childConf = childConfs[ i ]; 179 String childName = childConf.getAttribute( "name" ); 180 String fullChildName = m_name + "." + childName; 181 182 InstrumentableProxy childProxy = new InstrumentableProxy( 183 m_instrumentManager, this, fullChildName, childName ); 184 childProxy.enableLogging( getLogger() ); 185 childProxy.configure( childConf ); 186 m_childInstrumentableProxies.put( fullChildName, childProxy ); 187 188 m_childInstrumentableProxyArray = null; 190 m_childInstrumentableDescriptorArray = null; 191 } 192 193 Configuration[] instrumentConfs = configuration.getChildren( "instrument" ); 195 for( int i = 0; i < instrumentConfs.length; i++ ) 196 { 197 Configuration instrumentConf = instrumentConfs[ i ]; 198 String instrumentName = instrumentConf.getAttribute( "name" ); 199 String fullInstrumentName = m_name + "." + instrumentName; 200 201 InstrumentProxy instrumentProxy = 202 new InstrumentProxy( this, fullInstrumentName, instrumentName ); 203 instrumentProxy.enableLogging( getLogger() ); 204 instrumentProxy.configure( instrumentConf ); 205 m_instrumentProxies.put( fullInstrumentName, instrumentProxy ); 206 207 m_instrumentProxyArray = null; 209 m_instrumentDescriptorArray = null; 210 } 211 } 212 } 213 214 217 222 DefaultInstrumentManager getInstrumentManager() 223 { 224 return m_instrumentManager; 225 } 226 227 233 InstrumentableProxy getParentInstrumentableProxy() 234 { 235 return m_parentInstrumentableProxy; 236 } 237 238 244 boolean isConfigured() 245 { 246 return m_configured; 247 } 248 249 255 boolean isRegistered() 256 { 257 return m_registered; 258 } 259 260 264 void setRegistered() 265 { 266 m_registered = true; 267 } 268 269 277 String getName() 278 { 279 return m_name; 280 } 281 282 289 void setDescription( String description ) 290 { 291 m_description = description; 292 } 293 294 299 String getDescription() 300 { 301 return m_description; 302 } 303 304 309 InstrumentableDescriptorLocal getDescriptor() 310 { 311 return m_descriptor; 312 } 313 314 317 329 void addChildInstrumentableProxy( InstrumentableProxy childInstrumentableProxy ) 330 { 331 synchronized( this ) 332 { 333 m_childInstrumentableProxies.put( 334 childInstrumentableProxy.getName(), childInstrumentableProxy ); 335 336 m_childInstrumentableProxyArray = null; 338 m_childInstrumentableDescriptorArray = null; 339 } 340 341 stateChanged(); 342 } 343 344 354 InstrumentableProxy getChildInstrumentableProxy( String childInstrumentableName ) 355 { 356 synchronized( this ) 357 { 358 String name = childInstrumentableName; 359 while( true ) 360 { 361 InstrumentableProxy proxy = 362 (InstrumentableProxy)m_childInstrumentableProxies.get( name ); 363 if( proxy != null ) 364 { 365 return proxy; 366 } 367 368 int pos = name.lastIndexOf( '.' ); 370 if( pos > 0 ) 371 { 372 name = name.substring( 0, pos ); 373 } 374 else 375 { 376 return null; 377 } 378 } 379 } 380 } 381 382 389 InstrumentableProxy[] getChildInstrumentableProxies() 390 { 391 InstrumentableProxy[] proxies = m_childInstrumentableProxyArray; 392 if( proxies == null ) 393 { 394 proxies = updateChildInstrumentableProxyArray(); 395 } 396 397 return proxies; 398 } 399 400 407 InstrumentableDescriptorLocal[] getChildInstrumentableDescriptors() 408 { 409 InstrumentableDescriptorLocal[] descriptors = m_childInstrumentableDescriptorArray; 410 if( descriptors == null ) 411 { 412 descriptors = updateChildInstrumentableDescriptorArray(); 413 } 414 415 return descriptors; 416 } 417 418 424 private InstrumentableProxy[] updateChildInstrumentableProxyArray() 425 { 426 synchronized( this ) 427 { 428 m_childInstrumentableProxyArray = 429 new InstrumentableProxy[ m_childInstrumentableProxies.size() ]; 430 m_childInstrumentableProxies.values().toArray( m_childInstrumentableProxyArray ); 431 432 Arrays.sort( m_childInstrumentableProxyArray, new Comparator () 436 { 437 public int compare( Object o1, Object o2 ) 438 { 439 return ((InstrumentableProxy)o1).getDescription(). 440 compareTo( ((InstrumentableProxy)o2).getDescription() ); 441 } 442 443 public boolean equals( Object obj ) 444 { 445 return false; 446 } 447 } ); 448 449 return m_childInstrumentableProxyArray; 450 } 451 } 452 453 459 private InstrumentableDescriptorLocal[] updateChildInstrumentableDescriptorArray() 460 { 461 synchronized( this ) 462 { 463 if( m_childInstrumentableProxyArray == null ) 464 { 465 updateChildInstrumentableProxyArray(); 466 } 467 468 m_childInstrumentableDescriptorArray = 469 new InstrumentableDescriptorLocal[ m_childInstrumentableProxyArray.length ]; 470 for( int i = 0; i < m_childInstrumentableProxyArray.length; i++ ) 471 { 472 m_childInstrumentableDescriptorArray[ i ] = 473 m_childInstrumentableProxyArray[ i ].getDescriptor(); 474 } 475 476 return m_childInstrumentableDescriptorArray; 477 } 478 } 479 480 483 494 void addInstrumentProxy( InstrumentProxy instrumentProxy ) 495 { 496 synchronized( this ) 497 { 498 m_instrumentProxies.put( instrumentProxy.getName(), instrumentProxy ); 499 500 m_instrumentProxyArray = null; 502 m_instrumentDescriptorArray = null; 503 } 504 505 stateChanged(); 506 } 507 508 516 InstrumentProxy getInstrumentProxy( String instrumentName ) 517 { 518 synchronized( this ) 519 { 520 String name = instrumentName; 521 while( true ) 522 { 523 InstrumentProxy proxy = (InstrumentProxy)m_instrumentProxies.get( name ); 524 if( proxy != null ) 525 { 526 return proxy; 527 } 528 529 int pos = name.lastIndexOf( '.' ); 531 if( pos > 0 ) 532 { 533 name = name.substring( 0, pos ); 534 } 535 else 536 { 537 return null; 538 } 539 } 540 } 541 } 542 543 548 InstrumentProxy[] getInstrumentProxies() 549 { 550 InstrumentProxy[] proxies = m_instrumentProxyArray; 551 if( proxies == null ) 552 { 553 proxies = updateInstrumentProxyArray(); 554 } 555 return proxies; 556 } 557 558 563 InstrumentDescriptorLocal[] getInstrumentDescriptors() 564 { 565 InstrumentDescriptorLocal[] descriptors = m_instrumentDescriptorArray; 566 if( descriptors == null ) 567 { 568 descriptors = updateInstrumentDescriptorArray(); 569 } 570 return descriptors; 571 } 572 573 582 int getStateVersion() 583 { 584 return m_stateVersion; 585 } 586 587 593 private InstrumentProxy[] updateInstrumentProxyArray() 594 { 595 synchronized( this ) 596 { 597 m_instrumentProxyArray = new InstrumentProxy[ m_instrumentProxies.size() ]; 598 m_instrumentProxies.values().toArray( m_instrumentProxyArray ); 599 600 Arrays.sort( m_instrumentProxyArray, new Comparator () 604 { 605 public int compare( Object o1, Object o2 ) 606 { 607 return ((InstrumentProxy)o1).getDescription(). 608 compareTo( ((InstrumentProxy)o2).getDescription() ); 609 } 610 611 public boolean equals( Object obj ) 612 { 613 return false; 614 } 615 } ); 616 617 return m_instrumentProxyArray; 618 } 619 } 620 621 627 private InstrumentDescriptorLocal[] updateInstrumentDescriptorArray() 628 { 629 synchronized( this ) 630 { 631 if( m_instrumentProxyArray == null ) 632 { 633 updateInstrumentProxyArray(); 634 } 635 636 m_instrumentDescriptorArray = 637 new InstrumentDescriptorLocal[ m_instrumentProxyArray.length ]; 638 for( int i = 0; i < m_instrumentProxyArray.length; i++ ) 639 { 640 m_instrumentDescriptorArray[ i ] = m_instrumentProxyArray[ i ].getDescriptor(); 641 } 642 643 return m_instrumentDescriptorArray; 644 } 645 } 646 647 653 Configuration saveState() 654 { 655 boolean empty = true; 656 DefaultConfiguration state = new DefaultConfiguration( "instrumentable", "-" ); 657 state.setAttribute( "name", m_name ); 658 659 InstrumentableProxy[] childProxies = getChildInstrumentableProxies(); 661 for( int i = 0; i < childProxies.length; i++ ) 662 { 663 Configuration childState = childProxies[ i ].saveState(); 664 if ( childState != null ) 665 { 666 empty = false; 667 state.addChild( childState ); 668 } 669 } 670 671 InstrumentProxy[] proxies = getInstrumentProxies(); 673 for( int i = 0; i < proxies.length; i++ ) 674 { 675 Configuration childState = proxies[ i ].saveState(); 676 if ( childState != null ) 677 { 678 empty = false; 679 state.addChild( childState ); 680 } 681 } 682 683 if ( empty ) 685 { 686 state = null; 687 } 688 return state; 689 } 690 691 699 void loadState( Configuration state ) throws ConfigurationException 700 { 701 synchronized( this ) 702 { 703 Configuration[] childConfs = state.getChildren( "instrumentable" ); 705 for( int i = 0; i < childConfs.length; i++ ) 706 { 707 Configuration childConf = childConfs[ i ]; 708 String fullChildName = childConf.getAttribute( "name" ); 709 InstrumentableProxy childProxy = getChildInstrumentableProxy( fullChildName ); 710 if( childProxy == null ) 711 { 712 String childName = ( fullChildName.startsWith( m_name + "." ) ? 717 fullChildName.substring( m_name.length() + 1 ) : "BADNAME." + fullChildName ); 718 719 childProxy = new InstrumentableProxy( 720 m_instrumentManager, this, fullChildName, childName ); 721 childProxy.enableLogging( getLogger() ); 722 m_childInstrumentableProxies.put( fullChildName, childProxy ); 723 724 m_childInstrumentableProxyArray = null; 726 m_childInstrumentableDescriptorArray = null; 727 } 728 childProxy.loadState( childConf ); 729 } 730 731 Configuration[] instrumentConfs = state.getChildren( "instrument" ); 733 for( int i = 0; i < instrumentConfs.length; i++ ) 734 { 735 Configuration instrumentConf = instrumentConfs[ i ]; 736 String fullInstrumentName = instrumentConf.getAttribute( "name" ); 737 InstrumentProxy instrumentProxy = getInstrumentProxy( fullInstrumentName ); 738 if( instrumentProxy == null ) 739 { 740 String instrumentName = ( fullInstrumentName.startsWith( m_name + "." ) ? 744 fullInstrumentName.substring( m_name.length() + 1 ) : "BADNAME." + fullInstrumentName ); 745 746 instrumentProxy = 747 new InstrumentProxy( this, fullInstrumentName, instrumentName ); 748 instrumentProxy.enableLogging( getLogger() ); 749 m_instrumentProxies.put( fullInstrumentName, instrumentProxy ); 750 751 m_instrumentProxyArray = null; 753 m_instrumentDescriptorArray = null; 754 } 755 instrumentProxy.loadState( instrumentConf ); 756 } 757 } 758 759 stateChanged(); 760 } 761 762 765 protected void stateChanged() 766 { 767 m_stateVersion++; 768 769 if ( m_parentInstrumentableProxy == null ) 771 { 772 m_instrumentManager.stateChanged(); 774 } 775 else 776 { 777 m_parentInstrumentableProxy.stateChanged(); 778 } 779 } 780 } 781 | Popular Tags |