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.component.ComponentManager; 31 import org.apache.avalon.framework.logger.Logger; 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 51 public abstract class AbstractComponentManagerServlet 52 extends HttpServlet 53 implements Instrumentable 54 { 55 private String m_referenceName; 56 private ComponentManager m_componentManager; 57 private Logger m_logger; 58 59 60 private String m_instrumentableName; 61 62 63 private ArrayList m_instrumentList; 64 65 66 private ArrayList m_childList; 67 68 69 private boolean m_registered; 70 71 72 private CounterInstrument m_instrumentRequests; 73 74 75 private ValueInstrument m_instrumentTime; 76 77 80 87 public AbstractComponentManagerServlet( String referenceName ) 88 { 89 m_referenceName = referenceName; 91 92 m_registered = false; 94 m_instrumentList = new ArrayList (); 95 m_childList = new ArrayList (); 96 97 setInstrumentableName( referenceName ); 99 addInstrument( m_instrumentRequests = new CounterInstrument( "requests" ) ); 100 addInstrument( m_instrumentTime = new ValueInstrument( "time" ) ); 101 } 102 103 106 114 public void init( ServletConfig config ) 115 throws ServletException 116 { 117 ServletContext context = config.getServletContext(); 118 119 LoggerManager loggerManager = 121 (LoggerManager)context.getAttribute( LoggerManager.class.getName() ); 122 if( loggerManager == null ) 123 { 124 throw new IllegalStateException ( 125 "The ExcaliburComponentManagerServlet servlet was not correctly initialized." ); 126 } 127 Logger logger = loggerManager.getLoggerForCategory( "servlet" ); 128 m_logger = logger.getChildLogger( m_referenceName ); 129 130 if( getLogger().isDebugEnabled() ) 131 { 132 getLogger().debug( "servlet.init( config )" ); 133 } 134 135 m_componentManager = 137 (ComponentManager)context.getAttribute( ComponentManager.class.getName() ); 138 if( m_componentManager == null ) 139 { 140 throw new IllegalStateException ( 141 "The ExcaliburComponentManagerServlet servlet was not correctly initialized." ); 142 } 143 144 InstrumentManager instrumentManager = 146 (InstrumentManager)context.getAttribute( InstrumentManager.class.getName() ); 147 if( instrumentManager != null ) 148 { 149 try 150 { 151 instrumentManager.registerInstrumentable( 152 this, "servlets." + getInstrumentableName() ); 153 } 154 catch( Exception e ) 155 { 156 throw new ServletException ( 157 "Unable to register the servlet with the instrument manager.", e ); 158 } 159 } 160 161 super.init( config ); 164 } 165 166 170 public void destroy() 171 { 172 if( getLogger().isDebugEnabled() ) 173 { 174 getLogger().debug( "servlet.destroy()" ); 175 } 176 177 m_componentManager = null; 179 180 super.destroy(); 181 182 System.gc(); 184 185 try 189 { 190 Thread.sleep( 250 ); 191 } 192 catch( InterruptedException e ) 193 { 194 } 195 } 196 197 207 public void service( HttpServletRequest request, HttpServletResponse response ) 208 throws ServletException , IOException 209 { 210 if( getLogger().isDebugEnabled() ) 211 { 212 StringBuffer sb = new StringBuffer ( request.getRequestURI() ); 213 String query = request.getQueryString(); 214 if( query != null ) 215 { 216 sb.append( "?" ); 217 sb.append( query ); 218 } 219 220 getLogger().debug( "Request: " + sb.toString() ); 221 } 222 223 long start = System.currentTimeMillis(); 224 225 m_instrumentRequests.increment(); 227 228 super.service( request, response ); 229 230 if( m_instrumentTime.isActive() ) 232 { 233 m_instrumentTime.setValue( (int)( System.currentTimeMillis() - start ) ); 234 } 235 } 236 237 240 245 public final String getInstrumentableName() 246 { 247 return m_instrumentableName; 248 } 249 250 263 public final void setInstrumentableName( String name ) 264 { 265 m_instrumentableName = name; 266 } 267 268 277 public final Instrumentable[] getChildInstrumentables() 278 { 279 m_registered = true; 280 if( m_childList.size() == 0 ) 281 { 282 return Instrumentable.EMPTY_INSTRUMENTABLE_ARRAY; 283 } 284 else 285 { 286 Instrumentable[] children = new Instrumentable[ m_childList.size() ]; 287 m_childList.toArray( children ); 288 return children; 289 } 290 } 291 292 303 public final Instrument[] getInstruments() 304 { 305 m_registered = true; 306 if( m_instrumentList.size() == 0 ) 307 { 308 return Instrumentable.EMPTY_INSTRUMENT_ARRAY; 309 } 310 else 311 { 312 Instrument[] instruments = new Instrument[ m_instrumentList.size() ]; 313 m_instrumentList.toArray( instruments ); 314 return instruments; 315 } 316 } 317 318 321 328 protected void addInstrument( Instrument instrument ) 329 { 330 if( m_registered ) 331 { 332 throw new IllegalStateException ( "Instruments can not be added after the " 333 + "Instrumentable is registered with the InstrumentManager." ); 334 } 335 m_instrumentList.add( instrument ); 336 } 337 338 348 protected void addChildInstrumentable( Instrumentable child ) 349 { 350 if( m_registered ) 351 { 352 throw new IllegalStateException ( "Child Instrumentables can not be added after the " 353 + "Instrumentable is registered with the InstrumentManager." ); 354 } 355 m_childList.add( child ); 356 } 357 358 363 protected Logger getLogger() 364 { 365 return m_logger; 366 } 367 368 373 public ComponentManager getComponentManager() 374 { 375 return m_componentManager; 376 } 377 } 378 | Popular Tags |