1 50 package org.apache.excalibur.instrument.manager; 51 52 import java.io.ByteArrayOutputStream ; 53 import java.io.File ; 54 import java.io.FileInputStream ; 55 import java.io.FileOutputStream ; 56 import java.io.InputStream ; 57 import java.io.OutputStream ; 58 import java.util.ArrayList ; 59 import java.util.Arrays ; 60 import java.util.Comparator ; 61 import java.util.HashMap ; 62 import java.util.Iterator ; 63 64 import org.apache.avalon.framework.activity.Disposable; 65 import org.apache.avalon.framework.activity.Initializable; 66 import org.apache.avalon.framework.configuration.Configurable; 67 import org.apache.avalon.framework.configuration.Configuration; 68 import org.apache.avalon.framework.configuration.ConfigurationException; 69 import org.apache.avalon.framework.configuration.DefaultConfiguration; 70 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 71 import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer; 72 import org.apache.avalon.framework.container.ContainerUtil; 73 import org.apache.avalon.framework.logger.AbstractLogEnabled; 74 import org.apache.avalon.framework.logger.Logger; 75 import org.apache.avalon.framework.service.ServiceException; 76 import org.apache.excalibur.instrument.AbstractInstrument; 77 import org.apache.excalibur.instrument.CounterInstrument; 78 import org.apache.excalibur.instrument.Instrument; 79 import org.apache.excalibur.instrument.InstrumentManager; 80 import org.apache.excalibur.instrument.Instrumentable; 81 import org.apache.excalibur.instrument.ValueInstrument; 82 import org.apache.excalibur.instrument.manager.interfaces.InstrumentManagerClient; 83 import org.apache.excalibur.instrument.manager.interfaces.NoSuchInstrumentException; 84 import org.apache.excalibur.instrument.manager.interfaces.NoSuchInstrumentSampleException; 85 import org.apache.excalibur.instrument.manager.interfaces.NoSuchInstrumentableException; 86 87 93 public class DefaultInstrumentManager 94 extends AbstractLogEnabled 95 implements Configurable, Initializable, Disposable, InstrumentManager, 96 Instrumentable, Runnable 97 { 98 public static final String INSTRUMENT_TOTAL_MEMORY = "total-memory"; 99 public static final String INSTRUMENT_FREE_MEMORY = "free-memory"; 100 public static final String INSTRUMENT_MEMORY = "memory"; 101 public static final String INSTRUMENT_ACTIVE_THREAD_COUNT = "active-thread-count"; 102 103 104 private String m_name; 105 106 107 private String m_description; 108 109 110 private Configuration m_configuration; 111 112 113 private ArrayList m_connectors = new ArrayList (); 114 115 116 private File m_stateFile; 117 118 119 private long m_stateInterval; 120 121 122 private long m_lastStateSave; 123 124 125 private Object m_semaphore = new Object (); 126 127 128 private HashMap m_instrumentableProxies = new HashMap (); 129 130 131 private InstrumentableProxy[] m_instrumentableProxyArray; 132 133 134 private InstrumentableDescriptorLocal[] m_instrumentableDescriptorArray; 135 136 137 private ArrayList m_leasedInstrumentSamples = new ArrayList (); 138 139 140 private InstrumentSample[] m_leasedInstrumentSampleArray; 141 142 146 private Thread m_runner; 147 148 149 private String m_instrumentableName = "instrument-manager"; 150 151 152 private ValueInstrument m_totalMemoryInstrument; 153 154 155 private ValueInstrument m_freeMemoryInstrument; 156 157 158 private ValueInstrument m_memoryInstrument; 159 160 161 private ValueInstrument m_activeThreadCountInstrument; 162 163 164 private int m_stateVersion; 165 166 169 177 public DefaultInstrumentManager( String name ) 178 { 179 this(); 180 } 181 182 185 public DefaultInstrumentManager() 186 { 187 m_totalMemoryInstrument = new ValueInstrument( INSTRUMENT_TOTAL_MEMORY ); 189 m_freeMemoryInstrument = new ValueInstrument( INSTRUMENT_FREE_MEMORY ); 190 m_memoryInstrument = new ValueInstrument( INSTRUMENT_MEMORY ); 191 m_activeThreadCountInstrument = new ValueInstrument( INSTRUMENT_ACTIVE_THREAD_COUNT ); 192 } 193 194 197 204 public void configure( Configuration configuration ) 205 throws ConfigurationException 206 { 207 synchronized( m_semaphore ) 208 { 209 m_configuration = configuration; 210 211 m_name = configuration.getChild( "name" ).getValue( "instrument-manager" ); 213 m_description = configuration.getChild( "description" ).getValue( m_name ); 214 215 Logger connLogger = getLogger().getChildLogger( "connector" ); 217 218 Configuration connectorsConf = configuration.getChild( "connectors" ); 220 Configuration[] connectorConfs = 221 connectorsConf.getChildren( "connector" ); 222 for( int i = 0; i < connectorConfs.length; i++ ) 223 { 224 Configuration connectorConf = connectorConfs[ i ]; 225 String className = connectorConf.getAttribute( "class" ); 226 if ( className.equals( "altrmi" ) ) 228 { 229 className = "org.apache.excalibur.instrument.manager.altrmi." 230 + "InstrumentManagerAltrmiConnector"; 231 } 232 233 try 235 { 236 Class clazz = Class.forName( className ); 237 InstrumentManagerConnector connector = 238 (InstrumentManagerConnector)clazz.newInstance(); 239 240 connector.setInstrumentManager( this ); 242 ContainerUtil.enableLogging( connector, connLogger ); 243 ContainerUtil.configure( connector, connectorConf ); 244 ContainerUtil.start( connector ); 245 246 m_connectors.add( connector ); 247 } 248 catch ( Exception e ) 249 { 250 throw new ConfigurationException( "Unable to create connector because: " 251 + e ); 252 } 253 } 254 255 Configuration instrumentablesConf = configuration.getChild( "instrumentables" ); 257 Configuration[] instrumentableConfs = 258 instrumentablesConf.getChildren( "instrumentable" ); 259 for( int i = 0; i < instrumentableConfs.length; i++ ) 260 { 261 Configuration instrumentableConf = instrumentableConfs[ i ]; 262 String instrumentableName = instrumentableConf.getAttribute( "name" ); 263 264 InstrumentableProxy instrumentableProxy = new InstrumentableProxy( 265 this, null, instrumentableName, instrumentableName ); 266 instrumentableProxy.enableLogging( getLogger() ); 267 instrumentableProxy.configure( instrumentableConf ); 268 m_instrumentableProxies.put( instrumentableName, instrumentableProxy ); 269 270 m_instrumentableProxyArray = null; 272 m_instrumentableDescriptorArray = null; 273 } 274 275 Configuration stateFileConf = configuration.getChild( "state-file" ); 277 m_stateInterval = stateFileConf.getAttributeAsLong( "interval", 60000 ); 278 279 String stateFile = stateFileConf.getValue( null ); 280 if( stateFile != null ) 281 { 282 m_stateFile = new File ( stateFile ); 283 if( m_stateFile.exists() ) 284 { 285 try 286 { 287 loadStateFromFile( m_stateFile ); 288 } 289 catch( Exception e ) 290 { 291 getLogger().error( 292 "Unable to load the instrument manager state. The configuration " + 293 "may have been corruptped. A backup may have been made in the same " + 294 "directory when it was saved.", e ); 295 } 296 } 297 } 298 } 299 } 300 301 304 309 public void initialize() 310 throws Exception 311 { 312 registerInstrumentable( this, getInstrumentableName() ); 314 315 if( m_runner == null ) 316 { 317 m_runner = new Thread ( this, "InstrumentManagerRunner" ); 318 m_runner.start(); 319 } 320 } 321 322 325 328 public void dispose() 329 { 330 if( m_runner != null ) 331 { 332 m_runner = null; 333 } 334 335 for ( Iterator iter = m_connectors.iterator(); iter.hasNext(); ) 337 { 338 InstrumentManagerConnector connector = (InstrumentManagerConnector)iter.next(); 339 try 340 { 341 ContainerUtil.stop( connector ); 342 ContainerUtil.dispose( connector ); 343 } 344 catch ( Exception e ) 345 { 346 getLogger().error( "Encountered an unexpected error shutting down a connector", e ); 347 } 348 } 349 350 saveState(); 351 } 352 353 356 367 public void registerInstrumentable( Instrumentable instrumentable, String instrumentableName ) 368 throws Exception 369 { 370 getLogger().debug( "Registering Instrumentable: " + instrumentableName ); 371 372 synchronized( m_semaphore ) 373 { 374 int pos = instrumentableName.indexOf( '.' ); 378 if ( pos >= 0 ) 379 { 380 String parentName = instrumentableName.substring( 0, pos ); 381 String childName = 382 instrumentableName.substring( pos + 1 ); 383 InstrumentableProxy instrumentableProxy = 384 (InstrumentableProxy)m_instrumentableProxies.get( parentName ); 385 if( instrumentableProxy == null ) 386 { 387 instrumentableProxy = new InstrumentableProxy( 389 this, null, parentName, parentName ); 390 instrumentableProxy.enableLogging( getLogger() ); 391 m_instrumentableProxies.put( parentName, instrumentableProxy ); 394 395 m_instrumentableProxyArray = null; 397 m_instrumentableDescriptorArray = null; 398 399 registerDummyInstrumentableInner( 401 instrumentable, instrumentableProxy, parentName, childName ); 402 } 403 else 404 { 405 registerDummyInstrumentableInner( 407 instrumentable, instrumentableProxy, parentName, childName ); 408 } 409 } else { 410 InstrumentableProxy instrumentableProxy = 415 (InstrumentableProxy)m_instrumentableProxies.get( instrumentableName ); 416 if( instrumentableProxy == null ) 417 { 418 instrumentableProxy = new InstrumentableProxy( 420 this, null, instrumentableName, instrumentableName ); 421 instrumentableProxy.enableLogging( getLogger() ); 422 m_instrumentableProxies.put( instrumentableName, instrumentableProxy ); 425 426 m_instrumentableProxyArray = null; 428 m_instrumentableDescriptorArray = null; 429 430 registerInstrumentableInner( 432 instrumentable, instrumentableProxy, instrumentableName ); 433 } 434 else 435 { 436 registerInstrumentableInner( 438 instrumentable, instrumentableProxy, instrumentableName ); 439 } 440 } 441 } 442 443 stateChanged(); 444 } 445 446 447 450 455 public String getName() 456 { 457 return m_name; 458 } 459 460 465 public String getDescription() 466 { 467 return m_description; 468 } 469 470 481 public InstrumentableDescriptorLocal getInstrumentableDescriptor( String instrumentableName ) 482 throws NoSuchInstrumentableException 483 { 484 InstrumentableProxy proxy = getInstrumentableProxy( instrumentableName ); 485 if( proxy == null ) 486 { 487 throw new NoSuchInstrumentableException( 488 "No instrumentable can be found using name: " + instrumentableName ); 489 } 490 491 return proxy.getDescriptor(); 492 } 493 494 501 public InstrumentableDescriptorLocal[] getInstrumentableDescriptors() 502 { 503 InstrumentableDescriptorLocal[] descriptors = m_instrumentableDescriptorArray; 504 if( descriptors == null ) 505 { 506 descriptors = updateInstrumentableDescriptorArray(); 507 } 508 return descriptors; 509 } 510 511 522 public InstrumentableDescriptorLocal locateInstrumentableDescriptor( String instrumentableName ) 523 throws NoSuchInstrumentableException 524 { 525 InstrumentableProxy instrumentableProxy = 526 locateDeepestInstrumentableProxy( instrumentableName ); 527 if ( instrumentableProxy != null ) 528 { 529 if ( instrumentableProxy.getName().equals( instrumentableName ) ) 530 { 531 return instrumentableProxy.getDescriptor(); 533 } 534 } 535 536 throw new NoSuchInstrumentableException( 538 "No instrumentable can be found with the name: " + instrumentableName ); 539 } 540 541 551 public InstrumentDescriptorLocal locateInstrumentDescriptor( String instrumentName ) 552 throws NoSuchInstrumentException 553 { 554 InstrumentableProxy instrumentableProxy = 555 locateDeepestInstrumentableProxy( instrumentName ); 556 if ( instrumentableProxy != null ) 557 { 558 InstrumentProxy instrumentProxy = 560 instrumentableProxy.getInstrumentProxy( instrumentName ); 561 if ( instrumentProxy != null ) 562 { 563 if ( instrumentProxy.getName().equals( instrumentName ) ) 564 { 565 return instrumentProxy.getDescriptor(); 567 } 568 } 569 } 570 571 throw new NoSuchInstrumentException( 573 "No instrument can be found with the name: " + instrumentName ); 574 } 575 576 587 public InstrumentSampleDescriptorLocal locateInstrumentSampleDescriptor( String sampleName ) 588 throws NoSuchInstrumentSampleException 589 { 590 InstrumentableProxy instrumentableProxy = 591 locateDeepestInstrumentableProxy( sampleName ); 592 if ( instrumentableProxy != null ) 593 { 594 InstrumentProxy instrumentProxy = 596 instrumentableProxy.getInstrumentProxy( sampleName ); 597 if ( instrumentProxy != null ) 598 { 599 InstrumentSample sample = instrumentProxy.getInstrumentSample( sampleName ); 601 if ( sample != null ) 602 { 603 if ( sample.getName().equals( sampleName ) ) 604 { 605 return sample.getDescriptor(); 607 } 608 } 609 } 610 } 611 612 throw new NoSuchInstrumentException( 614 "No instrument sample can be found with the name: " + sampleName ); 615 } 616 617 626 int getStateVersion() 627 { 628 return m_stateVersion; 629 } 630 631 634 public void invokeGarbageCollection() 635 { 636 System.gc(); 637 } 638 639 640 641 648 public void loadStateFromFile( File stateFile ) 649 throws Exception 650 { 651 long now = System.currentTimeMillis(); 652 getLogger().debug( "Loading Instrument Manager state from: " + 653 stateFile.getAbsolutePath() ); 654 655 FileInputStream is = new FileInputStream ( stateFile ); 656 try 657 { 658 loadStateFromStream( is ); 659 } 660 finally 661 { 662 is.close(); 663 } 664 665 getLogger().debug( "Loading Instrument Manager state took " + 666 ( System.currentTimeMillis() - now ) + "ms." ); 667 } 668 669 676 public void loadStateFromStream( InputStream is ) 677 throws Exception 678 { 679 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 681 Configuration stateConfig = builder.build( is ); 682 683 loadStateFromConfiguration( stateConfig ); 684 } 685 686 694 public void loadStateFromConfiguration( Configuration state ) 695 throws ConfigurationException 696 { 697 Configuration[] instrumentableConfs = state.getChildren( "instrumentable" ); 698 for( int i = 0; i < instrumentableConfs.length; i++ ) 699 { 700 Configuration instrumentableConf = instrumentableConfs[ i ]; 701 String instrumentableName = instrumentableConf.getAttribute( "name" ); 702 InstrumentableProxy instrumentableProxy = getInstrumentableProxy( instrumentableName ); 703 if( instrumentableProxy == null ) 704 { 705 instrumentableProxy = new InstrumentableProxy( 709 this, null, instrumentableName, instrumentableName ); 710 instrumentableProxy.enableLogging( getLogger() ); 711 m_instrumentableProxies.put( instrumentableName, instrumentableProxy ); 712 713 m_instrumentableProxyArray = null; 715 m_instrumentableDescriptorArray = null; 716 } 717 718 instrumentableProxy.loadState( instrumentableConf ); 719 } 720 721 stateChanged(); 722 } 723 724 733 public void saveStateToFile( File stateFile ) 734 throws Exception 735 { 736 long now = System.currentTimeMillis(); 737 getLogger().debug( "Saving Instrument Manager state to: " + stateFile.getAbsolutePath() ); 738 739 ByteArrayOutputStream os = new ByteArrayOutputStream (); 744 byte[] data; 745 try 746 { 747 saveStateToStream( os ); 748 data = os.toByteArray(); 749 } 750 finally 751 { 752 os.close(); 753 } 754 755 File renameFile = null; 758 boolean success = false; 759 if( stateFile.exists() ) 760 { 761 renameFile = new File ( stateFile.getAbsolutePath() + "." + now + ".backup" ); 762 stateFile.renameTo( renameFile ); 763 } 764 765 FileOutputStream fos = new FileOutputStream ( stateFile ); 767 try 768 { 769 fos.write( data ); 770 success = true; 771 } 772 finally 773 { 774 fos.close(); 775 776 if ( !success ) 777 { 778 stateFile.delete(); 780 } 781 782 if ( renameFile != null ) 784 { 785 if ( success ) 786 { 787 renameFile.delete(); 789 } 790 else 791 { 792 renameFile.renameTo( stateFile ); 794 } 795 } 796 } 797 798 getLogger().debug( "Saving Instrument Manager state took " + 799 ( System.currentTimeMillis() - now ) + "ms." ); 800 } 801 802 809 public void saveStateToStream( OutputStream os ) 810 throws Exception 811 { 812 Configuration stateConfig = saveStateToConfiguration(); 813 814 DefaultConfigurationSerializer serializer = new DefaultConfigurationSerializer(); 816 serializer.setIndent( true ); 817 serializer.serialize( os, stateConfig ); 818 } 819 820 825 public Configuration saveStateToConfiguration() 826 { 827 DefaultConfiguration state = new DefaultConfiguration( "instrument-manager-state", "-" ); 828 829 InstrumentableProxy[] instrumentableProxies = m_instrumentableProxyArray; 830 if( instrumentableProxies == null ) 831 { 832 instrumentableProxies = updateInstrumentableProxyArray(); 833 } 834 835 for( int i = 0; i < instrumentableProxies.length; i++ ) 836 { 837 Configuration childState = instrumentableProxies[ i ].saveState(); 838 if ( childState != null ) 839 { 840 state.addChild( childState ); 841 } 842 } 843 844 return state; 845 } 846 847 850 863 public void setInstrumentableName( String name ) 864 { 865 m_instrumentableName = name; 866 } 867 868 873 public String getInstrumentableName() 874 { 875 return m_instrumentableName; 876 } 877 878 889 public Instrument[] getInstruments() 890 { 891 return new Instrument[] 892 { 893 m_totalMemoryInstrument, 894 m_freeMemoryInstrument, 895 m_memoryInstrument, 896 m_activeThreadCountInstrument 897 }; 898 } 899 900 909 public Instrumentable[] getChildInstrumentables() 910 { 911 return Instrumentable.EMPTY_INSTRUMENTABLE_ARRAY; 912 } 913 914 917 public void run() 918 { 919 while( m_runner != null ) 920 { 921 try 922 { 923 Thread.sleep( 1000 ); 924 925 memoryInstruments(); 926 threadInstruments(); 927 testInstrumentSampleLeases(); 928 929 long now = System.currentTimeMillis(); 931 if( now - m_lastStateSave >= m_stateInterval ) 932 { 933 saveState(); 934 } 935 } 936 catch( Throwable t ) 937 { 938 getLogger().error( "Encountered an unexpected error.", t ); 939 } 940 } 941 } 942 943 946 949 private void saveState() 950 { 951 long now = System.currentTimeMillis(); 952 953 m_lastStateSave = now; 955 956 if( m_stateFile == null ) 957 { 958 return; 959 } 960 961 try 962 { 963 saveStateToFile( m_stateFile ); 964 } 965 catch ( Exception e ) 966 { 967 getLogger().warn( "Unable to save the Instrument Manager state", e ); 968 } 969 } 970 971 979 private InstrumentableProxy getInstrumentableProxy( String instrumentableName ) 980 { 981 String name = instrumentableName; 982 while( true ) 983 { 984 InstrumentableProxy proxy = (InstrumentableProxy)m_instrumentableProxies.get( name ); 985 if( proxy != null ) 986 { 987 return proxy; 988 } 989 990 int pos = name.lastIndexOf( '.' ); 992 if( pos > 0 ) 993 { 994 name = name.substring( 0, pos ); 995 } 996 else 997 { 998 return null; 999 } 1000 } 1001 } 1002 1003 1013 private InstrumentableProxy locateDeepestInstrumentableProxy( String instrumentableName ) 1014 { 1015 InstrumentableProxy deepestProxy = null; 1016 InstrumentableProxy proxy = getInstrumentableProxy( instrumentableName ); 1018 1019 while ( proxy != null ) 1021 { 1022 deepestProxy = proxy; 1023 1024 proxy = deepestProxy.getChildInstrumentableProxy( instrumentableName ); 1025 } 1026 1027 return deepestProxy; 1028 } 1029 1030 1033 private void memoryInstruments() 1034 { 1035 Runtime runtime = null; 1037 long totalMemory = -1; 1038 long freeMemory = -1; 1039 1040 if( m_totalMemoryInstrument.isActive() ) 1042 { 1043 runtime = Runtime.getRuntime(); 1044 totalMemory = runtime.totalMemory(); 1045 m_totalMemoryInstrument.setValue( (int)totalMemory ); 1046 } 1047 1048 if( m_freeMemoryInstrument.isActive() ) 1050 { 1051 if( runtime == null ) 1052 { 1053 runtime = Runtime.getRuntime(); 1054 } 1055 freeMemory = runtime.freeMemory(); 1056 m_freeMemoryInstrument.setValue( (int)freeMemory ); 1057 } 1058 1059 if( m_memoryInstrument.isActive() ) 1061 { 1062 if( runtime == null ) 1063 { 1064 runtime = Runtime.getRuntime(); 1065 } 1066 if( totalMemory < 0 ) 1067 { 1068 totalMemory = runtime.totalMemory(); 1069 } 1070 if( freeMemory < 0 ) 1071 { 1072 freeMemory = runtime.freeMemory(); 1073 } 1074 m_memoryInstrument.setValue( (int)( totalMemory - freeMemory ) ); 1075 } 1076 } 1077 1078 1081 private void threadInstruments() 1082 { 1083 if( m_activeThreadCountInstrument.isActive() ) 1084 { 1085 ThreadGroup threadGroup = Thread.currentThread().getThreadGroup(); 1087 ThreadGroup parent; 1088 while( ( parent = threadGroup.getParent() ) != null ) 1089 { 1090 threadGroup = parent; 1091 } 1092 1093 m_activeThreadCountInstrument.setValue( threadGroup.activeCount() ); 1094 } 1095 } 1096 1097 1102 private void testInstrumentSampleLeases() 1103 { 1104 long now = System.currentTimeMillis(); 1105 1106 InstrumentSample[] samples; 1107 synchronized( m_leasedInstrumentSamples ) 1108 { 1109 samples = m_leasedInstrumentSampleArray; 1110 if ( samples == null ) 1111 { 1112 m_leasedInstrumentSampleArray = 1113 new InstrumentSample[ m_leasedInstrumentSamples.size() ]; 1114 m_leasedInstrumentSamples.toArray( m_leasedInstrumentSampleArray ); 1115 samples = m_leasedInstrumentSampleArray; 1116 } 1117 } 1118 1119 for ( int i = 0; i < samples.length; i++ ) 1120 { 1121 InstrumentSample sample = samples[i]; 1122 long expire = sample.getLeaseExpirationTime(); 1123 if ( now >= expire ) 1124 { 1125 InstrumentProxy instrument = sample.getInstrumentProxy(); 1127 instrument.removeInstrumentSample( sample ); 1128 sample.expire(); 1129 1130 m_leasedInstrumentSamples.remove( sample ); 1131 m_leasedInstrumentSampleArray = null; 1132 } 1133 } 1134 } 1135 1136 1142 void registerLeasedInstrumentSample( InstrumentSample instrumentSample ) 1143 { 1144 synchronized( m_leasedInstrumentSamples ) 1145 { 1146 if ( instrumentSample.getLeaseExpirationTime() <= 0 ) 1148 { 1149 throw new IllegalStateException ( "Got an InstrumentSample that was not leased." ); 1150 } 1151 1152 if ( m_leasedInstrumentSamples.indexOf( instrumentSample ) < 0 ) 1154 { 1155 m_leasedInstrumentSamples.add( instrumentSample ); 1156 m_leasedInstrumentSampleArray = null; 1157 } 1158 } 1159 } 1160 1161 1167 private InstrumentableProxy[] updateInstrumentableProxyArray() 1168 { 1169 synchronized( m_semaphore ) 1170 { 1171 m_instrumentableProxyArray = new InstrumentableProxy[ m_instrumentableProxies.size() ]; 1172 m_instrumentableProxies.values().toArray( m_instrumentableProxyArray ); 1173 1174 Arrays.sort( m_instrumentableProxyArray, new Comparator () 1178 { 1179 public int compare( Object o1, Object o2 ) 1180 { 1181 return ((InstrumentableProxy)o1).getDescription(). 1182 compareTo( ((InstrumentableProxy)o2).getDescription() ); 1183 } 1184 1185 public boolean equals( Object obj ) 1186 { 1187 return false; 1188 } 1189 } ); 1190 1191 return m_instrumentableProxyArray; 1192 } 1193 } 1194 1195 1201 private InstrumentableDescriptorLocal[] updateInstrumentableDescriptorArray() 1202 { 1203 synchronized( m_semaphore ) 1204 { 1205 if( m_instrumentableProxyArray == null ) 1206 { 1207 updateInstrumentableProxyArray(); 1208 } 1209 1210 m_instrumentableDescriptorArray = 1211 new InstrumentableDescriptorLocal[ m_instrumentableProxyArray.length ]; 1212 for( int i = 0; i < m_instrumentableProxyArray.length; i++ ) 1213 { 1214 m_instrumentableDescriptorArray[ i ] = m_instrumentableProxyArray[ i ].getDescriptor(); 1215 } 1216 1217 return m_instrumentableDescriptorArray; 1218 } 1219 } 1220 1221 1235 private void registerDummyInstrumentableInner( Instrumentable instrumentable, 1236 InstrumentableProxy instrumentableProxy, 1237 String instrumentableName, 1238 String childName ) 1239 throws Exception 1240 { 1241 int pos = childName.indexOf( '.' ); 1245 if ( pos >= 0 ) 1246 { 1247 String newParentName = childName.substring( 0, pos ); 1248 String newChildName = 1249 childName.substring( pos + 1 ); 1250 1251 String fullChildName = instrumentableName + "." + newParentName; 1252 1253 getLogger().debug( "Registering Child Instrumentable: " + fullChildName ); 1254 1255 InstrumentableProxy proxy = 1257 instrumentableProxy.getChildInstrumentableProxy( fullChildName ); 1258 if( proxy == null ) 1259 { 1260 proxy = new InstrumentableProxy( 1261 this, instrumentableProxy, fullChildName, newParentName ); 1262 proxy.enableLogging( getLogger() ); 1263 1264 instrumentableProxy.addChildInstrumentableProxy( proxy ); 1265 } 1266 1267 registerDummyInstrumentableInner( instrumentable, proxy, fullChildName, newChildName ); 1269 } 1270 else 1271 { 1272 String fullChildName = instrumentableName + "." + childName; 1274 1275 getLogger().debug( "Registering Child Instrumentable: " + fullChildName ); 1276 1277 InstrumentableProxy proxy = 1279 instrumentableProxy.getChildInstrumentableProxy( fullChildName ); 1280 if( proxy == null ) 1281 { 1282 proxy = new InstrumentableProxy( 1283 this, instrumentableProxy, fullChildName, childName ); 1284 proxy.enableLogging( getLogger() ); 1285 1286 instrumentableProxy.addChildInstrumentableProxy( proxy ); 1287 } 1288 1289 registerInstrumentableInner( instrumentable, proxy, fullChildName ); 1291 } 1292 } 1293 1294 1300 private void registerInstrumentableInner( Instrumentable instrumentable, 1301 InstrumentableProxy instrumentableProxy, 1302 String instrumentableName ) 1303 throws Exception 1304 { 1305 instrumentableProxy.setRegistered(); 1307 1308 Instrument[] instruments = instrumentable.getInstruments(); 1310 for( int i = 0; i < instruments.length; i++ ) 1311 { 1312 Instrument instrument = instruments[ i ]; 1313 String instrumentName = instrument.getInstrumentName(); 1314 String fullInstrumentName = instrumentableName + "." + instrumentName; 1315 1316 getLogger().debug( "Registering Instrument: " + fullInstrumentName ); 1317 1318 InstrumentProxy proxy = instrumentableProxy.getInstrumentProxy( fullInstrumentName ); 1320 if( proxy == null ) 1321 { 1322 proxy = new InstrumentProxy( 1323 instrumentableProxy, fullInstrumentName, instrumentName ); 1324 proxy.enableLogging( getLogger() ); 1325 1326 if( instrument instanceof CounterInstrument ) 1329 { 1330 proxy.setType( InstrumentManagerClient.INSTRUMENT_TYPE_COUNTER ); 1331 } 1332 else if( instrument instanceof ValueInstrument ) 1333 { 1334 proxy.setType( InstrumentManagerClient.INSTRUMENT_TYPE_VALUE ); 1335 } 1336 else 1337 { 1338 throw new ServiceException( fullInstrumentName, "Encountered an unknown " 1339 + "Instrument type for the Instrument with key, " 1340 + fullInstrumentName + ": " + instrument.getClass().getName() ); 1341 } 1342 1343 proxy.setRegistered(); 1345 1346 ( (AbstractInstrument)instrument ).setInstrumentProxy( proxy ); 1348 1349 instrumentableProxy.addInstrumentProxy( proxy ); 1350 } 1351 else 1352 { 1353 if( instrument instanceof CounterInstrument ) 1356 { 1357 switch( proxy.getType() ) 1358 { 1359 case InstrumentManagerClient.INSTRUMENT_TYPE_COUNTER: 1360 ( (AbstractInstrument)instrument ).setInstrumentProxy( proxy ); 1363 break; 1364 1365 case InstrumentManagerClient.INSTRUMENT_TYPE_NONE: 1366 proxy.setType( InstrumentManagerClient.INSTRUMENT_TYPE_COUNTER ); 1368 1369 ( (AbstractInstrument)instrument ).setInstrumentProxy( proxy ); 1371 break; 1372 1373 default: 1374 throw new ServiceException( instrumentName, 1375 "Instruments of more than one type are assigned to name: " 1376 + instrumentName ); 1377 } 1378 } 1379 else if( instrument instanceof ValueInstrument ) 1380 { 1381 switch( proxy.getType() ) 1382 { 1383 case InstrumentManagerClient.INSTRUMENT_TYPE_VALUE: 1384 ( (AbstractInstrument)instrument ).setInstrumentProxy( proxy ); 1387 break; 1388 1389 case InstrumentManagerClient.INSTRUMENT_TYPE_NONE: 1390 proxy.setType( InstrumentManagerClient.INSTRUMENT_TYPE_VALUE ); 1392 1393 ( (AbstractInstrument)instrument ).setInstrumentProxy( proxy ); 1395 break; 1396 1397 default: 1398 throw new ServiceException( instrumentName, 1399 "Instruments of more than one type are assigned to name: " 1400 + instrumentName ); 1401 } 1402 } 1403 else 1404 { 1405 throw new ServiceException( instrumentName, "Encountered an unknown Instrument " 1406 + "type for the Instrument with name, " + instrumentName + ": " 1407 + instrument.getClass().getName() ); 1408 } 1409 1410 proxy.setRegistered(); 1412 } 1413 } 1414 1415 Instrumentable[] children = instrumentable.getChildInstrumentables(); 1417 for ( int i = 0; i < children.length; i++ ) 1418 { 1419 Instrumentable child = children[i]; 1420 1421 String childName = child.getInstrumentableName(); 1423 if( childName == null ) 1424 { 1425 String msg = "The getInstrumentableName() method of a child Instrumentable of " + 1426 instrumentableName + " returned null. Child class: " + 1427 child.getClass().getName(); 1428 getLogger().debug( msg ); 1429 throw new ServiceException( instrumentable.getClass().getName(), msg ); 1430 } 1431 1432 String fullChildName = instrumentableName + "." + childName; 1433 1434 getLogger().debug( "Registering Child Instrumentable: " + fullChildName ); 1435 1436 InstrumentableProxy proxy = 1438 instrumentableProxy.getChildInstrumentableProxy( fullChildName ); 1439 if( proxy == null ) 1440 { 1441 proxy = new InstrumentableProxy( 1442 this, instrumentableProxy, fullChildName, childName ); 1443 proxy.enableLogging( getLogger() ); 1444 1445 instrumentableProxy.addChildInstrumentableProxy( proxy ); 1446 } 1447 1448 registerInstrumentableInner( child, proxy, fullChildName ); 1450 } 1451 } 1452 1453 1456 protected void stateChanged() 1457 { 1458 m_stateVersion++; 1459 } 1460} 1461 1462 | Popular Tags |