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.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import org.apache.avalon.framework.configuration.Configuration; 29 import org.apache.avalon.framework.configuration.ConfigurationException; 30 31 import org.apache.excalibur.instrument.client.InstrumentData; 32 import org.apache.excalibur.instrument.client.InstrumentSampleData; 33 34 class HTTPInstrumentData 35 extends AbstractHTTPElementData 36 implements InstrumentData 37 { 38 39 private boolean m_registered; 40 41 42 private int m_type; 43 44 private List m_samples = new ArrayList (); 45 private HTTPInstrumentSampleData[] m_sampleAry; 46 private Map m_sampleMap = new HashMap (); 47 48 51 54 HTTPInstrumentData( HTTPInstrumentableData parent, 55 String name ) 56 { 57 super( (HTTPInstrumentManagerConnection)parent.getConnection(), parent, name ); 58 59 m_registered = false; 60 } 61 62 65 74 protected void update( Configuration configuration, boolean recurse ) 75 throws ConfigurationException 76 { 77 super.update( configuration ); 78 79 if ( getLogger().isDebugEnabled() ) 80 { 81 getLogger().debug( 82 "Updated Instrument '" + getName() + "' to version " + getStateVersion() ); 83 } 84 85 m_registered = configuration.getAttributeAsBoolean( "registered" ); 86 m_type = configuration.getAttributeAsInteger( "type" ); 87 88 Map oldSampleMap; 92 synchronized( m_samples ) 93 { 94 oldSampleMap = new HashMap ( m_sampleMap ); 95 } 96 97 Configuration[] sampleConfs = configuration.getChildren( "sample" ); 98 for ( int i = 0; i < sampleConfs.length; i++ ) 99 { 100 Configuration sConf = sampleConfs[i]; 101 String sName = sConf.getAttribute( "name" ); 102 int sStateVersion = sConf.getAttributeAsInteger( "state-version" ); 103 104 HTTPInstrumentSampleData sData; 105 synchronized( m_samples ) 106 { 107 sData = (HTTPInstrumentSampleData)m_sampleMap.get( sName ); 108 if ( sData == null ) 109 { 110 sData = new HTTPInstrumentSampleData( this, sName ); 112 sData.enableLogging( getLogger().getChildLogger( sName ) ); 113 m_samples.add( sData ); 114 m_sampleMap.put( sName, sData ); 115 m_sampleAry = null; 116 } 117 oldSampleMap.remove( sName ); 118 } 119 120 if ( recurse ) 121 { 122 sData.update( sConf ); 123 } 124 else 125 { 126 if ( sStateVersion != sData.getStateVersion() ) 127 { 128 sData.update(); 130 } 131 } 132 } 133 134 if ( !oldSampleMap.isEmpty() ) 136 { 137 synchronized( m_samples ) 138 { 139 for ( Iterator iter = oldSampleMap.values().iterator(); iter.hasNext(); ) 140 { 141 HTTPInstrumentSampleData sample = (HTTPInstrumentSampleData)iter.next(); 142 m_samples.remove( sample ); 143 m_sampleMap.remove( sample.getName() ); 144 m_sampleAry = null; 145 } 146 } 147 } 148 } 149 150 156 public boolean update() 157 { 158 HTTPInstrumentManagerConnection connection = 159 (HTTPInstrumentManagerConnection)getConnection(); 160 161 Configuration configuration = connection.getState( 162 "instrument.xml?packed=true&name=" + urlEncode( getName() ) ); 163 if ( configuration != null ) 164 { 165 try 166 { 167 update( configuration, false ); 168 169 return true; 170 } 171 catch ( ConfigurationException e ) 172 { 173 getLogger().debug( "Unable to update.", e ); 174 } 175 } 176 177 return false; 178 } 179 180 183 188 public boolean isRegistered() 189 { 190 return m_registered; 191 } 192 193 201 public int getType() 202 { 203 return m_type; 204 } 205 206 211 public InstrumentSampleData[] getInstrumentSamples() 212 { 213 HTTPInstrumentSampleData[] samples = m_sampleAry; 214 if ( samples == null ) 215 { 216 synchronized ( m_samples ) 217 { 218 m_sampleAry = new HTTPInstrumentSampleData[m_samples.size()]; 219 m_samples.toArray( m_sampleAry ); 220 samples = m_sampleAry; 221 } 222 } 223 return samples; 224 } 225 226 237 public boolean createInstrumentSample( String description, 238 long interval, 239 int sampleCount, 240 long leaseTime, 241 int sampleType ) 242 { 243 HTTPInstrumentManagerConnection connection = 244 (HTTPInstrumentManagerConnection)getConnection(); 245 246 Configuration configuration = connection.getState( 247 "create-sample.xml?name=" + urlEncode( getName() ) 248 + "&description=" + urlEncode( description ) + "&interval=" + interval 249 + "&size=" + sampleCount + "&lease=" + leaseTime + "&type=" + sampleType ); 250 251 return configuration != null; 254 } 255 256 259 } | Popular Tags |