1 19 20 package org.apache.excalibur.instrument.client.http; 21 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 import java.io.UnsupportedEncodingException ; 25 import java.net.HttpURLConnection ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.net.URLEncoder ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 32 import org.apache.avalon.framework.configuration.Configuration; 33 import org.apache.avalon.framework.configuration.ConfigurationException; 34 import org.apache.avalon.framework.configuration.DefaultConfiguration; 35 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 36 import org.apache.avalon.framework.logger.AbstractLogEnabled; 37 import org.apache.avalon.framework.logger.Logger; 38 39 import org.apache.excalibur.instrument.client.InstrumentableData; 40 import org.apache.excalibur.instrument.client.InstrumentManagerConnection; 41 import org.apache.excalibur.instrument.client.InstrumentManagerConnectionListener; 42 import org.apache.excalibur.instrument.client.InstrumentManagerData; 43 import org.apache.excalibur.instrument.client.InstrumentSampleFrame; 44 45 53 public class HTTPInstrumentManagerConnection 54 extends InstrumentManagerConnection 55 { 56 private URL m_url; 57 58 60 private boolean m_connected; 61 62 65 private boolean m_disabled; 66 67 private HTTPInstrumentManagerData m_manager; 68 69 private List m_leasedSamples = new ArrayList (); 70 private HTTPInstrumentSampleData[] m_leasedSampleAry; 71 72 75 78 public HTTPInstrumentManagerConnection( URL url ) 79 { 80 m_url = url; 81 m_connected = false; 82 83 m_manager = new HTTPInstrumentManagerData( this ); 84 } 85 86 89 public void enableLogging( Logger logger ) 90 { 91 super.enableLogging( logger ); 92 m_manager.enableLogging( logger.getChildLogger( "manager" ) ); 93 } 94 95 100 public Object getKey() 101 { 102 return m_url; 103 } 104 105 110 public boolean isConnected() 111 { 112 return m_connected; 113 } 114 115 120 public InstrumentManagerData getInstrumentManager() 121 { 122 return m_manager; 123 } 124 125 130 public String getTabTitle() 131 { 132 if ( m_disabled ) 133 { 134 return "[DISABLED] " + super.getTabTitle(); 135 } 136 else 137 { 138 return super.getTabTitle(); 139 } 140 } 141 142 145 protected void invokeGC() 146 { 147 getState( "gc.xml" ); 148 } 149 150 155 public Configuration saveState() 156 { 157 synchronized( this ) 158 { 159 DefaultConfiguration state = (DefaultConfiguration)super.saveState(); 160 161 state.setAttribute( "url", m_url.toExternalForm() ); 162 163 return state; 164 } 165 } 166 167 175 public void loadState( Configuration state ) 176 throws ConfigurationException 177 { 178 synchronized( this ) 179 { 180 super.loadState( state ); 181 182 } 184 } 185 186 193 String urlEncode( String val ) 194 { 195 try 196 { 197 return URLEncoder.encode( val, "UTF8" ); 198 } 199 catch ( UnsupportedEncodingException e ) 200 { 201 getLogger().error( "Bad encoding.", e ); 203 return val; 204 } 205 } 206 207 216 public void updateSampleFrames() 217 { 218 InstrumentSampleFrame[] frames = getSampleFrameArray(); 219 if ( frames.length == 0 ) 220 { 221 return; 223 } 224 225 String [] names = new String [frames.length]; 227 long[] lastTimes = new long[frames.length]; 228 HTTPInstrumentSampleSnapshotData[] snapshots = 229 new HTTPInstrumentSampleSnapshotData[frames.length]; 230 for ( int i = 0; i < frames.length; i++ ) 231 { 232 InstrumentSampleFrame frame = frames[i]; 233 names[i] = frame.getInstrumentSampleName(); 234 lastTimes[i] = frame.getLastSnapshotTime(); 235 } 236 237 if ( isConnected() ) 239 { 240 StringBuffer sb = new StringBuffer (); 241 sb.append( "snapshots.xml?packed=true&compact=true" ); 242 for ( int i = 0; i < frames.length; i++ ) 243 { 244 sb.append( "&name=" ); 245 sb.append( this.urlEncode( names[i] ) ); 246 sb.append( "&base-time=" ); 247 sb.append( lastTimes[i] ); 248 } 249 Configuration configuration = getState( sb.toString() ); 250 if ( configuration != null ) 251 { 252 Configuration[] snapshotConfs = configuration.getChildren( "sample" ); 253 for ( int i = 0; i < snapshotConfs.length; i++ ) 254 { 255 Configuration snapshotConf = snapshotConfs[i]; 256 String name = snapshotConf.getAttribute( "name", null ); 257 if ( name != null ) 258 { 259 boolean expired = snapshotConf.getAttributeAsBoolean( "expired", false ); 260 if ( !expired ) 261 { 262 for ( int j = 0; j < frames.length; j++ ) 264 { 265 if ( name.equals( names[j] ) ) 266 { 267 snapshots[j] = 268 new HTTPInstrumentSampleSnapshotData( this, name ); 269 snapshots[j].enableLogging( getLogger() ); 270 try 271 { 272 snapshots[j].update( snapshotConf ); 273 } 274 catch ( ConfigurationException e ) 275 { 276 getLogger().info( "Snapshot update failed.", e ); 278 getLogger().info( " URL: " + sb.toString() ); 279 getLogger().info( " i:" + i + " j:" + j ); 280 snapshots[j] = null; 281 } 282 break; 283 } 284 } 285 } 286 } 287 } 288 } 289 } 290 291 for ( int i = 0; i < frames.length; i++ ) 294 { 295 InstrumentSampleFrame frame = frames[i]; 296 frame.updateSnapshot( snapshots[i] ); 297 } 298 } 299 300 303 308 URL getURL() 309 { 310 return m_url; 311 } 312 313 322 Configuration getState( String path ) 323 { 324 if ( m_disabled ) 325 { 326 return null; 327 } 328 329 URL url; 330 try 331 { 332 url = new URL ( m_url, path ); 333 } 334 catch ( MalformedURLException e ) 335 { 336 getLogger().debug( "Request failed.", e ); 337 return null; 338 } 339 340 try 341 { 342 HttpURLConnection conn = (HttpURLConnection )url.openConnection(); 343 344 if ( conn.getResponseCode() == conn.HTTP_OK ) 345 { 346 boolean oldConnected = m_connected; 347 m_connected = true; 348 if ( !oldConnected ) 349 { 350 InstrumentManagerConnectionListener[] listenerArray = getListenerArray(); 352 for ( int i = 0; i < listenerArray.length; i++ ) 353 { 354 listenerArray[i].opened( this ); 355 } 356 } 357 358 InputStream is = conn.getInputStream(); 359 try 360 { 361 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 362 try 363 { 364 return builder.build( is ); 365 } 366 catch ( ConfigurationException e ) 367 { 368 getLogger().warn( "Invalid XML reveived from the server.", e ); 369 return null; 370 } 371 catch ( org.xml.sax.SAXException e ) 372 { 373 getLogger().warn( "Invalid XML reveived from the server.", e ); 374 return null; 375 } 376 } 377 finally 378 { 379 is.close(); 380 } 381 } 382 else 383 { 384 if ( ( conn.getResponseCode() == 404 ) 385 && path.startsWith( "instrument-manager.xml" ) ) 386 { 387 getLogger().warn( "Requested " + url + " resulted in error code 404. " 388 + "Most likely not an Instrument Manager, disabling future requests." ); 389 m_disabled = true; 390 } 391 else 392 { 393 getLogger().debug( "Response: " + conn.getResponseCode() + " : " 394 + conn.getResponseMessage() ); 395 } 396 return null; 397 } 398 } 399 catch ( IOException e ) 400 { 401 String msg = e.getMessage(); 402 if ( msg == null ) 403 { 404 msg = e.toString(); 405 } 406 407 if ( msg.indexOf( "Connect" ) >= 0 ) 408 { 409 getLogger().debug( "Request failed. URL: " + url + " Error: " + msg ); 411 } 412 else 413 { 414 getLogger().debug( "Request failed. URL: " + url + " Error: ", e ); 415 } 416 417 418 boolean oldConnected = m_connected; 419 m_connected = false; 420 if ( oldConnected ) 421 { 422 InstrumentManagerConnectionListener[] listenerArray = getListenerArray(); 424 for ( int i = 0; i < listenerArray.length; i++ ) 425 { 426 listenerArray[i].closed( this ); 427 } 428 } 429 430 return null; 431 } 432 } 433 } | Popular Tags |