KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > manager > StatusManagerServlet


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

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

50
51 public class StatusManagerServlet
52     extends HttpServlet JavaDoc implements NotificationListener JavaDoc {
53
54
55     // ----------------------------------------------------- Instance Variables
56

57
58     /**
59      * The debugging detail level for this servlet.
60      */

61     private int debug = 0;
62
63
64     /**
65      * MBean server.
66      */

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

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

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

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

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

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

103
104     /**
105      * Initialize this servlet.
106      */

107     public void init() throws ServletException JavaDoc {
108
109         // Retrieve the MBean server
110
mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
111
112         // Set our properties from the initialization parameters
113
String JavaDoc value = null;
114         try {
115             value = getServletConfig().getInitParameter("debug");
116             debug = Integer.parseInt(value);
117         } catch (Throwable JavaDoc t) {
118             ;
119         }
120
121         try {
122
123             // Query protocol handlers
124
String JavaDoc onStr = "*:type=ProtocolHandler,*";
125             ObjectName JavaDoc objectName = new ObjectName JavaDoc(onStr);
126             Set JavaDoc set = mBeanServer.queryMBeans(objectName, null);
127             Iterator JavaDoc iterator = set.iterator();
128             while (iterator.hasNext()) {
129                 ObjectInstance JavaDoc oi = (ObjectInstance JavaDoc) iterator.next();
130                 protocolHandlers.addElement(oi.getObjectName());
131             }
132
133             // Query Thread Pools
134
onStr = "*:type=ThreadPool,*";
135             objectName = new ObjectName JavaDoc(onStr);
136             set = mBeanServer.queryMBeans(objectName, null);
137             iterator = set.iterator();
138             while (iterator.hasNext()) {
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                 ObjectInstance JavaDoc oi = (ObjectInstance JavaDoc) iterator.next();
150                 globalRequestProcessors.addElement(oi.getObjectName());
151             }
152
153             // Query Request Processors
154
onStr = "*:type=RequestProcessor,*";
155             objectName = new ObjectName JavaDoc(onStr);
156             set = mBeanServer.queryMBeans(objectName, null);
157             iterator = set.iterator();
158             while (iterator.hasNext()) {
159                 ObjectInstance JavaDoc oi = (ObjectInstance JavaDoc) iterator.next();
160                 requestProcessors.addElement(oi.getObjectName());
161             }
162
163             // Register with MBean server
164
onStr = "JMImplementation:type=MBeanServerDelegate";
165             objectName = new ObjectName JavaDoc(onStr);
166             mBeanServer.addNotificationListener(objectName, this, null, null);
167
168         } catch (Exception JavaDoc e) {
169             e.printStackTrace();
170         }
171
172     }
173
174
175     /**
176      * Finalize this servlet.
177      */

178     public void destroy() {
179
180         ; // No actions necessary
181

182     }
183
184
185     /**
186      * Process a GET request for the specified resource.
187      *
188      * @param request The servlet request we are processing
189      * @param response The servlet response we are creating
190      *
191      * @exception IOException if an input/output error occurs
192      * @exception ServletException if a servlet-specified error occurs
193      */

194     public void doGet(HttpServletRequest JavaDoc request,
195                       HttpServletResponse JavaDoc response)
196         throws IOException JavaDoc, ServletException JavaDoc {
197
198         // mode is flag for HTML or XML output
199
int mode = 0;
200         // if ?XML=true, set the mode to XML
201
if (request.getParameter("XML") != null
202             && request.getParameter("XML").equals("true")) {
203             mode = 1;
204         }
205         StatusTransformer.setContentType(response, mode);
206
207         PrintWriter JavaDoc writer = response.getWriter();
208
209         boolean completeStatus = false;
210         if ((request.getPathInfo() != null)
211             && (request.getPathInfo().equals("/all"))) {
212             completeStatus = true;
213         }
214         // use StatusTransformer to output status
215
StatusTransformer.writeHeader(writer,mode);
216
217         // Body Header Section
218
Object JavaDoc[] args = new Object JavaDoc[2];
219         args[0] = request.getContextPath();
220         if (completeStatus) {
221             args[1] = sm.getString("statusServlet.complete");
222         } else {
223             args[1] = sm.getString("statusServlet.title");
224         }
225         // use StatusTransformer to output status
226
StatusTransformer.writeBody(writer,args,mode);
227
228         // Manager Section
229
args = new Object JavaDoc[9];
230         args[0] = sm.getString("htmlManagerServlet.manager");
231         args[1] = response.encodeURL(request.getContextPath() + "/html/list");
232         args[2] = sm.getString("htmlManagerServlet.list");
233         args[3] = response.encodeURL
234             (request.getContextPath() + "/" +
235              sm.getString("htmlManagerServlet.helpHtmlManagerFile"));
236         args[4] = sm.getString("htmlManagerServlet.helpHtmlManager");
237         args[5] = response.encodeURL
238             (request.getContextPath() + "/" +
239              sm.getString("htmlManagerServlet.helpManagerFile"));
240         args[6] = sm.getString("htmlManagerServlet.helpManager");
241         if (completeStatus) {
242             args[7] = response.encodeURL
243                 (request.getContextPath() + "/status");
244             args[8] = sm.getString("statusServlet.title");
245         } else {
246             args[7] = response.encodeURL
247                 (request.getContextPath() + "/status/all");
248             args[8] = sm.getString("statusServlet.complete");
249         }
250         // use StatusTransformer to output status
251
StatusTransformer.writeManager(writer,args,mode);
252
253         // Server Header Section
254
args = new Object JavaDoc[7];
255         args[0] = sm.getString("htmlManagerServlet.serverTitle");
256         args[1] = sm.getString("htmlManagerServlet.serverVersion");
257         args[2] = sm.getString("htmlManagerServlet.serverJVMVersion");
258         args[3] = sm.getString("htmlManagerServlet.serverJVMVendor");
259         args[4] = sm.getString("htmlManagerServlet.serverOSName");
260         args[5] = sm.getString("htmlManagerServlet.serverOSVersion");
261         args[6] = sm.getString("htmlManagerServlet.serverOSArch");
262         // use StatusTransformer to output status
263
StatusTransformer.writePageHeading(writer,args,mode);
264
265         // Server Row Section
266
args = new Object JavaDoc[6];
267         args[0] = ServerInfo.getServerInfo();
268         args[1] = System.getProperty("java.runtime.version");
269         args[2] = System.getProperty("java.vm.vendor");
270         args[3] = System.getProperty("os.name");
271         args[4] = System.getProperty("os.version");
272         args[5] = System.getProperty("os.arch");
273         // use StatusTransformer to output status
274
StatusTransformer.writeServerInfo(writer, args, mode);
275
276         try {
277
278             // Display operating system statistics using APR if available
279
StatusTransformer.writeOSState(writer,mode);
280
281             // Display virtual machine statistics
282
StatusTransformer.writeVMState(writer,mode);
283
284             Enumeration JavaDoc enumeration = threadPools.elements();
285             while (enumeration.hasMoreElements()) {
286                 ObjectName JavaDoc objectName = (ObjectName JavaDoc) enumeration.nextElement();
287                 String JavaDoc name = objectName.getKeyProperty("name");
288                 // use StatusTransformer to output status
289
StatusTransformer.writeConnectorState
290                     (writer, objectName,
291                      name, mBeanServer, globalRequestProcessors,
292                      requestProcessors, mode);
293             }
294
295             if ((request.getPathInfo() != null)
296                 && (request.getPathInfo().equals("/all"))) {
297                 // Note: Retrieving the full status is much slower
298
// use StatusTransformer to output status
299
StatusTransformer.writeDetailedState
300                     (writer, mBeanServer, mode);
301             }
302
303         } catch (Exception JavaDoc e) {
304             throw new ServletException JavaDoc(e);
305         }
306
307         // use StatusTransformer to output status
308
StatusTransformer.writeFooter(writer, mode);
309
310     }
311
312     // ------------------------------------------- NotificationListener Methods
313

314
315     public void handleNotification(Notification JavaDoc notification,
316                                    java.lang.Object JavaDoc handback) {
317
318         if (notification instanceof MBeanServerNotification JavaDoc) {
319             ObjectName JavaDoc objectName =
320                 ((MBeanServerNotification JavaDoc) notification).getMBeanName();
321             if (notification.getType().equals
322                 (MBeanServerNotification.REGISTRATION_NOTIFICATION)) {
323                 String JavaDoc type = objectName.getKeyProperty("type");
324                 if (type != null) {
325                     if (type.equals("ProtocolHandler")) {
326                         protocolHandlers.addElement(objectName);
327                     } else if (type.equals("ThreadPool")) {
328                         threadPools.addElement(objectName);
329                     } else if (type.equals("GlobalRequestProcessor")) {
330                         globalRequestProcessors.addElement(objectName);
331                     } else if (type.equals("RequestProcessor")) {
332                         requestProcessors.addElement(objectName);
333                     }
334                 }
335             } else if (notification.getType().equals
336                        (MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) {
337                 String JavaDoc type = objectName.getKeyProperty("type");
338                 if (type != null) {
339                     if (type.equals("ProtocolHandler")) {
340                         protocolHandlers.removeElement(objectName);
341                     } else if (type.equals("ThreadPool")) {
342                         threadPools.removeElement(objectName);
343                     } else if (type.equals("GlobalRequestProcessor")) {
344                         globalRequestProcessors.removeElement(objectName);
345                     } else if (type.equals("RequestProcessor")) {
346                         requestProcessors.removeElement(objectName);
347                     }
348                 }
349                 String JavaDoc j2eeType = objectName.getKeyProperty("j2eeType");
350                 if (j2eeType != null) {
351                     
352                 }
353             }
354         }
355
356     }
357
358
359 }
360
Popular Tags