1 19 20 package org.apache.excalibur.instrument.client.http; 21 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import org.apache.avalon.framework.configuration.Configuration; 28 import org.apache.avalon.framework.configuration.ConfigurationException; 29 30 import org.apache.excalibur.instrument.client.InstrumentableData; 31 import org.apache.excalibur.instrument.client.InstrumentManagerData; 32 33 class HTTPInstrumentManagerData 34 extends AbstractHTTPData 35 implements InstrumentManagerData 36 { 37 38 private String m_name; 39 40 41 private boolean m_batchedUpdates; 42 43 44 private boolean m_readOnly; 45 46 private List m_instrumentables = new ArrayList (); 47 private HTTPInstrumentableData[] m_instrumentableAry; 48 private Map m_instrumentableMap = new HashMap (); 49 50 53 56 HTTPInstrumentManagerData( HTTPInstrumentManagerConnection connection ) 57 { 58 super( connection, connection.getURL().toExternalForm() ); 59 60 m_name = connection.getURL().toExternalForm(); 61 } 62 63 66 75 protected void update( Configuration configuration, boolean recurse ) 76 throws ConfigurationException 77 { 78 super.update( configuration ); 79 80 m_name = configuration.getAttribute( "name" ); 81 82 m_batchedUpdates = configuration.getAttributeAsBoolean( "batched-updates", false ); 84 85 m_readOnly = configuration.getAttributeAsBoolean( "read-only", false ); 87 88 if ( getLogger().isDebugEnabled() ) 89 { 90 getLogger().debug( 91 "Updated InstrumentManager '" + getName() + "' to version " + getStateVersion() ); 92 } 93 94 Configuration[] instrumentableConfs = configuration.getChildren( "instrumentable" ); 95 for ( int i = 0; i < instrumentableConfs.length; i++ ) 96 { 97 Configuration iaConf = instrumentableConfs[i]; 98 String iaName = iaConf.getAttribute( "name" ); 99 int iaStateVersion = iaConf.getAttributeAsInteger( "state-version" ); 100 101 HTTPInstrumentableData iaData; 102 synchronized ( m_instrumentables ) 103 { 104 iaData = (HTTPInstrumentableData)m_instrumentableMap.get( iaName ); 105 if ( iaData == null ) 106 { 107 iaData = new HTTPInstrumentableData( this, iaName ); 109 iaData.enableLogging( getLogger().getChildLogger( iaName ) ); 110 m_instrumentables.add( iaData ); 111 m_instrumentableAry = null; 112 m_instrumentableMap.put( iaName, iaData ); 113 } 114 } 115 116 if ( recurse ) 117 { 118 iaData.update( iaConf, recurse ); 119 } 120 else 121 { 122 if ( iaStateVersion != iaData.getStateVersion() ) 123 { 124 iaData.update(); 126 } 127 } 128 } 129 } 130 131 137 public boolean update() 138 { 139 HTTPInstrumentManagerConnection connection = 140 (HTTPInstrumentManagerConnection)getConnection(); 141 142 Configuration configuration = connection.getState( "instrument-manager.xml?packed=true" ); 143 if ( configuration != null ) 144 { 145 try 146 { 147 update( configuration, false ); 148 149 151 return true; 152 } 153 catch ( ConfigurationException e ) 154 { 155 getLogger().debug( "Unable to update.", e ); 156 } 157 } 158 return false; 159 } 160 161 164 169 public String getName() 170 { 171 return m_name; 172 } 173 174 182 public boolean isReadOnly() 183 { 184 return m_readOnly; 185 } 186 187 195 private boolean isSupportsBatchedUpdates() 196 { 197 return m_batchedUpdates; 198 } 199 200 205 public InstrumentableData[] getInstrumentables() 206 { 207 HTTPInstrumentableData[] instrumentables = m_instrumentableAry; 208 if ( instrumentables == null ) 209 { 210 synchronized ( m_instrumentables ) 211 { 212 m_instrumentableAry = new HTTPInstrumentableData[m_instrumentables.size()]; 213 m_instrumentables.toArray( m_instrumentableAry ); 214 instrumentables = m_instrumentableAry; 215 } 216 } 217 return instrumentables; 218 } 219 220 226 public boolean updateAll() 227 { 228 HTTPInstrumentManagerConnection connection = 229 (HTTPInstrumentManagerConnection)getConnection(); 230 231 Configuration configuration = 232 connection.getState( "instrument-manager.xml?packed=true&recurse=true" ); 233 if ( configuration != null ) 234 { 235 try 236 { 237 update( configuration, true ); 238 239 241 return true; 242 } 243 catch ( ConfigurationException e ) 244 { 245 getLogger().debug( "Unable to update.", e ); 246 } 247 } 248 return false; 249 } 250 251 263 public void createInstrumentSample( String instrumentName, 264 String description, 265 long interval, 266 int sampleCount, 267 long leaseTime, 268 int sampleType ) 269 { 270 HTTPInstrumentManagerConnection connection = 271 (HTTPInstrumentManagerConnection)getConnection(); 272 273 connection.getState( "create-sample.xml?name=" + urlEncode( instrumentName ) 274 + "&description=" + urlEncode( description ) + "&interval=" + interval 275 + "&size=" + sampleCount + "&lease=" + leaseTime + "&type=" + sampleType ); 276 } 277 278 291 public void createInstrumentSamples( String [] instrumentNames, 292 String [] descriptions, 293 long[] intervals, 294 int[] sampleCounts, 295 long[] leaseTimes, 296 int[] sampleTypes ) 297 { 298 HTTPInstrumentManagerConnection connection = 299 (HTTPInstrumentManagerConnection)getConnection(); 300 301 if ( ( instrumentNames.length != descriptions.length ) 303 || ( instrumentNames.length != intervals.length ) 304 || ( instrumentNames.length != sampleCounts.length ) 305 || ( instrumentNames.length != leaseTimes.length ) 306 || ( instrumentNames.length != sampleTypes.length ) ) 307 { 308 throw new IllegalArgumentException ( "Array lengths of all parameters must be equal." ); 309 } 310 311 if ( isSupportsBatchedUpdates() ) 313 { 314 StringBuffer sb = new StringBuffer (); 315 sb.append( "create-samples.xml?" ); 316 for ( int i = 0; i < instrumentNames.length; i++ ) 317 { 318 if ( i > 0 ) 319 { 320 sb.append( "&" ); 321 } 322 sb.append( "name=" ); 323 sb.append( urlEncode( instrumentNames[i] ) ); 324 sb.append( "&description=" ); 325 sb.append( urlEncode( descriptions[i] ) ); 326 sb.append( "&interval=" ); 327 sb.append( intervals[i] ); 328 sb.append( "&size=" ); 329 sb.append( sampleCounts[i] ); 330 sb.append( "&lease=" ); 331 sb.append( leaseTimes[i] ); 332 sb.append( "&type=" ); 333 sb.append( sampleTypes[i] ); 334 } 335 336 connection.getState( sb.toString() ); 337 } 338 else 339 { 340 for ( int i = 0; i < instrumentNames.length; i++ ) 341 { 342 createInstrumentSample( instrumentNames[i], descriptions[i], intervals[i], 343 sampleCounts[i], leaseTimes[i], sampleTypes[i] ); 344 } 345 } 346 } 347 348 351 } | Popular Tags |