KickJava   Java API By Example, From Geeks To Geeks.

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


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

16 package org.jboss.web.tomcat.tc6;
17
18
19 import java.io.IOException JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MBeanServerNotification JavaDoc;
28 import javax.management.Notification JavaDoc;
29 import javax.management.NotificationListener JavaDoc;
30 import javax.management.ObjectInstance JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.http.HttpServlet JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37
38 import org.apache.tomcat.util.modeler.Registry;
39
40 import org.apache.catalina.util.ServerInfo;
41 import org.apache.catalina.util.StringManager;
42 import org.apache.catalina.manager.Constants;
43 import org.apache.catalina.manager.StatusTransformer;
44
45 /**
46  * This servlet will display a complete status of the HTTP/1.1 connector.
47  *
48  * @author Remy Maucherat
49  * @version $Revision: 56603 $ $Date: 2006-09-06 09:57:20 -0400 (Wed, 06 Sep 2006) $
50  */

51
52 public class StatusServlet
53    extends HttpServlet JavaDoc implements NotificationListener JavaDoc
54 {
55
56
57    // ----------------------------------------------------- Instance Variables
58

59
60    /**
61     * The debugging detail level for this servlet.
62     */

63    private int debug = 0;
64
65
66    /**
67     * MBean server.
68     */

69    protected MBeanServer JavaDoc mBeanServer = null;
70
71
72    /**
73     * Vector of protocol handlers object names.
74     */

75    protected Vector JavaDoc protocolHandlers = new Vector JavaDoc();
76
77
78    /**
79     * Vector of thread pools object names.
80     */

81    protected Vector JavaDoc threadPools = new Vector JavaDoc();
82
83
84    /**
85     * Vector of request processors object names.
86     */

87    protected Vector JavaDoc requestProcessors = new Vector JavaDoc();
88
89
90    /**
91     * Vector of global request processors object names.
92     */

93    protected Vector JavaDoc globalRequestProcessors = new Vector JavaDoc();
94
95
96    /**
97     * The string manager for this package.
98     */

99    protected static StringManager sm =
100       StringManager.getManager(Constants.Package);
101
102
103    // --------------------------------------------------------- Public Methods
104

105
106    /**
107     * Initialize this servlet.
108     */

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

191    public void destroy()
192    {
193
194       ; // No actions necessary
195

196    }
197
198
199    /**
200     * Process a GET request for the specified resource.
201     *
202     * @param request The servlet request we are processing
203     * @param response The servlet response we are creating
204     * @throws IOException if an input/output error occurs
205     * @throws ServletException if a servlet-specified error occurs
206     */

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

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

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