KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > tc5 > StatusServlet


1 /*
2  * JBoss, the OpenSource WebOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.web.tomcat.tc5;
8
9
10 import java.io.IOException JavaDoc;
11 import java.io.PrintWriter JavaDoc;
12 import java.util.Enumeration JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Set JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 import javax.management.MBeanServer JavaDoc;
18 import javax.management.MBeanServerNotification JavaDoc;
19 import javax.management.Notification JavaDoc;
20 import javax.management.NotificationListener JavaDoc;
21 import javax.management.ObjectInstance JavaDoc;
22 import javax.management.ObjectName JavaDoc;
23
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.commons.modeler.Registry;
30
31 import org.apache.catalina.util.ServerInfo;
32 import org.apache.catalina.util.StringManager;
33 import org.apache.catalina.manager.Constants;
34 import org.apache.catalina.manager.StatusTransformer;
35
36 /**
37  * This servlet will display a complete status of the HTTP/1.1 connector.
38  *
39  * @author Remy Maucherat
40  * @version $Revision: 1.5.2.1 $ $Date: 2005/05/18 00:16:17 $
41  */

42
43 public class StatusServlet
44    extends HttpServlet JavaDoc implements NotificationListener JavaDoc
45 {
46
47
48    // ----------------------------------------------------- Instance Variables
49

50
51    /**
52     * The debugging detail level for this servlet.
53     */

54    private int debug = 0;
55
56
57    /**
58     * MBean server.
59     */

60    protected MBeanServer JavaDoc mBeanServer = null;
61
62
63    /**
64     * Vector of protocol handlers object names.
65     */

66    protected Vector JavaDoc protocolHandlers = new Vector JavaDoc();
67
68
69    /**
70     * Vector of thread pools object names.
71     */

72    protected Vector JavaDoc threadPools = new Vector JavaDoc();
73
74
75    /**
76     * Vector of request processors object names.
77     */

78    protected Vector JavaDoc requestProcessors = new Vector JavaDoc();
79
80
81    /**
82     * Vector of global request processors object names.
83     */

84    protected Vector JavaDoc globalRequestProcessors = new Vector JavaDoc();
85
86
87    /**
88     * The string manager for this package.
89     */

90    protected static StringManager sm =
91       StringManager.getManager(Constants.Package);
92
93
94    // --------------------------------------------------------- Public Methods
95

96
97    /**
98     * Initialize this servlet.
99     */

100    public void init() throws ServletException JavaDoc
101    {
102
103       // Retrieve the MBean server
104
mBeanServer = Registry.getServer();
105
106       // Set our properties from the initialization parameters
107
String JavaDoc value = null;
108       try
109       {
110          value = getServletConfig().getInitParameter("debug");
111          debug = Integer.parseInt(value);
112       }
113       catch (Throwable JavaDoc t)
114       {
115          ;
116       }
117
118       try
119       {
120
121          // Query protocol handlers
122
String JavaDoc onStr = "*:type=ProtocolHandler,*";
123          ObjectName JavaDoc objectName = new ObjectName JavaDoc(onStr);
124          Set JavaDoc set = mBeanServer.queryMBeans(objectName, null);
125          Iterator JavaDoc iterator = set.iterator();
126          while (iterator.hasNext())
127          {
128             ObjectInstance JavaDoc oi = (ObjectInstance JavaDoc) iterator.next();
129             protocolHandlers.addElement(oi.getObjectName());
130          }
131
132          // Query Thread Pools
133
onStr = "*:type=ThreadPool,*";
134          objectName = new ObjectName JavaDoc(onStr);
135          set = mBeanServer.queryMBeans(objectName, null);
136          iterator = set.iterator();
137          while (iterator.hasNext())
138          {
139             ObjectInstance JavaDoc oi = (ObjectInstance JavaDoc) iterator.next();
140             threadPools.addElement(oi.getObjectName());
141          }
142
143          // Query Global Request Processors
144
onStr = "*:type=GlobalRequestProcessor,*";
145          objectName = new ObjectName JavaDoc(onStr);
146          set = mBeanServer.queryMBeans(objectName, null);
147          iterator = set.iterator();
148          while (iterator.hasNext())
149          {
150             ObjectInstance JavaDoc oi = (ObjectInstance JavaDoc) iterator.next();
151             globalRequestProcessors.addElement(oi.getObjectName());
152          }
153
154          // Query Request Processors
155
onStr = "*:type=RequestProcessor,*";
156          objectName = new ObjectName JavaDoc(onStr);
157          set = mBeanServer.queryMBeans(objectName, null);
158          iterator = set.iterator();
159          while (iterator.hasNext())
160          {
161             ObjectInstance JavaDoc oi = (ObjectInstance JavaDoc) iterator.next();
162             requestProcessors.addElement(oi.getObjectName());
163          }
164
165          // Register with MBean server
166
onStr = "JMImplementation:type=MBeanServerDelegate";
167          objectName = new ObjectName JavaDoc(onStr);
168          mBeanServer.addNotificationListener(objectName, this, null, null);
169
170       }
171       catch (Exception JavaDoc e)
172       {
173          e.printStackTrace();
174       }
175
176    }
177
178
179    /**
180     * Finalize this servlet.
181     */

182    public void destroy()
183    {
184
185       ; // No actions necessary
186

187    }
188
189
190    /**
191     * Process a GET request for the specified resource.
192     *
193     * @param request The servlet request we are processing
194     * @param response The servlet response we are creating
195     * @throws IOException if an input/output error occurs
196     * @throws ServletException if a servlet-specified error occurs
197     */

198    public void doGet(HttpServletRequest JavaDoc request,
199       HttpServletResponse JavaDoc response)
200       throws IOException JavaDoc, ServletException JavaDoc
201    {
202
203       // mode is flag for HTML or XML output
204
int mode = 0;
205       // if ?XML=true, set the mode to XML
206
if ("true".equals(request.getParameter("XML")))
207       {
208          mode = 1;
209       }
210       StatusTransformer.setContentType(response, mode);
211
212       PrintWriter JavaDoc writer = response.getWriter();
213
214       boolean completeStatus = false;
215       if ("true".equals(request.getParameter("full")))
216       {
217          completeStatus = true;
218       }
219         
220       // use StatusTransformer to output status
221
if (mode == 0)
222       {
223          // HTML Header Section
224
writer.print(HTML_HEADER);
225       }
226       else if (mode == 1)
227       {
228          writer.write(Constants.XML_DECLARATION);
229          writer.write
230             (Constants.XML_STYLE);
231          writer.write("<status>");
232       }
233
234       try
235       {
236
237          // Display virtual machine statistics
238
StatusTransformer.writeVMState(writer, mode);
239
240          Enumeration JavaDoc i = threadPools.elements();
241          while (i.hasMoreElements())
242          {
243             ObjectName JavaDoc objectName = (ObjectName JavaDoc) i.nextElement();
244             String JavaDoc name = objectName.getKeyProperty("name");
245             // use StatusTransformer to output status
246
StatusTransformer.writeConnectorState
247                (writer, objectName,
248                   name, mBeanServer, globalRequestProcessors,
249                   requestProcessors, mode);
250          }
251
252          if (completeStatus)
253          {
254             // Note: Retrieving the full status is much slower
255
// use StatusTransformer to output status
256
StatusTransformer.writeDetailedState
257                (writer, mBeanServer, mode);
258          }
259
260       }
261       catch (Exception JavaDoc e)
262       {
263          throw new ServletException JavaDoc(e);
264       }
265
266       if (mode == 0)
267       {
268          writer.print(HTML_FOOTER);
269       }
270       else if (mode == 1)
271       {
272          writer.write("</status>");
273       }
274
275    }
276
277    // ------------------------------------------- NotificationListener Methods
278

279
280    public void handleNotification(Notification JavaDoc notification,
281       java.lang.Object JavaDoc handback)
282    {
283
284       if (notification instanceof MBeanServerNotification JavaDoc)
285       {
286          ObjectName JavaDoc objectName =
287             ((MBeanServerNotification JavaDoc) notification).getMBeanName();
288          if (notification.getType().equals
289             (MBeanServerNotification.REGISTRATION_NOTIFICATION))
290          {
291             String JavaDoc type = objectName.getKeyProperty("type");
292             if (type != null)
293             {
294                if (type.equals("ProtocolHandler"))
295                {
296                   protocolHandlers.addElement(objectName);
297                }
298                else if (type.equals("ThreadPool"))
299                {
300                   threadPools.addElement(objectName);
301                }
302                else if (type.equals("GlobalRequestProcessor"))
303                {
304                   globalRequestProcessors.addElement(objectName);
305                }
306                else if (type.equals("RequestProcessor"))
307                {
308                   requestProcessors.addElement(objectName);
309                }
310             }
311          }
312          else if (notification.getType().equals
313             (MBeanServerNotification.UNREGISTRATION_NOTIFICATION))
314          {
315             String JavaDoc type = objectName.getKeyProperty("type");
316             if (type != null)
317             {
318                if (type.equals("ProtocolHandler"))
319                {
320                   protocolHandlers.removeElement(objectName);
321                }
322                else if (type.equals("ThreadPool"))
323                {
324                   threadPools.removeElement(objectName);
325                }
326                else if (type.equals("GlobalRequestProcessor"))
327                {
328                   globalRequestProcessors.removeElement(objectName);
329                }
330                else if (type.equals("RequestProcessor"))
331                {
332                   requestProcessors.removeElement(objectName);
333                }
334             }
335             String JavaDoc j2eeType = objectName.getKeyProperty("j2eeType");
336             if (j2eeType != null)
337             {
338
339             }
340          }
341       }
342
343    }
344
345
346    // ------------------------------------------------------- Private Constats
347

348
349    private static final String JavaDoc HTML_HEADER =
350       "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"
351       + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
352       + "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
353       + "<head>"
354       + "<title>Tomcat Status</title>"
355       + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />"
356       + "<link rel=\"StyleSheet\" HREF=\"jboss.css\" type=\"text/css\"/>"
357       + "</head>"
358       + "<body>"
359       + "<!-- header begin -->"
360       + "<a HREF=\"http://www.jboss.org\">"
361       + "<img SRC=\"logo.gif\" alt=\"JBoss\" id=\"logo\" width=\"226\" height=\"105\" />"
362       + "</a>"
363       + "<div id=\"header\">&nbsp;</div>"
364       + "<div id=\"navigation_bar\">"
365       + "</div>"
366       + "<!-- header end -->";
367
368    private static final String JavaDoc HTML_FOOTER =
369       "<!-- footer begin -->"
370       + "<div id=\"footer\">"
371       + "<div id=\"credits\">JBoss&trade; Application Server</div>"
372       + "<div id=\"footer_bar\">&nbsp;</div>"
373       + "</div>"
374       + "<!-- footer end -->"
375       + "</body>"
376       + "</html>";
377
378
379 }
380
Popular Tags