View Javadoc

1   /*
2    * Copyright 1999,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  
17  
18  package org.apache.catalina.manager;
19  
20  
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  import java.util.Enumeration;
24  import java.util.Iterator;
25  import java.util.Set;
26  import java.util.Vector;
27  
28  import javax.management.MBeanServer;
29  import javax.management.MBeanServerNotification;
30  import javax.management.Notification;
31  import javax.management.NotificationListener;
32  import javax.management.ObjectInstance;
33  import javax.management.ObjectName;
34  
35  import javax.servlet.ServletException;
36  import javax.servlet.http.HttpServlet;
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  
40  import org.apache.commons.modeler.Registry;
41  
42  import org.apache.catalina.util.ServerInfo;
43  import org.apache.catalina.util.StringManager;
44  
45  import org.talika.tms.Constants;
46  
47  /**
48   * This servlet will display a complete status of the HTTP/1.1 connector.
49   *
50   * @author Remy Maucherat
51   * @author Jose M. Palomar
52   * @version $Revision: 179 $ $Date: 2004-07-31 11:47:06 +0200 (sáb 31 de jul de 2004) $
53   */
54  
55  public class StatusManagerXServlet
56      extends HttpServlet implements NotificationListener {
57  
58  
59      // ----------------------------------------------------- Instance Variables
60  
61  
62      /**
63       * The debugging detail level for this servlet.
64       */
65      private int debug = 0;
66  
67  
68      /**
69       * MBean server.
70       */
71      protected MBeanServer mBeanServer = null;
72  
73  
74      /**
75       * Vector of protocol handlers object names.
76       */
77      protected Vector protocolHandlers = new Vector();
78  
79  
80      /**
81       * Vector of thread pools object names.
82       */
83      protected Vector threadPools = new Vector();
84  
85  
86      /**
87       * Vector of request processors object names.
88       */
89      protected Vector requestProcessors = new Vector();
90  
91  
92      /**
93       * Vector of global request processors object names.
94       */
95      protected Vector globalRequestProcessors = new Vector();
96  
97  
98      /**
99       * The string manager for this package.
100      */
101     protected static StringManager sm =
102         StringManager.getManager(Constants.Package);
103 
104 
105     // --------------------------------------------------------- Public Methods
106 
107 
108     /**
109      * Initialize this servlet.
110      */
111     public void init() throws ServletException {
112 
113         // Retrieve the MBean server
114         mBeanServer = Registry.getServer();
115 
116         // Set our properties from the initialization parameters
117         String value = null;
118         try {
119             value = getServletConfig().getInitParameter("debug");
120             debug = Integer.parseInt(value);
121         } catch (Throwable t) {
122             ;
123         }
124 
125         try {
126 
127             // Query protocol handlers
128             String onStr = "*:type=ProtocolHandler,*";
129             ObjectName objectName = new ObjectName(onStr);
130             Set set = mBeanServer.queryMBeans(objectName, null);
131             Iterator iterator = set.iterator();
132             while (iterator.hasNext()) {
133                 ObjectInstance oi = (ObjectInstance) iterator.next();
134                 protocolHandlers.addElement(oi.getObjectName());
135             }
136 
137             // Query Thread Pools
138             onStr = "*:type=ThreadPool,*";
139             objectName = new ObjectName(onStr);
140             set = mBeanServer.queryMBeans(objectName, null);
141             iterator = set.iterator();
142             while (iterator.hasNext()) {
143                 ObjectInstance oi = (ObjectInstance) iterator.next();
144                 threadPools.addElement(oi.getObjectName());
145             }
146 
147             // Query Global Request Processors
148             onStr = "*:type=GlobalRequestProcessor,*";
149             objectName = new ObjectName(onStr);
150             set = mBeanServer.queryMBeans(objectName, null);
151             iterator = set.iterator();
152             while (iterator.hasNext()) {
153                 ObjectInstance oi = (ObjectInstance) iterator.next();
154                 globalRequestProcessors.addElement(oi.getObjectName());
155             }
156 
157             // Query Request Processors
158             onStr = "*:type=RequestProcessor,*";
159             objectName = new ObjectName(onStr);
160             set = mBeanServer.queryMBeans(objectName, null);
161             iterator = set.iterator();
162             while (iterator.hasNext()) {
163                 ObjectInstance oi = (ObjectInstance) iterator.next();
164                 requestProcessors.addElement(oi.getObjectName());
165             }
166 
167             // Register with MBean server
168             onStr = "JMImplementation:type=MBeanServerDelegate";
169             objectName = new ObjectName(onStr);
170             mBeanServer.addNotificationListener(objectName, this, null, null);
171 
172         } catch (Exception e) {
173             e.printStackTrace();
174         }
175 
176     }
177 
178 
179     /**
180      * Finalize this servlet.
181      */
182     public void destroy() {
183 
184         ;       // No actions necessary
185 
186     }
187 
188 
189     /**
190      * Process a GET request for the specified resource.
191      *
192      * @param request The servlet request we are processing
193      * @param response The servlet response we are creating
194      *
195      * @exception IOException if an input/output error occurs
196      * @exception ServletException if a servlet-specified error occurs
197      */
198     public void doGet(HttpServletRequest request,
199                       HttpServletResponse response)
200         throws IOException, ServletException {
201 
202         // mode is flag for HTML or XML output
203         int mode = 0;
204         // if ?XML=true, set the mode to XML
205         if (request.getParameter("XML") != null 
206             && request.getParameter("XML").equals("true")) {
207             mode = 1;
208         }
209         StatusTransformer.setContentType(response, mode);
210 
211         PrintWriter writer = response.getWriter();
212 
213         boolean completeStatus = false;
214         if ((request.getPathInfo() != null) 
215             && (request.getPathInfo().equals("/all"))) {
216             completeStatus = true;
217         }
218         // use StatusTransformer to output status
219         StatusTransformer.writeHeader(writer,mode);
220 
221         // Body Header Section
222         Object[] args = new Object[2];
223         args[0] = request.getContextPath();
224         if (completeStatus) {
225             args[1] = sm.getString("statusServlet.complete");
226         } else {
227             args[1] = sm.getString("statusServlet.title");
228         }
229         // use StatusTransformer to output status
230         StatusTransformer.writeBody(writer,args,mode);
231 
232         // Manager Section
233         args = new Object[9];
234         args[0] = sm.getString("htmlManagerServlet.manager");
235         args[1] = response.encodeURL(request.getContextPath() + "/html/vhosts");
236         args[2] = sm.getString("htmlManagerServlet.vhosts");
237         args[3] = response.encodeURL
238             (request.getContextPath() + "/" +
239              sm.getString("htmlManagerServlet.helpHtmlManagerFile"));
240         args[4] = sm.getString("htmlManagerServlet.helpHtmlManager");
241         args[5] = response.encodeURL
242             (request.getContextPath() + "/" +
243              sm.getString("htmlManagerServlet.helpManagerFile"));
244         args[6] = sm.getString("htmlManagerServlet.helpManager");
245         if (completeStatus) {
246             args[7] = response.encodeURL
247                 (request.getContextPath() + "/status");
248             args[8] = sm.getString("statusServlet.title");
249         } else {
250             args[7] = response.encodeURL
251                 (request.getContextPath() + "/status/all");
252             args[8] = sm.getString("statusServlet.complete");
253         }
254         // use StatusTransformer to output status
255         StatusTransformer.writeManager(writer,args,mode);
256 
257         // Server Header Section
258         args = new Object[7];
259         args[0] = sm.getString("htmlManagerServlet.serverTitle");
260         args[1] = sm.getString("htmlManagerServlet.serverVersion");
261         args[2] = sm.getString("htmlManagerServlet.serverJVMVersion");
262         args[3] = sm.getString("htmlManagerServlet.serverJVMVendor");
263         args[4] = sm.getString("htmlManagerServlet.serverOSName");
264         args[5] = sm.getString("htmlManagerServlet.serverOSVersion");
265         args[6] = sm.getString("htmlManagerServlet.serverOSArch");
266         // use StatusTransformer to output status
267         StatusTransformer.writePageHeading(writer,args,mode);
268 
269         // Server Row Section
270         args = new Object[6];
271         args[0] = ServerInfo.getServerInfo();
272         args[1] = System.getProperty("java.runtime.version");
273         args[2] = System.getProperty("java.vm.vendor");
274         args[3] = System.getProperty("os.name");
275         args[4] = System.getProperty("os.version");
276         args[5] = System.getProperty("os.arch");
277         // use StatusTransformer to output status
278         StatusTransformer.writeServerInfo(writer, args, mode);
279 
280         try {
281 
282             // Display virtual machine statistics
283             StatusTransformer.writeVMState(writer,mode);
284 
285             Enumeration enum = threadPools.elements();
286             while (enum.hasMoreElements()) {
287                 ObjectName objectName = (ObjectName) enum.nextElement();
288                 String name = objectName.getKeyProperty("name");
289                 // use StatusTransformer to output status
290                 StatusTransformer.writeConnectorState
291                     (writer, objectName,
292                      name, mBeanServer, globalRequestProcessors,
293                      requestProcessors, mode);
294             }
295 
296             if ((request.getPathInfo() != null) 
297                 && (request.getPathInfo().equals("/all"))) {
298                 // Note: Retrieving the full status is much slower
299                 // use StatusTransformer to output status
300                 StatusTransformer.writeDetailedState
301                     (writer, mBeanServer, mode);
302             }
303 
304         } catch (Exception e) {
305             throw new ServletException(e);
306         }
307 
308         // use StatusTransformer to output status
309         StatusTransformer.writeFooter(writer, mode);
310 
311     }
312 
313     // ------------------------------------------- NotificationListener Methods
314 
315 
316     public void handleNotification(Notification notification,
317                                    java.lang.Object handback) {
318 
319         if (notification instanceof MBeanServerNotification) {
320             ObjectName objectName = 
321                 ((MBeanServerNotification) notification).getMBeanName();
322             if (notification.getType().equals
323                 (MBeanServerNotification.REGISTRATION_NOTIFICATION)) {
324                 String type = objectName.getKeyProperty("type");
325                 if (type != null) {
326                     if (type.equals("ProtocolHandler")) {
327                         protocolHandlers.addElement(objectName);
328                     } else if (type.equals("ThreadPool")) {
329                         threadPools.addElement(objectName);
330                     } else if (type.equals("GlobalRequestProcessor")) {
331                         globalRequestProcessors.addElement(objectName);
332                     } else if (type.equals("RequestProcessor")) {
333                         requestProcessors.addElement(objectName);
334                     }
335                 }
336             } else if (notification.getType().equals
337                        (MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) {
338                 String type = objectName.getKeyProperty("type");
339                 if (type != null) {
340                     if (type.equals("ProtocolHandler")) {
341                         protocolHandlers.removeElement(objectName);
342                     } else if (type.equals("ThreadPool")) {
343                         threadPools.removeElement(objectName);
344                     } else if (type.equals("GlobalRequestProcessor")) {
345                         globalRequestProcessors.removeElement(objectName);
346                     } else if (type.equals("RequestProcessor")) {
347                         requestProcessors.removeElement(objectName);
348                     }
349                 }
350                 String j2eeType = objectName.getKeyProperty("j2eeType");
351                 if (j2eeType != null) {
352                     
353                 }
354             }
355         }
356 
357     }
358 
359 
360 }