1 17 package org.apache.avalon.excalibur.component.servlet; 18 19 import java.io.IOException ; 20 import java.util.ArrayList ; 21 22 import javax.servlet.ServletConfig ; 23 import javax.servlet.ServletContext ; 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServlet ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import org.apache.avalon.excalibur.logger.LoggerManager; 30 import org.apache.avalon.framework.logger.Logger; 31 import org.apache.avalon.framework.service.ServiceManager; 32 import org.apache.excalibur.instrument.CounterInstrument; 33 import org.apache.excalibur.instrument.Instrument; 34 import org.apache.excalibur.instrument.InstrumentManager; 35 import org.apache.excalibur.instrument.Instrumentable; 36 import org.apache.excalibur.instrument.ValueInstrument; 37 38 49 public abstract class AbstractServiceManagerServlet 50 extends HttpServlet 51 implements Instrumentable 52 { 53 private String m_referenceName; 54 private ServiceManager m_serviceManager; 55 private Logger m_logger; 56 57 58 private String m_instrumentableName; 59 60 61 private ArrayList m_instrumentList; 62 63 64 private ArrayList m_childList; 65 66 67 private boolean m_registered; 68 69 70 private CounterInstrument m_instrumentRequests; 71 72 73 private ValueInstrument m_instrumentTime; 74 75 78 85 public AbstractServiceManagerServlet( String referenceName ) 86 { 87 m_referenceName = referenceName; 89 90 m_registered = false; 92 m_instrumentList = new ArrayList (); 93 m_childList = new ArrayList (); 94 95 setInstrumentableName( referenceName ); 97 addInstrument( m_instrumentRequests = new CounterInstrument( "requests" ) ); 98 addInstrument( m_instrumentTime = new ValueInstrument( "time" ) ); 99 } 100 101 104 112 public void init( ServletConfig config ) 113 throws ServletException 114 { 115 ServletContext context = config.getServletContext(); 116 117 LoggerManager loggerManager = 119 (LoggerManager)context.getAttribute( LoggerManager.class.getName() ); 120 if ( loggerManager == null ) 121 { 122 throw new IllegalStateException ( 123 "The ExcaliburComponentManagerServlet servlet was not correctly initialized." ); 124 } 125 Logger logger = loggerManager.getLoggerForCategory( "servlet" ); 126 m_logger = logger.getChildLogger( m_referenceName ); 127 128 if ( getLogger().isDebugEnabled() ) 129 { 130 getLogger().debug( "servlet.init( config )" ); 131 } 132 133 m_serviceManager = 135 (ServiceManager)context.getAttribute( ServiceManager.class.getName() ); 136 if ( m_serviceManager == null ) 137 { 138 throw new IllegalStateException ( 139 "The ExcaliburComponentManagerServlet servlet was not correctly initialized." ); 140 } 141 142 InstrumentManager instrumentManager = 144 (InstrumentManager)context.getAttribute( InstrumentManager.class.getName() ); 145 if ( instrumentManager != null ) 146 { 147 try 148 { 149 instrumentManager.registerInstrumentable( 150 this, "servlets." + getInstrumentableName() ); 151 } 152 catch ( Exception e ) 153 { 154 throw new ServletException ( 155 "Unable to register the servlet with the instrument manager.", e ); 156 } 157 } 158 159 super.init( config ); 162 } 163 164 168 public void destroy() 169 { 170 if ( getLogger().isDebugEnabled() ) 171 { 172 getLogger().debug( "servlet.destroy()" ); 173 } 174 175 m_serviceManager = null; 177 178 super.destroy(); 179 180 System.gc(); 182 183 try 187 { 188 Thread.sleep(250); 189 } 190 catch ( InterruptedException e ) 191 { 192 } 193 } 194 195 205 public void service( HttpServletRequest request, HttpServletResponse response ) 206 throws ServletException , IOException 207 { 208 if ( getLogger().isDebugEnabled() ) 209 { 210 StringBuffer sb = new StringBuffer ( request.getRequestURI() ); 211 String query = request.getQueryString(); 212 if ( query != null ) 213 { 214 sb.append( "?" ); 215 sb.append( query ); 216 } 217 218 getLogger().debug( "Request: " + sb.toString() ); 219 } 220 221 long start = System.currentTimeMillis(); 222 223 m_instrumentRequests.increment(); 225 226 super.service( request, response ); 227 228 if ( m_instrumentTime.isActive() ) 230 { 231 m_instrumentTime.setValue( (int)( System.currentTimeMillis() - start ) ); 232 } 233 } 234 235 238 243 public final String getInstrumentableName() 244 { 245 return m_instrumentableName; 246 } 247 248 261 public final void setInstrumentableName( String name ) 262 { 263 m_instrumentableName = name; 264 } 265 266 275 public final Instrumentable[] getChildInstrumentables() 276 { 277 m_registered = true; 278 if( m_childList.size() == 0 ) 279 { 280 return Instrumentable.EMPTY_INSTRUMENTABLE_ARRAY; 281 } 282 else 283 { 284 Instrumentable[] children = new Instrumentable[ m_childList.size() ]; 285 m_childList.toArray( children ); 286 return children; 287 } 288 } 289 290 301 public final Instrument[] getInstruments() 302 { 303 m_registered = true; 304 if( m_instrumentList.size() == 0 ) 305 { 306 return Instrumentable.EMPTY_INSTRUMENT_ARRAY; 307 } 308 else 309 { 310 Instrument[] instruments = new Instrument[ m_instrumentList.size() ]; 311 m_instrumentList.toArray( instruments ); 312 return instruments; 313 } 314 } 315 316 319 326 protected void addInstrument( Instrument instrument ) 327 { 328 if( m_registered ) 329 { 330 throw new IllegalStateException ( "Instruments can not be added after the " 331 + "Instrumentable is registered with the InstrumentManager." ); 332 } 333 m_instrumentList.add( instrument ); 334 } 335 336 346 protected void addChildInstrumentable( Instrumentable child ) 347 { 348 if( m_registered ) 349 { 350 throw new IllegalStateException ( "Child Instrumentables can not be added after the " 351 + "Instrumentable is registered with the InstrumentManager." ); 352 } 353 m_childList.add( child ); 354 } 355 356 361 protected Logger getLogger() 362 { 363 return m_logger; 364 } 365 366 371 public ServiceManager getServiceManager() 372 { 373 return m_serviceManager; 374 } 375 } 376 | Popular Tags |