1 19 20 package org.apache.excalibur.instrument.manager.http; 21 22 import java.net.InetAddress ; 23 import java.net.UnknownHostException ; 24 25 import org.apache.avalon.framework.activity.Startable; 26 import org.apache.avalon.framework.configuration.Configurable; 27 import org.apache.avalon.framework.configuration.Configuration; 28 import org.apache.avalon.framework.configuration.ConfigurationException; 29 30 import org.apache.excalibur.instrument.AbstractLogEnabledInstrumentable; 31 32 import org.apache.excalibur.instrument.manager.DefaultInstrumentManager; 33 import org.apache.excalibur.instrument.manager.DefaultInstrumentManagerConnector; 34 import org.apache.excalibur.instrument.manager.http.server.AbstractHTTPURLHandler; 35 import org.apache.excalibur.instrument.manager.http.server.HTTPServer; 36 37 49 public class InstrumentManagerHTTPConnector 50 extends AbstractLogEnabledInstrumentable 51 implements DefaultInstrumentManagerConnector, Configurable, Startable 52 { 53 54 public static final int DEFAULT_PORT = 15080; 55 56 57 public static final String ENCODING = "UTF-8"; 58 public static final String XML_BANNER = "<?xml version='1.0' encoding='" + ENCODING + "'?>"; 59 60 61 private DefaultInstrumentManager m_manager; 62 63 64 private int m_port; 65 66 67 private InetAddress m_bindAddr; 68 69 70 private boolean m_xml; 71 72 73 private boolean m_html; 74 75 76 private int m_chartWidth; 77 78 79 private int m_chartHeight; 80 81 82 private boolean m_antialias; 83 84 86 private int m_maxLeasedSamples; 87 88 90 private int m_maxLeasedSampleSize; 91 92 94 private long m_maxLeasedSampleLease; 95 96 98 private boolean m_readOnly; 99 100 101 private String m_rootBreadCrumbURL; 102 103 104 private String m_rootBreadCrumbLabel; 105 106 private HTTPServer m_httpServer; 107 108 111 114 public InstrumentManagerHTTPConnector() 115 { 116 setInstrumentableName( "http" ); 117 } 118 119 122 127 public void setInstrumentManager( DefaultInstrumentManager manager ) 128 { 129 m_manager = manager; 130 } 131 132 135 public void configure( Configuration configuration ) 136 throws ConfigurationException 137 { 138 m_port = configuration.getAttributeAsInteger( "port", DEFAULT_PORT ); 139 140 String bindAddress = configuration.getChild( "bind" ).getValue( null ); 141 try 142 { 143 if ( null != bindAddress ) 144 { 145 m_bindAddr = InetAddress.getByName( bindAddress ); 146 } 147 } 148 catch ( final UnknownHostException e ) 149 { 150 throw new ConfigurationException( 151 "Unable to resolve the bind point: " + bindAddress, e ); 152 } 153 154 m_xml = configuration.getAttributeAsBoolean( "xml", true ); 155 m_html = configuration.getAttributeAsBoolean( "html", true ); 156 157 m_chartWidth = configuration.getAttributeAsInteger( "chart-width", 600 ); 158 m_chartHeight = configuration.getAttributeAsInteger( "chart-height", 120 ); 159 m_antialias = configuration.getAttributeAsBoolean( "antialias", false ); 160 161 int maxSamples = m_manager.getMaxLeasedSamples(); 164 m_maxLeasedSamples = Math.min( maxSamples, configuration.getAttributeAsInteger( 165 "max-leased-samples", maxSamples ) ); 166 167 int maxSize = m_manager.getMaxLeasedSampleSize(); 168 m_maxLeasedSampleSize = Math.min( maxSize, configuration.getAttributeAsInteger( 169 "max-leased-sample-size", maxSize ) ); 170 171 long maxLease = m_manager.getMaxLeasedSampleLease(); 172 m_maxLeasedSampleLease = Math.min( maxLease, 1000L * configuration.getAttributeAsInteger( 173 "max-leased-sample-lease", (int)( maxLease / 1000 ) ) ); 174 175 m_readOnly = configuration.getAttributeAsBoolean( "read-only", false ); 176 177 m_rootBreadCrumbURL = configuration.getAttribute( "root-bread-crumb-url", null ); 178 m_rootBreadCrumbLabel = configuration.getAttribute( 179 "root-bread-crumb-label", m_rootBreadCrumbURL ); 180 181 String accessLogFile = configuration.getAttribute( "access-log", null ); 182 183 m_httpServer = new HTTPServer( m_port, m_bindAddr ); 184 m_httpServer.enableLogging( getLogger().getChildLogger( "server" ) ); 185 m_httpServer.setInstrumentableName( "server" ); 186 m_httpServer.setAccessLogFile( accessLogFile ); 187 addChildInstrumentable( m_httpServer ); 188 } 189 190 193 public void start() 194 throws Exception 195 { 196 if ( m_xml ) 198 { 199 String nameBase = "xml-"; 201 initAndRegisterHandler( new XMLInstrumentManagerHandler( m_manager, this ), 202 nameBase + "instrument-manager" ); 203 initAndRegisterHandler( new XMLInstrumentableHandler( m_manager, this ), 204 nameBase + "instrumentable" ); 205 initAndRegisterHandler( new XMLInstrumentHandler( m_manager, this ), 206 nameBase + "instrument" ); 207 initAndRegisterHandler( new XMLSampleHandler( m_manager, this ), nameBase + "sample" ); 208 initAndRegisterHandler( new XMLSnapshotHandler( m_manager, this ), nameBase + "snapshot" ); 209 initAndRegisterHandler( new XMLSnapshotsHandler( m_manager, this ), nameBase + "snapshots" ); 210 211 if ( !m_readOnly ) 212 { 213 initAndRegisterHandler( 214 new XMLSampleLeaseHandler( m_manager, this ), nameBase + "sample-lease" ); 215 initAndRegisterHandler( 216 new XMLSampleLeasesHandler( m_manager, this ), nameBase + "sample-leases" ); 217 initAndRegisterHandler( 218 new XMLCreateSampleHandler( m_manager, this ), nameBase + "create-sample" ); 219 initAndRegisterHandler( 220 new XMLCreateSamplesHandler( m_manager, this ), nameBase + "create-samples" ); 221 initAndRegisterHandler( new XMLGCHandler( m_manager, this ), nameBase + "gc" ); 222 } 223 } 224 225 if ( m_html ) 226 { 227 String nameBase = "html-"; 229 initAndRegisterHandler( new HTMLInstrumentManagerHandler( m_manager, this ), 230 nameBase + "instrument-manager" ); 231 initAndRegisterHandler( new HTMLInstrumentableHandler( m_manager, this ), 232 nameBase + "instrumentable" ); 233 initAndRegisterHandler( new HTMLInstrumentHandler( m_manager, this ), 234 nameBase + "instrument" ); 235 initAndRegisterHandler( new HTMLSampleHandler( m_manager, this ), 236 nameBase + "sample" ); 237 initAndRegisterHandler( new SampleChartHandler( 238 m_manager, m_chartWidth, m_chartHeight, m_antialias ), "sample-chart" ); 239 initAndRegisterHandler( new FavIconHandler(), "favicon" ); 240 241 if ( !m_readOnly ) 242 { 243 initAndRegisterHandler( 244 new HTMLSampleLeaseHandler( m_manager, this ), nameBase + "sample-lease" ); 245 initAndRegisterHandler( 246 new HTMLCreateSampleHandler( m_manager, this ), nameBase + "create-sample" ); 247 initAndRegisterHandler( new HTMLGCHandler( m_manager, this ), nameBase + "gc" ); 248 } 249 250 initAndRegisterHandler( new HTMLRootHandler( m_manager, this ), nameBase + "root" ); 252 } 253 254 getLogger().debug( "Starting Instrument Manager HTTP Connector" ); 255 m_httpServer.start(); 256 getLogger().info( "Instrument Manager HTTP Connector listening on port: " + m_port ); 257 } 258 259 public void stop() 260 throws Exception 261 { 262 getLogger().debug( "Stopping Instrument Manager HTTP Connector" ); 263 m_httpServer.stop(); 264 m_httpServer = null; 265 } 266 267 270 275 int getMaxLeasedSamples() 276 { 277 return m_maxLeasedSamples; 278 } 279 280 285 int getMaxLeasedSampleSize() 286 { 287 return m_maxLeasedSampleSize; 288 } 289 290 296 long getMaxLeasedSampleLease() 297 { 298 return m_maxLeasedSampleLease; 299 } 300 301 307 boolean isReadOnly() 308 { 309 return m_readOnly; 310 } 311 312 317 String getRootBreadCrumbURL() 318 { 319 return m_rootBreadCrumbURL; 320 } 321 322 327 String getRootBreadCrumbLabel() 328 { 329 return m_rootBreadCrumbLabel; 330 } 331 332 private void initAndRegisterHandler( AbstractHTTPURLHandler handler, String name ) 333 throws Exception 334 { 335 handler.enableLogging( getLogger().getChildLogger( name ) ); 336 handler.setInstrumentableName( name ); 337 addChildInstrumentable( handler ); 338 339 m_httpServer.registerHandler( handler ); 340 } 341 } 342 343 | Popular Tags |