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