1 19 20 package org.apache.excalibur.instrument.manager.impl; 21 22 import java.io.PrintWriter ; 23 import java.util.Arrays ; 24 import java.util.Comparator ; 25 import java.util.HashMap ; 26 27 import org.apache.avalon.framework.configuration.Configurable; 28 import org.apache.avalon.framework.configuration.Configuration; 29 import org.apache.avalon.framework.configuration.ConfigurationException; 30 import org.apache.avalon.framework.logger.AbstractLogEnabled; 31 32 import org.apache.excalibur.instrument.manager.InstrumentableDescriptor; 33 import org.apache.excalibur.instrument.manager.InstrumentDescriptor; 34 35 45 class InstrumentableProxy 46 extends AbstractLogEnabled 47 implements Configurable 48 { 49 50 private DefaultInstrumentManagerImpl m_instrumentManager; 51 52 54 private InstrumentableProxy m_parentInstrumentableProxy; 55 56 57 private boolean m_configured; 58 59 60 private boolean m_registered; 61 62 63 private String m_name; 64 65 66 private String m_description; 67 68 69 private InstrumentableDescriptorImpl m_descriptor; 70 71 72 private HashMap m_childInstrumentableProxies = new HashMap (); 73 74 75 private InstrumentableProxy[] m_childInstrumentableProxyArray; 76 77 78 private InstrumentableDescriptor[] m_childInstrumentableDescriptorArray; 79 80 81 private HashMap m_instrumentProxies = new HashMap (); 82 83 84 private InstrumentProxy[] m_instrumentProxyArray; 85 86 87 private InstrumentDescriptor[] m_instrumentDescriptorArray; 88 89 90 private int m_stateVersion; 91 92 95 104 InstrumentableProxy( DefaultInstrumentManagerImpl instrumentManager, 105 InstrumentableProxy parentInstrumentableProxy, 106 String name, 107 String description ) 108 { 109 m_instrumentManager = instrumentManager; 110 m_parentInstrumentableProxy = parentInstrumentableProxy; 111 m_name = name; 112 m_description = description; 113 114 m_descriptor = new InstrumentableDescriptorImpl( this ); 116 } 117 118 121 131 public void configure( Configuration configuration ) 132 throws ConfigurationException 133 { 134 synchronized( this ) 135 { 136 m_description = configuration.getAttribute( "description", m_description ); 138 139 if( getLogger().isDebugEnabled() ) 140 { 141 getLogger().debug( "Configuring Instrumentable: " + m_name + " as \"" + 142 m_description + "\"" ); 143 } 144 145 m_configured = true; 146 147 Configuration[] childConfs = configuration.getChildren( "instrumentable" ); 149 for( int i = 0; i < childConfs.length; i++ ) 150 { 151 Configuration childConf = childConfs[ i ]; 152 String childName = childConf.getAttribute( "name" ); 153 String fullChildName = m_name + "." + childName; 154 155 InstrumentableProxy childProxy = getChildInstrumentableProxy( fullChildName ); 157 if( childProxy == null ) 158 { 159 childProxy = new InstrumentableProxy( 160 m_instrumentManager, this, fullChildName, childName ); 161 childProxy.enableLogging( getLogger() ); 162 m_instrumentManager.incrementInstrumentableCount(); 163 m_childInstrumentableProxies.put( fullChildName, childProxy ); 164 165 m_childInstrumentableProxyArray = null; 167 m_childInstrumentableDescriptorArray = null; 168 } 169 childProxy.configure( childConf ); 171 } 172 173 Configuration[] instrumentConfs = configuration.getChildren( "instrument" ); 175 for( int i = 0; i < instrumentConfs.length; i++ ) 176 { 177 Configuration instrumentConf = instrumentConfs[ i ]; 178 String instrumentName = instrumentConf.getAttribute( "name" ); 179 String fullInstrumentName = m_name + "." + instrumentName; 180 181 InstrumentProxy instrumentProxy = getInstrumentProxy( fullInstrumentName ); 183 if ( instrumentProxy == null ) 184 { 185 instrumentProxy = 186 new InstrumentProxy( this, fullInstrumentName, instrumentName ); 187 instrumentProxy.enableLogging( getLogger() ); 188 m_instrumentManager.incrementInstrumentCount(); 189 m_instrumentProxies.put( fullInstrumentName, instrumentProxy ); 190 191 m_instrumentProxyArray = null; 193 m_instrumentDescriptorArray = null; 194 } 195 instrumentProxy.configure( instrumentConf ); 197 } 198 } 199 } 200 201 204 209 DefaultInstrumentManagerImpl getInstrumentManager() 210 { 211 return m_instrumentManager; 212 } 213 214 220 InstrumentableProxy getParentInstrumentableProxy() 221 { 222 return m_parentInstrumentableProxy; 223 } 224 225 231 boolean isConfigured() 232 { 233 return m_configured; 234 } 235 236 242 boolean isRegistered() 243 { 244 return m_registered; 245 } 246 247 251 void setRegistered() 252 { 253 if ( !m_registered ) 254 { 255 m_registered = true; 256 stateChanged(); 257 } 258 } 259 260 267 String getName() 268 { 269 return m_name; 270 } 271 272 279 void setDescription( String description ) 280 { 281 String oldDescription = m_description; if ( ( oldDescription == description ) || ( ( description != null ) && description.equals( oldDescription ) ) ) 283 { 284 } 286 else 287 { 288 m_description = description; 289 stateChanged(); 290 } 291 } 292 293 298 String getDescription() 299 { 300 return m_description; 301 } 302 303 308 InstrumentableDescriptor getDescriptor() 309 { 310 return m_descriptor; 311 } 312 313 316 327 void addChildInstrumentableProxy( InstrumentableProxy childInstrumentableProxy ) 328 { 329 synchronized( this ) 330 { 331 m_childInstrumentableProxies.put( 332 childInstrumentableProxy.getName(), childInstrumentableProxy ); 333 334 m_childInstrumentableProxyArray = null; 336 m_childInstrumentableDescriptorArray = null; 337 } 338 339 stateChanged(); 340 } 341 342 345 InstrumentableProxy getChildInstrumentableProxy( String childInstrumentableName, 346 boolean create ) 347 { 348 synchronized( this ) 349 { 350 InstrumentableProxy childInstrumentableProxy = 351 (InstrumentableProxy)m_childInstrumentableProxies.get( childInstrumentableName ); 352 if ( ( childInstrumentableProxy == null ) && create ) 353 { 354 int pos = childInstrumentableName.lastIndexOf( '.' ); 357 String childName; 358 if ( pos >= 0 ) 359 { 360 childName = childInstrumentableName.substring( pos + 1 ); 361 } 362 else 363 { 364 childName = childInstrumentableName; 365 } 366 367 childInstrumentableProxy = new InstrumentableProxy( 368 m_instrumentManager, this, childInstrumentableName, childName ); 369 childInstrumentableProxy.enableLogging( getLogger() ); 370 m_instrumentManager.incrementInstrumentableCount(); 371 m_childInstrumentableProxies.put( 372 childInstrumentableName, childInstrumentableProxy ); 373 374 m_childInstrumentableProxyArray = null; 376 m_childInstrumentableDescriptorArray = null; 377 } 378 379 return childInstrumentableProxy; 381 } 382 } 383 384 394 InstrumentableProxy getChildInstrumentableProxy( String childInstrumentableName ) 395 { 396 synchronized( this ) 397 { 398 String name = childInstrumentableName; 399 while( true ) 400 { 401 InstrumentableProxy proxy = 402 (InstrumentableProxy)m_childInstrumentableProxies.get( name ); 403 if( proxy != null ) 404 { 405 return proxy; 406 } 407 408 int pos = name.lastIndexOf( '.' ); 410 if( pos > 0 ) 411 { 412 name = name.substring( 0, pos ); 413 } 414 else 415 { 416 return null; 417 } 418 } 419 } 420 } 421 422 429 InstrumentableProxy[] getChildInstrumentableProxies() 430 { 431 InstrumentableProxy[] proxies = m_childInstrumentableProxyArray; 432 if( proxies == null ) 433 { 434 proxies = updateChildInstrumentableProxyArray(); 435 } 436 437 return proxies; 438 } 439 440 447 InstrumentableDescriptor[] getChildInstrumentableDescriptors() 448 { 449 InstrumentableDescriptor[] descriptors = m_childInstrumentableDescriptorArray; 450 if( descriptors == null ) 451 { 452 descriptors = updateChildInstrumentableDescriptorArray(); 453 } 454 455 return descriptors; 456 } 457 458 464 private InstrumentableProxy[] updateChildInstrumentableProxyArray() 465 { 466 synchronized( this ) 467 { 468 InstrumentableProxy[] childInstrumentableProxyArray = 469 new InstrumentableProxy[ m_childInstrumentableProxies.size() ]; 470 m_childInstrumentableProxies.values().toArray( childInstrumentableProxyArray ); 471 472 Arrays.sort( childInstrumentableProxyArray, new Comparator () 476 { 477 public int compare( Object o1, Object o2 ) 478 { 479 return ((InstrumentableProxy)o1).getDescription(). 480 compareTo( ((InstrumentableProxy)o2).getDescription() ); 481 } 482 483 public boolean equals( Object obj ) 484 { 485 return false; 486 } 487 } ); 488 489 m_childInstrumentableProxyArray = childInstrumentableProxyArray; 492 493 return childInstrumentableProxyArray; 494 } 495 } 496 497 503 private InstrumentableDescriptor[] updateChildInstrumentableDescriptorArray() 504 { 505 synchronized( this ) 506 { 507 InstrumentableProxy[] childInstrumentableProxyArray = m_childInstrumentableProxyArray; 511 if( childInstrumentableProxyArray == null ) 512 { 513 childInstrumentableProxyArray = updateChildInstrumentableProxyArray(); 514 } 515 516 InstrumentableDescriptor[] childInstrumentableDescriptorArray = 517 new InstrumentableDescriptor[ childInstrumentableProxyArray.length ]; 518 for( int i = 0; i < childInstrumentableProxyArray.length; i++ ) 519 { 520 childInstrumentableDescriptorArray[ i ] = 521 childInstrumentableProxyArray[ i ].getDescriptor(); 522 } 523 524 m_childInstrumentableDescriptorArray = childInstrumentableDescriptorArray; 527 528 return childInstrumentableDescriptorArray; 529 } 530 } 531 532 535 546 void addInstrumentProxy( InstrumentProxy instrumentProxy ) 547 { 548 synchronized( this ) 549 { 550 m_instrumentProxies.put( instrumentProxy.getName(), instrumentProxy ); 551 552 m_instrumentProxyArray = null; 554 m_instrumentDescriptorArray = null; 555 } 556 557 stateChanged(); 558 } 559 560 563 InstrumentProxy getInstrumentProxy( String instrumentName, boolean create ) 564 { 565 synchronized( this ) 566 { 567 InstrumentProxy instrumentProxy = 568 (InstrumentProxy)m_instrumentProxies.get( instrumentName ); 569 if ( ( instrumentProxy == null ) && create ) 570 { 571 int pos = instrumentName.lastIndexOf( '.' ); 574 String instName; 575 if ( pos >= 0 ) 576 { 577 instName = instrumentName.substring( pos + 1 ); 578 } 579 else 580 { 581 instName = instrumentName; 582 } 583 584 instrumentProxy = new InstrumentProxy( this, instrumentName, instName ); 585 instrumentProxy.enableLogging( getLogger() ); 586 m_instrumentManager.incrementInstrumentCount(); 587 m_instrumentProxies.put( instrumentName, instrumentProxy ); 588 589 m_instrumentProxyArray = null; 591 m_instrumentDescriptorArray = null; 592 } 593 594 return instrumentProxy; 596 } 597 } 598 599 607 InstrumentProxy getInstrumentProxy( String instrumentName ) 608 { 609 synchronized( this ) 610 { 611 String name = instrumentName; 612 while( true ) 613 { 614 InstrumentProxy proxy = (InstrumentProxy)m_instrumentProxies.get( name ); 615 if( proxy != null ) 616 { 617 return proxy; 618 } 619 620 int pos = name.lastIndexOf( '.' ); 622 if( pos > 0 ) 623 { 624 name = name.substring( 0, pos ); 625 } 626 else 627 { 628 return null; 629 } 630 } 631 } 632 } 633 634 639 InstrumentProxy[] getInstrumentProxies() 640 { 641 InstrumentProxy[] proxies = m_instrumentProxyArray; 642 if( proxies == null ) 643 { 644 proxies = updateInstrumentProxyArray(); 645 } 646 return proxies; 647 } 648 649 654 InstrumentDescriptor[] getInstrumentDescriptors() 655 { 656 InstrumentDescriptor[] descriptors = m_instrumentDescriptorArray; 657 if( descriptors == null ) 658 { 659 descriptors = updateInstrumentDescriptorArray(); 660 } 661 return descriptors; 662 } 663 664 673 int getStateVersion() 674 { 675 return m_stateVersion; 676 } 677 678 684 private InstrumentProxy[] updateInstrumentProxyArray() 685 { 686 synchronized( this ) 687 { 688 InstrumentProxy[] instrumentProxyArray = 689 new InstrumentProxy[ m_instrumentProxies.size() ]; 690 m_instrumentProxies.values().toArray( instrumentProxyArray ); 691 692 Arrays.sort( instrumentProxyArray, new Comparator () 696 { 697 public int compare( Object o1, Object o2 ) 698 { 699 return ((InstrumentProxy)o1).getDescription(). 700 compareTo( ((InstrumentProxy)o2).getDescription() ); 701 } 702 703 public boolean equals( Object obj ) 704 { 705 return false; 706 } 707 } ); 708 709 m_instrumentProxyArray = instrumentProxyArray; 712 713 return instrumentProxyArray; 714 } 715 } 716 717 723 private InstrumentDescriptor[] updateInstrumentDescriptorArray() 724 { 725 synchronized( this ) 726 { 727 InstrumentProxy[] instrumentProxyArray = m_instrumentProxyArray; 731 if( instrumentProxyArray == null ) 732 { 733 instrumentProxyArray = updateInstrumentProxyArray(); 734 } 735 736 InstrumentDescriptor[] instrumentDescriptorArray = 737 new InstrumentDescriptor[ instrumentProxyArray.length ]; 738 for( int i = 0; i < instrumentProxyArray.length; i++ ) 739 { 740 instrumentDescriptorArray[ i ] = instrumentProxyArray[ i ].getDescriptor(); 741 } 742 743 m_instrumentDescriptorArray = instrumentDescriptorArray; 746 747 return instrumentDescriptorArray; 748 } 749 } 750 751 756 void writeState( PrintWriter out ) 757 { 758 761 InstrumentableProxy[] childProxies = getChildInstrumentableProxies(); 763 for( int i = 0; i < childProxies.length; i++ ) 764 { 765 childProxies[i].writeState( out ); 766 } 767 768 InstrumentProxy[] proxies = getInstrumentProxies(); 770 for( int i = 0; i < proxies.length; i++ ) 771 { 772 proxies[i].writeState( out ); 773 } 774 } 775 776 779 protected void stateChanged() 780 { 781 m_stateVersion++; 782 783 if ( m_parentInstrumentableProxy == null ) 785 { 786 m_instrumentManager.stateChanged(); 788 } 789 else 790 { 791 m_parentInstrumentableProxy.stateChanged(); 792 } 793 } 794 } 795 | Popular Tags |