KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.IOException JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 import javax.management.MBeanServer JavaDoc;
32 import javax.management.ObjectInstance JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 import org.apache.catalina.util.RequestUtil;
38
39 /**
40  * This is a refactoring of the servlet to externalize
41  * the output into a simple class. Although we could
42  * use XSLT, that is unnecessarily complex.
43  *
44  * @author Peter Lin
45  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
46  */

47
48 public class StatusTransformer {
49
50
51     // --------------------------------------------------------- Public Methods
52

53
54     public static void setContentType(HttpServletResponse JavaDoc response,
55                                       int mode) {
56         if (mode == 0){
57             response.setContentType("text/html;charset="+Constants.CHARSET);
58         } else if (mode == 1){
59             response.setContentType("text/xml;charset="+Constants.CHARSET);
60         }
61     }
62
63
64     /**
65      * Process a GET request for the specified resource.
66      *
67      * @param request The servlet request we are processing
68      * @param response The servlet response we are creating
69      *
70      * @exception IOException if an input/output error occurs
71      * @exception ServletException if a servlet-specified error occurs
72      */

73     public static void writeHeader(PrintWriter JavaDoc writer, int mode) {
74         if (mode == 0){
75             // HTML Header Section
76
writer.print(Constants.HTML_HEADER_SECTION);
77         } else if (mode == 1){
78             writer.write(Constants.XML_DECLARATION);
79             writer.write
80                 (Constants.XML_STYLE);
81             writer.write("<status>");
82         }
83     }
84
85
86     /**
87      * Write the header body. XML output doesn't bother
88      * to output this stuff, since it's just title.
89      *
90      * @param writer The output writer
91      * @param args What to write
92      * @param mode 0 means write
93      */

94     public static void writeBody(PrintWriter JavaDoc writer, Object JavaDoc[] args, int mode) {
95         if (mode == 0){
96             writer.print(MessageFormat.format
97                          (Constants.BODY_HEADER_SECTION, args));
98         }
99     }
100
101
102     /**
103      * Write the manager webapp information.
104      *
105      * @param writer The output writer
106      * @param args What to write
107      * @param mode 0 means write
108      */

109     public static void writeManager(PrintWriter JavaDoc writer, Object JavaDoc[] args,
110                                     int mode) {
111         if (mode == 0){
112             writer.print(MessageFormat.format(Constants.MANAGER_SECTION, args));
113         }
114     }
115
116
117     public static void writePageHeading(PrintWriter JavaDoc writer, Object JavaDoc[] args,
118                                         int mode) {
119         if (mode == 0){
120             writer.print(MessageFormat.format
121                          (Constants.SERVER_HEADER_SECTION, args));
122         }
123     }
124
125
126     public static void writeServerInfo(PrintWriter JavaDoc writer, Object JavaDoc[] args,
127                                        int mode){
128         if (mode == 0){
129             writer.print(MessageFormat.format(Constants.SERVER_ROW_SECTION, args));
130         }
131     }
132
133
134     /**
135      *
136      */

137     public static void writeFooter(PrintWriter JavaDoc writer, int mode) {
138         if (mode == 0){
139             // HTML Tail Section
140
writer.print(Constants.HTML_TAIL_SECTION);
141         } else if (mode == 1){
142             writer.write("</status>");
143         }
144     }
145
146
147     /**
148      * Write the OS state. Mode 0 will generate HTML.
149      * Mode 1 will generate XML.
150      */

151     public static void writeOSState(PrintWriter JavaDoc writer, int mode) {
152         long[] result = new long[16];
153         boolean ok = false;
154         try {
155             String JavaDoc methodName = "info";
156             Class JavaDoc paramTypes[] = new Class JavaDoc[1];
157             paramTypes[0] = result.getClass();
158             Object JavaDoc paramValues[] = new Object JavaDoc[1];
159             paramValues[0] = result;
160             Method JavaDoc method = Class.forName("org.apache.tomcat.jni.OS")
161                 .getMethod(methodName, paramTypes);
162             method.invoke(null, paramValues);
163             ok = true;
164         } catch (Throwable JavaDoc t) {
165             // Ignore
166
}
167         
168         if (ok) {
169             if (mode == 0){
170                 writer.print("<h1>OS</h1>");
171
172                 writer.print("<p>");
173                 writer.print(" Physical memory: ");
174                 writer.print(formatSize(new Long JavaDoc(result[0]), true));
175                 writer.print(" Available memory: ");
176                 writer.print(formatSize(new Long JavaDoc(result[1]), true));
177                 writer.print(" Total page file: ");
178                 writer.print(formatSize(new Long JavaDoc(result[2]), true));
179                 writer.print(" Free page file: ");
180                 writer.print(formatSize(new Long JavaDoc(result[3]), true));
181                 writer.print(" Memory load: ");
182                 writer.print(new Long JavaDoc(result[6]));
183                 writer.print("<br>");
184                 writer.print(" Process kernel time: ");
185                 writer.print(formatTime(new Long JavaDoc(result[11] / 1000), true));
186                 writer.print(" Process user time: ");
187                 writer.print(formatTime(new Long JavaDoc(result[12] / 1000), true));
188                 writer.print("</p>");
189             } else if (mode == 1){
190             }
191         }
192         
193     }
194     
195     
196     /**
197      * Write the VM state. Mode 0 will generate HTML.
198      * Mode 1 will generate XML.
199      */

200     public static void writeVMState(PrintWriter JavaDoc writer, int mode)
201         throws Exception JavaDoc {
202
203         if (mode == 0){
204             writer.print("<h1>JVM</h1>");
205
206             writer.print("<p>");
207             writer.print(" Free memory: ");
208             writer.print(formatSize
209                          (new Long JavaDoc(Runtime.getRuntime().freeMemory()), true));
210             writer.print(" Total memory: ");
211             writer.print(formatSize
212                          (new Long JavaDoc(Runtime.getRuntime().totalMemory()), true));
213             writer.print(" Max memory: ");
214             writer.print(formatSize
215                          (new Long JavaDoc(Runtime.getRuntime().maxMemory()), true));
216             writer.print("</p>");
217         } else if (mode == 1){
218             writer.write("<jvm>");
219
220             writer.write("<memory");
221             writer.write(" free='" + Runtime.getRuntime().freeMemory() + "'");
222             writer.write(" total='" + Runtime.getRuntime().totalMemory() + "'");
223             writer.write(" max='" + Runtime.getRuntime().maxMemory() + "'/>");
224
225             writer.write("</jvm>");
226         }
227
228     }
229
230
231     /**
232      * Write connector state.
233      */

234     public static void writeConnectorState(PrintWriter JavaDoc writer,
235                                            ObjectName JavaDoc tpName, String JavaDoc name,
236                                            MBeanServer JavaDoc mBeanServer,
237                                            Vector JavaDoc globalRequestProcessors,
238                                            Vector JavaDoc requestProcessors,
239                                            int mode)
240         throws Exception JavaDoc {
241
242         if (mode == 0) {
243             writer.print("<h1>");
244             writer.print(name);
245             writer.print("</h1>");
246
247             writer.print("<p>");
248             writer.print(" Max threads: ");
249             writer.print(mBeanServer.getAttribute(tpName, "maxThreads"));
250             writer.print(" Current thread count: ");
251             writer.print(mBeanServer.getAttribute(tpName, "currentThreadCount"));
252             writer.print(" Current thread busy: ");
253             writer.print(mBeanServer.getAttribute(tpName, "currentThreadsBusy"));
254             try {
255                 Object JavaDoc value = mBeanServer.getAttribute(tpName, "keepAliveCount");
256                 writer.print(" Keeped alive sockets count: ");
257                 writer.print(value);
258             } catch (Exception JavaDoc e) {
259                 // Ignore
260
}
261             
262             writer.print("<br>");
263
264             ObjectName JavaDoc grpName = null;
265
266             Enumeration JavaDoc enumeration = globalRequestProcessors.elements();
267             while (enumeration.hasMoreElements()) {
268                 ObjectName JavaDoc objectName = (ObjectName JavaDoc) enumeration.nextElement();
269                 if (name.equals(objectName.getKeyProperty("name"))) {
270                     grpName = objectName;
271                 }
272             }
273
274             if (grpName == null) {
275                 return;
276             }
277
278             writer.print(" Max processing time: ");
279             writer.print(formatTime(mBeanServer.getAttribute
280                                     (grpName, "maxTime"), false));
281             writer.print(" Processing time: ");
282             writer.print(formatTime(mBeanServer.getAttribute
283                                     (grpName, "processingTime"), true));
284             writer.print(" Request count: ");
285             writer.print(mBeanServer.getAttribute(grpName, "requestCount"));
286             writer.print(" Error count: ");
287             writer.print(mBeanServer.getAttribute(grpName, "errorCount"));
288             writer.print(" Bytes received: ");
289             writer.print(formatSize(mBeanServer.getAttribute
290                                     (grpName, "bytesReceived"), true));
291             writer.print(" Bytes sent: ");
292             writer.print(formatSize(mBeanServer.getAttribute
293                                     (grpName, "bytesSent"), true));
294             writer.print("</p>");
295
296             writer.print("<table border=\"0\"><tr><th>Stage</th><th>Time</th><th>B Sent</th><th>B Recv</th><th>Client</th><th>VHost</th><th>Request</th></tr>");
297
298             enumeration = requestProcessors.elements();
299             while (enumeration.hasMoreElements()) {
300                 ObjectName JavaDoc objectName = (ObjectName JavaDoc) enumeration.nextElement();
301                 if (name.equals(objectName.getKeyProperty("worker"))) {
302                     writer.print("<tr>");
303                     writeProcessorState(writer, objectName, mBeanServer, mode);
304                     writer.print("</tr>");
305                 }
306             }
307
308             writer.print("</table>");
309
310             writer.print("<p>");
311             writer.print("P: Parse and prepare request S: Service F: Finishing R: Ready K: Keepalive");
312             writer.print("</p>");
313         } else if (mode == 1){
314             writer.write("<connector name='" + name + "'>");
315
316             writer.write("<threadInfo ");
317             writer.write(" maxThreads=\"" + mBeanServer.getAttribute(tpName, "maxThreads") + "\"");
318             writer.write(" currentThreadCount=\"" + mBeanServer.getAttribute(tpName, "currentThreadCount") + "\"");
319             writer.write(" currentThreadsBusy=\"" + mBeanServer.getAttribute(tpName, "currentThreadsBusy") + "\"");
320             writer.write(" />");
321
322             ObjectName JavaDoc grpName = null;
323
324             Enumeration JavaDoc enumeration = globalRequestProcessors.elements();
325             while (enumeration.hasMoreElements()) {
326                 ObjectName JavaDoc objectName = (ObjectName JavaDoc) enumeration.nextElement();
327                 if (name.equals(objectName.getKeyProperty("name"))) {
328                     grpName = objectName;
329                 }
330             }
331
332             if (grpName != null) {
333
334                 writer.write("<requestInfo ");
335                 writer.write(" maxTime=\"" + mBeanServer.getAttribute(grpName, "maxTime") + "\"");
336                 writer.write(" processingTime=\"" + mBeanServer.getAttribute(grpName, "processingTime") + "\"");
337                 writer.write(" requestCount=\"" + mBeanServer.getAttribute(grpName, "requestCount") + "\"");
338                 writer.write(" errorCount=\"" + mBeanServer.getAttribute(grpName, "errorCount") + "\"");
339                 writer.write(" bytesReceived=\"" + mBeanServer.getAttribute(grpName, "bytesReceived") + "\"");
340                 writer.write(" bytesSent=\"" + mBeanServer.getAttribute(grpName, "bytesSent") + "\"");
341                 writer.write(" />");
342
343                 writer.write("<workers>");
344                 enumeration = requestProcessors.elements();
345                 while (enumeration.hasMoreElements()) {
346                     ObjectName JavaDoc objectName = (ObjectName JavaDoc) enumeration.nextElement();
347                     if (name.equals(objectName.getKeyProperty("worker"))) {
348                         writeProcessorState(writer, objectName, mBeanServer, mode);
349                     }
350                 }
351                 writer.write("</workers>");
352             }
353
354             writer.write("</connector>");
355         }
356
357     }
358
359
360     /**
361      * Write processor state.
362      */

363     protected static void writeProcessorState(PrintWriter JavaDoc writer,
364                                               ObjectName JavaDoc pName,
365                                               MBeanServer JavaDoc mBeanServer,
366                                               int mode)
367         throws Exception JavaDoc {
368
369         Integer JavaDoc stageValue =
370             (Integer JavaDoc) mBeanServer.getAttribute(pName, "stage");
371         int stage = stageValue.intValue();
372         boolean fullStatus = true;
373         boolean showRequest = true;
374         String JavaDoc stageStr = null;
375
376         switch (stage) {
377
378         case (1/*org.apache.coyote.Constants.STAGE_PARSE*/):
379             stageStr = "P";
380             fullStatus = false;
381             break;
382         case (2/*org.apache.coyote.Constants.STAGE_PREPARE*/):
383             stageStr = "P";
384             fullStatus = false;
385             break;
386         case (3/*org.apache.coyote.Constants.STAGE_SERVICE*/):
387             stageStr = "S";
388             break;
389         case (4/*org.apache.coyote.Constants.STAGE_ENDINPUT*/):
390             stageStr = "F";
391             break;
392         case (5/*org.apache.coyote.Constants.STAGE_ENDOUTPUT*/):
393             stageStr = "F";
394             break;
395         case (7/*org.apache.coyote.Constants.STAGE_ENDED*/):
396             stageStr = "R";
397             fullStatus = false;
398             break;
399         case (6/*org.apache.coyote.Constants.STAGE_KEEPALIVE*/):
400             stageStr = "K";
401             fullStatus = true;
402             showRequest = false;
403             break;
404         case (0/*org.apache.coyote.Constants.STAGE_NEW*/):
405             stageStr = "R";
406             fullStatus = false;
407             break;
408         default:
409             // Unknown stage
410
stageStr = "?";
411             fullStatus = false;
412
413         }
414
415         if (mode == 0) {
416             writer.write("<td><strong>");
417             writer.write(stageStr);
418             writer.write("</strong></td>");
419
420             if (fullStatus) {
421                 writer.write("<td>");
422                 writer.print(formatTime(mBeanServer.getAttribute
423                                         (pName, "requestProcessingTime"), false));
424                 writer.write("</td>");
425                 writer.write("<td>");
426                 if (showRequest) {
427                     writer.print(formatSize(mBeanServer.getAttribute
428                                             (pName, "requestBytesSent"), false));
429                 } else {
430                     writer.write("?");
431                 }
432                 writer.write("</td>");
433                 writer.write("<td>");
434                 if (showRequest) {
435                     writer.print(formatSize(mBeanServer.getAttribute
436                                             (pName, "requestBytesReceived"),
437                                             false));
438                 } else {
439                     writer.write("?");
440                 }
441                 writer.write("</td>");
442                 writer.write("<td>");
443                 writer.print(filter(mBeanServer.getAttribute
444                                     (pName, "remoteAddr")));
445                 writer.write("</td>");
446                 writer.write("<td nowrap>");
447                 writer.write(filter(mBeanServer.getAttribute
448                                     (pName, "virtualHost")));
449                 writer.write("</td>");
450                 writer.write("<td nowrap>");
451                 if (showRequest) {
452                     writer.write(filter(mBeanServer.getAttribute
453                                         (pName, "method")));
454                     writer.write(" ");
455                     writer.write(filter(mBeanServer.getAttribute
456                                         (pName, "currentUri")));
457                     String JavaDoc queryString = (String JavaDoc) mBeanServer.getAttribute
458                         (pName, "currentQueryString");
459                     if ((queryString != null) && (!queryString.equals(""))) {
460                         writer.write("?");
461                         writer.print(RequestUtil.filter(queryString));
462                     }
463                     writer.write(" ");
464                     writer.write(filter(mBeanServer.getAttribute
465                                         (pName, "protocol")));
466                 } else {
467                     writer.write("?");
468                 }
469                 writer.write("</td>");
470             } else {
471                 writer.write("<td>?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td>");
472             }
473         } else if (mode == 1){
474             writer.write("<worker ");
475             writer.write(" stage=\"" + stageStr + "\"");
476
477             if (fullStatus) {
478                 writer.write(" requestProcessingTime=\""
479                              + mBeanServer.getAttribute
480                              (pName, "requestProcessingTime") + "\"");
481                 writer.write(" requestBytesSent=\"");
482                 if (showRequest) {
483                     writer.write("" + mBeanServer.getAttribute
484                                  (pName, "requestBytesSent"));
485                 } else {
486                     writer.write("0");
487                 }
488                 writer.write("\"");
489                 writer.write(" requestBytesReceived=\"");
490                 if (showRequest) {
491                     writer.write("" + mBeanServer.getAttribute
492                                  (pName, "requestBytesReceived"));
493                 } else {
494                     writer.write("0");
495                 }
496                 writer.write("\"");
497                 writer.write(" remoteAddr=\""
498                              + filter(mBeanServer.getAttribute
499                                       (pName, "remoteAddr")) + "\"");
500                 writer.write(" virtualHost=\""
501                              + filter(mBeanServer.getAttribute
502                                       (pName, "virtualHost")) + "\"");
503
504                 if (showRequest) {
505                     writer.write(" method=\""
506                                  + filter(mBeanServer.getAttribute
507                                           (pName, "method")) + "\"");
508                     writer.write(" currentUri=\""
509                                  + filter(mBeanServer.getAttribute
510                                           (pName, "currentUri")) + "\"");
511
512                     String JavaDoc queryString = (String JavaDoc) mBeanServer.getAttribute
513                         (pName, "currentQueryString");
514                     if ((queryString != null) && (!queryString.equals(""))) {
515                         writer.write(" currentQueryString=\""
516                                      + RequestUtil.filter(queryString) + "\"");
517                     } else {
518                         writer.write(" currentQueryString=\"&#63;\"");
519                     }
520                     writer.write(" protocol=\""
521                                  + filter(mBeanServer.getAttribute
522                                           (pName, "protocol")) + "\"");
523                 } else {
524                     writer.write(" method=\"&#63;\"");
525                     writer.write(" currentUri=\"&#63;\"");
526                     writer.write(" currentQueryString=\"&#63;\"");
527                     writer.write(" protocol=\"&#63;\"");
528                 }
529             } else {
530                 writer.write(" requestProcessingTime=\"0\"");
531                 writer.write(" requestBytesSent=\"0\"");
532                 writer.write(" requestBytesRecieved=\"0\"");
533                 writer.write(" remoteAddr=\"&#63;\"");
534                 writer.write(" virtualHost=\"&#63;\"");
535                 writer.write(" method=\"&#63;\"");
536                 writer.write(" currentUri=\"&#63;\"");
537                 writer.write(" currentQueryString=\"&#63;\"");
538                 writer.write(" protocol=\"&#63;\"");
539             }
540             writer.write(" />");
541         }
542
543     }
544
545
546     /**
547      * Write applications state.
548      */

549     public static void writeDetailedState(PrintWriter JavaDoc writer,
550                                           MBeanServer JavaDoc mBeanServer, int mode)
551         throws Exception JavaDoc {
552
553         if (mode == 0){
554             ObjectName JavaDoc queryHosts = new ObjectName JavaDoc("*:j2eeType=WebModule,*");
555             Set JavaDoc hostsON = mBeanServer.queryNames(queryHosts, null);
556
557             // Navigation menu
558
writer.print("<h1>");
559             writer.print("Application list");
560             writer.print("</h1>");
561
562             writer.print("<p>");
563             int count = 0;
564             Iterator JavaDoc iterator = hostsON.iterator();
565             while (iterator.hasNext()) {
566                 ObjectName JavaDoc contextON = (ObjectName JavaDoc) iterator.next();
567                 String JavaDoc webModuleName = contextON.getKeyProperty("name");
568                 if (webModuleName.startsWith("//")) {
569                     webModuleName = webModuleName.substring(2);
570                 }
571                 int slash = webModuleName.indexOf("/");
572                 if (slash == -1) {
573                     count++;
574                     continue;
575                 }
576
577                 writer.print("<a HREF=\"#" + (count++) + ".0\">");
578                 writer.print(webModuleName);
579                 writer.print("</a>");
580                 if (iterator.hasNext()) {
581                     writer.print("<br>");
582                 }
583
584             }
585             writer.print("</p>");
586
587             // Webapp list
588
count = 0;
589             iterator = hostsON.iterator();
590             while (iterator.hasNext()) {
591                 ObjectName JavaDoc contextON = (ObjectName JavaDoc) iterator.next();
592                 writer.print("<a class=\"A.name\" name=\""
593                              + (count++) + ".0\">");
594                 writeContext(writer, contextON, mBeanServer, mode);
595             }
596
597         } else if (mode == 1){
598             // for now we don't write out the Detailed state in XML
599
}
600
601     }
602
603
604     /**
605      * Write context state.
606      */

607     protected static void writeContext(PrintWriter JavaDoc writer,
608                                        ObjectName JavaDoc objectName,
609                                        MBeanServer JavaDoc mBeanServer, int mode)
610         throws Exception JavaDoc {
611
612         if (mode == 0){
613             String JavaDoc webModuleName = objectName.getKeyProperty("name");
614             String JavaDoc name = webModuleName;
615             if (name == null) {
616                 return;
617             }
618             
619             String JavaDoc hostName = null;
620             String JavaDoc contextName = null;
621             if (name.startsWith("//")) {
622                 name = name.substring(2);
623             }
624             int slash = name.indexOf("/");
625             if (slash != -1) {
626                 hostName = name.substring(0, slash);
627                 contextName = name.substring(slash);
628             } else {
629                 return;
630             }
631
632             ObjectName JavaDoc queryManager = new ObjectName JavaDoc
633                 (objectName.getDomain() + ":type=Manager,path=" + contextName
634                  + ",host=" + hostName + ",*");
635             Set JavaDoc managersON = mBeanServer.queryNames(queryManager, null);
636             ObjectName JavaDoc managerON = null;
637             Iterator JavaDoc iterator2 = managersON.iterator();
638             while (iterator2.hasNext()) {
639                 managerON = (ObjectName JavaDoc) iterator2.next();
640             }
641
642             ObjectName JavaDoc queryJspMonitor = new ObjectName JavaDoc
643                 (objectName.getDomain() + ":type=JspMonitor,WebModule=" +
644                  webModuleName + ",*");
645             Set JavaDoc jspMonitorONs = mBeanServer.queryNames(queryJspMonitor, null);
646
647             // Special case for the root context
648
if (contextName.equals("/")) {
649                 contextName = "";
650             }
651
652             writer.print("<h1>");
653             writer.print(name);
654             writer.print("</h1>");
655             writer.print("</a>");
656
657             writer.print("<p>");
658             Object JavaDoc startTime = mBeanServer.getAttribute(objectName,
659                                                         "startTime");
660             writer.print(" Start time: " +
661                          new Date JavaDoc(((Long JavaDoc) startTime).longValue()));
662             writer.print(" Startup time: ");
663             writer.print(formatTime(mBeanServer.getAttribute
664                                     (objectName, "startupTime"), false));
665             writer.print(" TLD scan time: ");
666             writer.print(formatTime(mBeanServer.getAttribute
667                                     (objectName, "tldScanTime"), false));
668             if (managerON != null) {
669                 writeManager(writer, managerON, mBeanServer, mode);
670             }
671             if (jspMonitorONs != null) {
672                 writeJspMonitor(writer, jspMonitorONs, mBeanServer, mode);
673             }
674             writer.print("</p>");
675
676             String JavaDoc onStr = objectName.getDomain()
677                 + ":j2eeType=Servlet,WebModule=" + webModuleName + ",*";
678             ObjectName JavaDoc servletObjectName = new ObjectName JavaDoc(onStr);
679             Set JavaDoc set = mBeanServer.queryMBeans(servletObjectName, null);
680             Iterator JavaDoc iterator = set.iterator();
681             while (iterator.hasNext()) {
682                 ObjectInstance JavaDoc oi = (ObjectInstance JavaDoc) iterator.next();
683                 writeWrapper(writer, oi.getObjectName(), mBeanServer, mode);
684             }
685
686         } else if (mode == 1){
687             // for now we don't write out the context in XML
688
}
689
690     }
691
692
693     /**
694      * Write detailed information about a manager.
695      */

696     public static void writeManager(PrintWriter JavaDoc writer, ObjectName JavaDoc objectName,
697                                     MBeanServer JavaDoc mBeanServer, int mode)
698         throws Exception JavaDoc {
699
700         if (mode == 0) {
701             writer.print("<br>");
702             writer.print(" Active sessions: ");
703             writer.print(mBeanServer.getAttribute
704                          (objectName, "activeSessions"));
705             writer.print(" Session count: ");
706             writer.print(mBeanServer.getAttribute
707                          (objectName, "sessionCounter"));
708             writer.print(" Max active sessions: ");
709             writer.print(mBeanServer.getAttribute(objectName, "maxActive"));
710             writer.print(" Rejected session creations: ");
711             writer.print(mBeanServer.getAttribute
712                          (objectName, "rejectedSessions"));
713             writer.print(" Expired sessions: ");
714             writer.print(mBeanServer.getAttribute
715                          (objectName, "expiredSessions"));
716             writer.print(" Longest session alive time: ");
717             writer.print(formatSeconds(mBeanServer.getAttribute(
718                                                     objectName,
719                                                     "sessionMaxAliveTime")));
720             writer.print(" Average session alive time: ");
721             writer.print(formatSeconds(mBeanServer.getAttribute(
722                                                     objectName,
723                                                     "sessionAverageAliveTime")));
724             writer.print(" Processing time: ");
725             writer.print(formatTime(mBeanServer.getAttribute
726                                     (objectName, "processingTime"), false));
727         } else if (mode == 1) {
728             // for now we don't write out the wrapper details
729
}
730
731     }
732
733
734     /**
735      * Write JSP monitoring information.
736      */

737     public static void writeJspMonitor(PrintWriter JavaDoc writer,
738                                        Set JavaDoc jspMonitorONs,
739                                        MBeanServer JavaDoc mBeanServer,
740                                        int mode)
741             throws Exception JavaDoc {
742
743         int jspCount = 0;
744         int jspReloadCount = 0;
745
746         Iterator JavaDoc iter = jspMonitorONs.iterator();
747         while (iter.hasNext()) {
748             ObjectName JavaDoc jspMonitorON = (ObjectName JavaDoc) iter.next();
749             Object JavaDoc obj = mBeanServer.getAttribute(jspMonitorON, "jspCount");
750             jspCount += ((Integer JavaDoc) obj).intValue();
751             obj = mBeanServer.getAttribute(jspMonitorON, "jspReloadCount");
752             jspReloadCount += ((Integer JavaDoc) obj).intValue();
753         }
754
755         if (mode == 0) {
756             writer.print("<br>");
757             writer.print(" JSPs loaded: ");
758             writer.print(jspCount);
759             writer.print(" JSPs reloaded: ");
760             writer.print(jspReloadCount);
761         } else if (mode == 1) {
762             // for now we don't write out anything
763
}
764     }
765
766
767     /**
768      * Write detailed information about a wrapper.
769      */

770     public static void writeWrapper(PrintWriter JavaDoc writer, ObjectName JavaDoc objectName,
771                                     MBeanServer JavaDoc mBeanServer, int mode)
772         throws Exception JavaDoc {
773
774         if (mode == 0) {
775             String JavaDoc servletName = objectName.getKeyProperty("name");
776             
777             String JavaDoc[] mappings = (String JavaDoc[])
778                 mBeanServer.invoke(objectName, "findMappings", null, null);
779             
780             writer.print("<h2>");
781             writer.print(servletName);
782             if ((mappings != null) && (mappings.length > 0)) {
783                 writer.print(" [ ");
784                 for (int i = 0; i < mappings.length; i++) {
785                     writer.print(mappings[i]);
786                     if (i < mappings.length - 1) {
787                         writer.print(" , ");
788                     }
789                 }
790                 writer.print(" ] ");
791             }
792             writer.print("</h2>");
793             
794             writer.print("<p>");
795             writer.print(" Processing time: ");
796             writer.print(formatTime(mBeanServer.getAttribute
797                                     (objectName, "processingTime"), true));
798             writer.print(" Max time: ");
799             writer.print(formatTime(mBeanServer.getAttribute
800                                     (objectName, "maxTime"), false));
801             writer.print(" Request count: ");
802             writer.print(mBeanServer.getAttribute(objectName, "requestCount"));
803             writer.print(" Error count: ");
804             writer.print(mBeanServer.getAttribute(objectName, "errorCount"));
805             writer.print(" Load time: ");
806             writer.print(formatTime(mBeanServer.getAttribute
807                                     (objectName, "loadTime"), false));
808             writer.print(" Classloading time: ");
809             writer.print(formatTime(mBeanServer.getAttribute
810                                     (objectName, "classLoadTime"), false));
811             writer.print("</p>");
812         } else if (mode == 1){
813             // for now we don't write out the wrapper details
814
}
815
816     }
817
818
819     /**
820      * Filter the specified message string for characters that are sensitive
821      * in HTML. This avoids potential attacks caused by including JavaScript
822      * codes in the request URL that is often reported in error messages.
823      *
824      * @param obj The message string to be filtered
825      */

826     public static String JavaDoc filter(Object JavaDoc obj) {
827
828         if (obj == null)
829             return ("?");
830         String JavaDoc message = obj.toString();
831
832         char content[] = new char[message.length()];
833         message.getChars(0, message.length(), content, 0);
834         StringBuffer JavaDoc result = new StringBuffer JavaDoc(content.length + 50);
835         for (int i = 0; i < content.length; i++) {
836             switch (content[i]) {
837             case '<':
838                 result.append("&lt;");
839                 break;
840             case '>':
841                 result.append("&gt;");
842                 break;
843             case '&':
844                 result.append("&amp;");
845                 break;
846             case '"':
847                 result.append("&quot;");
848                 break;
849             default:
850                 result.append(content[i]);
851             }
852         }
853         return (result.toString());
854
855     }
856
857
858     /**
859      * Display the given size in bytes, either as KB or MB.
860      *
861      * @param mb true to display megabytes, false for kilobytes
862      */

863     public static String JavaDoc formatSize(Object JavaDoc obj, boolean mb) {
864
865         long bytes = -1L;
866
867         if (obj instanceof Long JavaDoc) {
868             bytes = ((Long JavaDoc) obj).longValue();
869         } else if (obj instanceof Integer JavaDoc) {
870             bytes = ((Integer JavaDoc) obj).intValue();
871         }
872
873         if (mb) {
874             long mbytes = bytes / (1024 * 1024);
875             long rest =
876                 ((bytes - (mbytes * (1024 * 1024))) * 100) / (1024 * 1024);
877             return (mbytes + "." + ((rest < 10) ? "0" : "") + rest + " MB");
878         } else {
879             return ((bytes / 1024) + " KB");
880         }
881
882     }
883
884
885     /**
886      * Display the given time in ms, either as ms or s.
887      *
888      * @param seconds true to display seconds, false for milliseconds
889      */

890     public static String JavaDoc formatTime(Object JavaDoc obj, boolean seconds) {
891
892         long time = -1L;
893
894         if (obj instanceof Long JavaDoc) {
895             time = ((Long JavaDoc) obj).longValue();
896         } else if (obj instanceof Integer JavaDoc) {
897             time = ((Integer JavaDoc) obj).intValue();
898         }
899
900         if (seconds) {
901             return ((((float) time ) / 1000) + " s");
902         } else {
903             return (time + " ms");
904         }
905     }
906
907
908     /**
909      * Formats the given time (given in seconds) as a string.
910      *
911      * @param obj Time object to be formatted as string
912      *
913      * @return String formatted time
914      */

915     public static String JavaDoc formatSeconds(Object JavaDoc obj) {
916
917         long time = -1L;
918
919         if (obj instanceof Long JavaDoc) {
920             time = ((Long JavaDoc) obj).longValue();
921         } else if (obj instanceof Integer JavaDoc) {
922             time = ((Integer JavaDoc) obj).intValue();
923         }
924
925         return (time + " s");
926     }
927
928 }
929
Popular Tags