KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > component > servlet > AbstractComponentManagerServlet


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.avalon.excalibur.component.servlet;
18
19 import java.io.IOException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21
22 import javax.servlet.ServletConfig JavaDoc;
23 import javax.servlet.ServletContext JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServlet JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
39  * Abstract Servlet which can be used with the ExcaliburComponentManagerServlet
40  * to enable servlets to have access to a ComponentManager as well as logging
41  * and instrumentation features.
42  *
43  * @deprecated Use of this class has been deprecated along with the
44  * ComponentManager interface. Please use the
45  * AbstractServiceManagerServlet.
46  *
47  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
48  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:16 $
49  * @since 4.2
50  */

51 public abstract class AbstractComponentManagerServlet
52     extends HttpServlet JavaDoc
53     implements Instrumentable
54 {
55     private String JavaDoc m_referenceName;
56     private ComponentManager m_componentManager;
57     private Logger m_logger;
58
59     /** Instrumentable Name assigned to this Instrumentable */
60     private String JavaDoc m_instrumentableName;
61
62     /** Stores the instruments during initialization. */
63     private ArrayList JavaDoc m_instrumentList;
64
65     /** Stores the child instrumentables during initialization. */
66     private ArrayList JavaDoc m_childList;
67
68     /** Flag which is to used to keep track of when the Instrumentable has been registered. */
69     private boolean m_registered;
70
71     /** Counts the number of times the service is requested. */
72     private CounterInstrument m_instrumentRequests;
73
74     /** Records the amount of time execute takes to be processed. */
75     private ValueInstrument m_instrumentTime;
76
77     /*---------------------------------------------------------------
78      * Constructors
79      *-------------------------------------------------------------*/

80     /**
81      * Create a new AbstractComponentManagerServlet.
82      *
83      * @param referenceName A name which does not include any spaces or periods
84      * that will be used to name the logger category and
85      * instrumentable which represents this servlet.
86      */

87     public AbstractComponentManagerServlet( String JavaDoc referenceName )
88     {
89         //System.out.println( "AbstractComponentManagerServlet( " + referenceName + " )" );
90
m_referenceName = referenceName;
91
92         // Set up Instrumentable like AbstractInstrumentable
93
m_registered = false;
94         m_instrumentList = new ArrayList JavaDoc();
95         m_childList = new ArrayList JavaDoc();
96
97         // Create the instruments
98
setInstrumentableName( referenceName );
99         addInstrument( m_instrumentRequests = new CounterInstrument( "requests" ) );
100         addInstrument( m_instrumentTime = new ValueInstrument( "time" ) );
101     }
102
103     /*---------------------------------------------------------------
104      * HttpServlet Methods
105      *-------------------------------------------------------------*/

106     /**
107      * Called by the servlet container to initialize a servlet before it is
108      * put into service.
109      *
110      * @param config ServletConfig object for the servlet.
111      *
112      * @throws ServletException If there are any initialization problems.
113      */

114     public void init( ServletConfig JavaDoc config )
115         throws ServletException JavaDoc
116     {
117         ServletContext JavaDoc context = config.getServletContext();
118
119         // Initialize logging for the servlet.
120
LoggerManager loggerManager =
121             (LoggerManager)context.getAttribute( LoggerManager.class.getName() );
122         if( loggerManager == null )
123         {
124             throw new IllegalStateException JavaDoc(
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         // Obtain a reference to the ComponentManager
136
m_componentManager =
137             (ComponentManager)context.getAttribute( ComponentManager.class.getName() );
138         if( m_componentManager == null )
139         {
140             throw new IllegalStateException JavaDoc(
141                 "The ExcaliburComponentManagerServlet servlet was not correctly initialized." );
142         }
143
144         // Register this servlet with the InstrumentManager if it exists.
145
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 JavaDoc e )
155             {
156                 throw new ServletException JavaDoc(
157                     "Unable to register the servlet with the instrument manager.", e );
158             }
159         }
160
161         // Do this last so the subclasses will be able to access these objects in their
162
// init method.
163
super.init( config );
164     }
165
166     /**
167      * Called by the servlet container to indicate to a servlet that the servlet
168      * is being taken out of service.
169      */

170     public void destroy()
171     {
172         if( getLogger().isDebugEnabled() )
173         {
174             getLogger().debug( "servlet.destroy()" );
175         }
176
177         // Release the ComponentManager by removing its reference.
178
m_componentManager = null;
179
180         super.destroy();
181
182         // Make sure that the component manager gets collected.
183
System.gc();
184
185         // Give the system time for the Gc to complete. This is necessary to make sure that
186
// the ECMServlet has time to dispose all of its managers before the Tomcat server
187
// invalidates the current class loader.
188
try
189         {
190             Thread.sleep( 250 );
191         }
192         catch( InterruptedException JavaDoc e )
193         {
194         }
195     }
196
197     /**
198      * Receives standard HTTP requests from the public service method and dispatches
199      * them to the doXXX methods defined in this class.
200      * Overrides the default method to allow for instrumentation.
201      *
202      * @param request The HttpServletRequest object that contains the request the
203      * client made of the servlet.
204      * @param response The HttpServletResponse object that contains the response
205      * the servlet returns to the client.
206      */

207     public void service( HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response )
208         throws ServletException JavaDoc, IOException JavaDoc
209     {
210         if( getLogger().isDebugEnabled() )
211         {
212             StringBuffer JavaDoc sb = new StringBuffer JavaDoc( request.getRequestURI() );
213             String JavaDoc 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         // Notify the Instrument Manager
226
m_instrumentRequests.increment();
227
228         super.service( request, response );
229
230         // Notify the Instrument Manager how long the service took.
231
if( m_instrumentTime.isActive() )
232         {
233             m_instrumentTime.setValue( (int)( System.currentTimeMillis() - start ) );
234         }
235     }
236
237     /*---------------------------------------------------------------
238      * Instrumentable Methods
239      *-------------------------------------------------------------*/

240     /**
241      * Gets the name of the Instrumentable.
242      *
243      * @return The name used to identify a Instrumentable.
244      */

245     public final String JavaDoc getInstrumentableName()
246     {
247         return m_instrumentableName;
248     }
249
250     /**
251      * Sets the name for the Instrumentable. The Instrumentable Name is used
252      * to uniquely identify the Instrumentable during the configuration of
253      * the InstrumentManager and to gain access to an InstrumentableDescriptor
254      * through the InstrumentManager. The value should be a string which does
255      * not contain spaces or periods.
256      * <p>
257      * This value may be set by a parent Instrumentable, or by the
258      * InstrumentManager using the value of the 'instrumentable' attribute in
259      * the configuration of the component.
260      *
261      * @param name The name used to identify a Instrumentable.
262      */

263     public final void setInstrumentableName( String JavaDoc name )
264     {
265         m_instrumentableName = name;
266     }
267
268     /**
269      * Any Object which implements Instrumentable can also make use of other
270      * Instrumentable child objects. This method is used to tell the
271      * InstrumentManager about them.
272      *
273      * @return An array of child Instrumentables. This method should never
274      * return null. If there are no child Instrumentables, then
275      * EMPTY_INSTRUMENTABLE_ARRAY can be returned.
276      */

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     /**
293      * Obtain a reference to all the Instruments that the Instrumentable object
294      * wishes to expose. All sampling is done directly through the
295      * Instruments as opposed to the Instrumentable interface.
296      *
297      * @return An array of the Instruments available for profiling. Should
298      * never be null. If there are no Instruments, then
299      * EMPTY_INSTRUMENT_ARRAY can be returned. This should never be
300      * the case though unless there are child Instrumentables with
301      * Instruments.
302      */

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     /*---------------------------------------------------------------
319      * Methods
320      *-------------------------------------------------------------*/

321     /**
322      * Adds an Instrument to the list of Instruments published by the component.
323      * This method may not be called after the Instrumentable has been
324      * registered with the InstrumentManager.
325      *
326      * @param instrument Instrument to publish.
327      */

328     protected void addInstrument( Instrument instrument )
329     {
330         if( m_registered )
331         {
332             throw new IllegalStateException JavaDoc( "Instruments can not be added after the "
333                 + "Instrumentable is registered with the InstrumentManager." );
334         }
335         m_instrumentList.add( instrument );
336     }
337
338     /**
339      * Adds a child Instrumentable to the list of child Instrumentables
340      * published by the component. This method may not be called after the
341      * Instrumentable has been registered with the InstrumentManager.
342      * <p>
343      * Note that Child Instrumentables must be named by the caller using the
344      * setInstrumentableName method.
345      *
346      * @param child Child Instrumentable to publish.
347      */

348     protected void addChildInstrumentable( Instrumentable child )
349     {
350         if( m_registered )
351         {
352             throw new IllegalStateException JavaDoc( "Child Instrumentables can not be added after the "
353                 + "Instrumentable is registered with the InstrumentManager." );
354         }
355         m_childList.add( child );
356     }
357
358     /**
359      * Obtain a reference to the servlet's logger.
360      *
361      * @return The servlet's logger.
362      */

363     protected Logger getLogger()
364     {
365         return m_logger;
366     }
367
368     /**
369      * Returns the current ComponentManager.
370      *
371      * @return The current ComponentManager.
372      */

373     public ComponentManager getComponentManager()
374     {
375         return m_componentManager;
376     }
377 }
378
Popular Tags