001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.manager;
019:
020: import java.io.IOException;
021: import java.io.PrintWriter;
022: import java.util.Enumeration;
023: import java.util.Iterator;
024: import java.util.Set;
025: import java.util.Vector;
026:
027: import javax.management.MBeanServer;
028: import javax.management.MBeanServerNotification;
029: import javax.management.Notification;
030: import javax.management.NotificationListener;
031: import javax.management.ObjectInstance;
032: import javax.management.ObjectName;
033: import javax.servlet.ServletException;
034: import javax.servlet.http.HttpServlet;
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletResponse;
037:
038: import org.apache.catalina.util.ServerInfo;
039: import org.apache.catalina.util.StringManager;
040: import org.apache.tomcat.util.modeler.Registry;
041:
042: /**
043: * This servlet will display a complete status of the HTTP/1.1 connector.
044: *
045: * @author Remy Maucherat
046: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
047: */
048:
049: public class StatusManagerServlet extends HttpServlet implements
050: NotificationListener {
051:
052: // ----------------------------------------------------- Instance Variables
053:
054: /**
055: * The debugging detail level for this servlet.
056: */
057: private int debug = 0;
058:
059: /**
060: * MBean server.
061: */
062: protected MBeanServer mBeanServer = null;
063:
064: /**
065: * Vector of protocol handlers object names.
066: */
067: protected Vector protocolHandlers = new Vector();
068:
069: /**
070: * Vector of thread pools object names.
071: */
072: protected Vector threadPools = new Vector();
073:
074: /**
075: * Vector of request processors object names.
076: */
077: protected Vector requestProcessors = new Vector();
078:
079: /**
080: * Vector of global request processors object names.
081: */
082: protected Vector globalRequestProcessors = new Vector();
083:
084: /**
085: * The string manager for this package.
086: */
087: protected static StringManager sm = StringManager
088: .getManager(Constants.Package);
089:
090: // --------------------------------------------------------- Public Methods
091:
092: /**
093: * Initialize this servlet.
094: */
095: public void init() throws ServletException {
096:
097: // Retrieve the MBean server
098: mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
099:
100: // Set our properties from the initialization parameters
101: String value = null;
102: try {
103: value = getServletConfig().getInitParameter("debug");
104: debug = Integer.parseInt(value);
105: } catch (Throwable t) {
106: ;
107: }
108:
109: try {
110:
111: // Query protocol handlers
112: String onStr = "*:type=ProtocolHandler,*";
113: ObjectName objectName = new ObjectName(onStr);
114: Set set = mBeanServer.queryMBeans(objectName, null);
115: Iterator iterator = set.iterator();
116: while (iterator.hasNext()) {
117: ObjectInstance oi = (ObjectInstance) iterator.next();
118: protocolHandlers.addElement(oi.getObjectName());
119: }
120:
121: // Query Thread Pools
122: onStr = "*:type=ThreadPool,*";
123: objectName = new ObjectName(onStr);
124: set = mBeanServer.queryMBeans(objectName, null);
125: iterator = set.iterator();
126: while (iterator.hasNext()) {
127: ObjectInstance oi = (ObjectInstance) iterator.next();
128: threadPools.addElement(oi.getObjectName());
129: }
130:
131: // Query Global Request Processors
132: onStr = "*:type=GlobalRequestProcessor,*";
133: objectName = new ObjectName(onStr);
134: set = mBeanServer.queryMBeans(objectName, null);
135: iterator = set.iterator();
136: while (iterator.hasNext()) {
137: ObjectInstance oi = (ObjectInstance) iterator.next();
138: globalRequestProcessors.addElement(oi.getObjectName());
139: }
140:
141: // Query Request Processors
142: onStr = "*:type=RequestProcessor,*";
143: objectName = new ObjectName(onStr);
144: set = mBeanServer.queryMBeans(objectName, null);
145: iterator = set.iterator();
146: while (iterator.hasNext()) {
147: ObjectInstance oi = (ObjectInstance) iterator.next();
148: requestProcessors.addElement(oi.getObjectName());
149: }
150:
151: // Register with MBean server
152: onStr = "JMImplementation:type=MBeanServerDelegate";
153: objectName = new ObjectName(onStr);
154: mBeanServer.addNotificationListener(objectName, this , null,
155: null);
156:
157: } catch (Exception e) {
158: e.printStackTrace();
159: }
160:
161: }
162:
163: /**
164: * Finalize this servlet.
165: */
166: public void destroy() {
167:
168: ; // No actions necessary
169:
170: }
171:
172: /**
173: * Process a GET request for the specified resource.
174: *
175: * @param request The servlet request we are processing
176: * @param response The servlet response we are creating
177: *
178: * @exception IOException if an input/output error occurs
179: * @exception ServletException if a servlet-specified error occurs
180: */
181: public void doGet(HttpServletRequest request,
182: HttpServletResponse response) throws IOException,
183: ServletException {
184:
185: // mode is flag for HTML or XML output
186: int mode = 0;
187: // if ?XML=true, set the mode to XML
188: if (request.getParameter("XML") != null
189: && request.getParameter("XML").equals("true")) {
190: mode = 1;
191: }
192: StatusTransformer.setContentType(response, mode);
193:
194: PrintWriter writer = response.getWriter();
195:
196: boolean completeStatus = false;
197: if ((request.getPathInfo() != null)
198: && (request.getPathInfo().equals("/all"))) {
199: completeStatus = true;
200: }
201: // use StatusTransformer to output status
202: StatusTransformer.writeHeader(writer, mode);
203:
204: // Body Header Section
205: Object[] args = new Object[2];
206: args[0] = request.getContextPath();
207: if (completeStatus) {
208: args[1] = sm.getString("statusServlet.complete");
209: } else {
210: args[1] = sm.getString("statusServlet.title");
211: }
212: // use StatusTransformer to output status
213: StatusTransformer.writeBody(writer, args, mode);
214:
215: // Manager Section
216: args = new Object[9];
217: args[0] = sm.getString("htmlManagerServlet.manager");
218: args[1] = response.encodeURL(request.getContextPath()
219: + "/html/list");
220: args[2] = sm.getString("htmlManagerServlet.list");
221: args[3] = response
222: .encodeURL(request.getContextPath()
223: + "/"
224: + sm
225: .getString("htmlManagerServlet.helpHtmlManagerFile"));
226: args[4] = sm.getString("htmlManagerServlet.helpHtmlManager");
227: args[5] = response.encodeURL(request.getContextPath() + "/"
228: + sm.getString("htmlManagerServlet.helpManagerFile"));
229: args[6] = sm.getString("htmlManagerServlet.helpManager");
230: if (completeStatus) {
231: args[7] = response.encodeURL(request.getContextPath()
232: + "/status");
233: args[8] = sm.getString("statusServlet.title");
234: } else {
235: args[7] = response.encodeURL(request.getContextPath()
236: + "/status/all");
237: args[8] = sm.getString("statusServlet.complete");
238: }
239: // use StatusTransformer to output status
240: StatusTransformer.writeManager(writer, args, mode);
241:
242: // Server Header Section
243: args = new Object[7];
244: args[0] = sm.getString("htmlManagerServlet.serverTitle");
245: args[1] = sm.getString("htmlManagerServlet.serverVersion");
246: args[2] = sm.getString("htmlManagerServlet.serverJVMVersion");
247: args[3] = sm.getString("htmlManagerServlet.serverJVMVendor");
248: args[4] = sm.getString("htmlManagerServlet.serverOSName");
249: args[5] = sm.getString("htmlManagerServlet.serverOSVersion");
250: args[6] = sm.getString("htmlManagerServlet.serverOSArch");
251: // use StatusTransformer to output status
252: StatusTransformer.writePageHeading(writer, args, mode);
253:
254: // Server Row Section
255: args = new Object[6];
256: args[0] = ServerInfo.getServerInfo();
257: args[1] = System.getProperty("java.runtime.version");
258: args[2] = System.getProperty("java.vm.vendor");
259: args[3] = System.getProperty("os.name");
260: args[4] = System.getProperty("os.version");
261: args[5] = System.getProperty("os.arch");
262: // use StatusTransformer to output status
263: StatusTransformer.writeServerInfo(writer, args, mode);
264:
265: try {
266:
267: // Display operating system statistics using APR if available
268: StatusTransformer.writeOSState(writer, mode);
269:
270: // Display virtual machine statistics
271: StatusTransformer.writeVMState(writer, mode);
272:
273: Enumeration enumeration = threadPools.elements();
274: while (enumeration.hasMoreElements()) {
275: ObjectName objectName = (ObjectName) enumeration
276: .nextElement();
277: String name = objectName.getKeyProperty("name");
278: // use StatusTransformer to output status
279: StatusTransformer.writeConnectorState(writer,
280: objectName, name, mBeanServer,
281: globalRequestProcessors, requestProcessors,
282: mode);
283: }
284:
285: if ((request.getPathInfo() != null)
286: && (request.getPathInfo().equals("/all"))) {
287: // Note: Retrieving the full status is much slower
288: // use StatusTransformer to output status
289: StatusTransformer.writeDetailedState(writer,
290: mBeanServer, mode);
291: }
292:
293: } catch (Exception e) {
294: throw new ServletException(e);
295: }
296:
297: // use StatusTransformer to output status
298: StatusTransformer.writeFooter(writer, mode);
299:
300: }
301:
302: // ------------------------------------------- NotificationListener Methods
303:
304: public void handleNotification(Notification notification,
305: java.lang.Object handback) {
306:
307: if (notification instanceof MBeanServerNotification) {
308: ObjectName objectName = ((MBeanServerNotification) notification)
309: .getMBeanName();
310: if (notification.getType().equals(
311: MBeanServerNotification.REGISTRATION_NOTIFICATION)) {
312: String type = objectName.getKeyProperty("type");
313: if (type != null) {
314: if (type.equals("ProtocolHandler")) {
315: protocolHandlers.addElement(objectName);
316: } else if (type.equals("ThreadPool")) {
317: threadPools.addElement(objectName);
318: } else if (type.equals("GlobalRequestProcessor")) {
319: globalRequestProcessors.addElement(objectName);
320: } else if (type.equals("RequestProcessor")) {
321: requestProcessors.addElement(objectName);
322: }
323: }
324: } else if (notification
325: .getType()
326: .equals(
327: MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) {
328: String type = objectName.getKeyProperty("type");
329: if (type != null) {
330: if (type.equals("ProtocolHandler")) {
331: protocolHandlers.removeElement(objectName);
332: } else if (type.equals("ThreadPool")) {
333: threadPools.removeElement(objectName);
334: } else if (type.equals("GlobalRequestProcessor")) {
335: globalRequestProcessors
336: .removeElement(objectName);
337: } else if (type.equals("RequestProcessor")) {
338: requestProcessors.removeElement(objectName);
339: }
340: }
341: String j2eeType = objectName.getKeyProperty("j2eeType");
342: if (j2eeType != null) {
343:
344: }
345: }
346: }
347:
348: }
349:
350: }
|