KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > servlets > ResinStatusServlet


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.servlets;
31
32 import com.caucho.config.ConfigException;
33 import com.caucho.jmx.Jmx;
34 import com.caucho.log.Log;
35 import com.caucho.management.server.*;
36 import com.caucho.util.L10N;
37 import com.caucho.util.QDate;
38
39 import javax.management.MBeanServer JavaDoc;
40 import javax.management.ObjectName JavaDoc;
41 import javax.servlet.GenericServlet JavaDoc;
42 import javax.servlet.ServletException JavaDoc;
43 import javax.servlet.ServletRequest JavaDoc;
44 import javax.servlet.ServletResponse JavaDoc;
45 import javax.servlet.http.HttpServletRequest JavaDoc;
46 import javax.servlet.http.HttpServletResponse JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.io.PrintWriter JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.Collections JavaDoc;
51 import java.util.Comparator JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.Set JavaDoc;
54 import java.util.logging.Level JavaDoc;
55 import java.util.logging.Logger JavaDoc;
56
57 /**
58  * Displays some status information about the Resin server.
59  * The servlet must be explicitly enabled (using /servlet is forbidden),
60  * and it must have the init-param enable set to "read" or "write".
61  * (There will likely be a future additional requirement of satisfying
62  * a role.)
63  */

64 public class ResinStatusServlet extends GenericServlet JavaDoc {
65   static final protected Logger JavaDoc log = Log.open(ResinStatusServlet.class);
66   static final L10N L = new L10N(ResinStatusServlet.class);
67
68   private static final long SECOND = 1000L;
69   private static final long MINUTE = 60 * SECOND;
70   private static final long HOUR = 60 * MINUTE;
71   private static final long DAY = 24 * HOUR;
72
73   private String JavaDoc _enable;
74
75   private MBeanServer JavaDoc _mbeanServer;
76   private ResinMXBean _resin;
77   private ServerMXBean _server;
78   private ClusterMXBean _cluster;
79   private ProxyCacheMXBean _proxyCache;
80
81   /**
82    * Set to read or write.
83    */

84   public void setEnable(String JavaDoc enable)
85     throws ConfigException
86   {
87     if ("read".equals(enable) || "write".equals(enable))
88       _enable = enable;
89     else
90       throw new ConfigException(L.l("enable value '{0}' must either be read or write.",
91                                     enable));
92   }
93
94   /**
95    * Initialize the servlet with the server's sruns.
96    */

97   public void init()
98     throws ServletException JavaDoc
99   {
100     if (_enable == null)
101       throw new ServletException JavaDoc(L.l("ResinStatusServlet requires an explicit enable attribute."));
102
103     try {
104       //_mbeanServer = (MBeanServer) new InitialContext().lookup("java:comp/jmx/GlobalMBeanServer");
105

106       // _mbeanServer = Jmx.findMBeanServer();
107

108       // _mbeanServer = Jmx.getMBeanServer();
109
_mbeanServer =
110         Jmx.getGlobalMBeanServer();
111
112       //_resinServer = (ResinServerMBean) Jmx.find("resin:type=ResinServer");
113
//_servletServer = (ServletServerMBean) Jmx.find("resin:name=default,type=Server");
114

115       _resin = (ResinMXBean) Jmx.findGlobal("resin:type=Resin");
116       _server = (ServerMXBean) Jmx.findGlobal("resin:type=Server");
117       _cluster = (ClusterMXBean) Jmx.findGlobal("resin:type=Cluster");
118       _proxyCache = (ProxyCacheMXBean) Jmx.findGlobal("resin:type=ProxyCache");
119     } catch (Exception JavaDoc e) {
120       throw new ServletException JavaDoc(e);
121     }
122   }
123
124   /**
125    * Handle the request.
126    */

127   public void service(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
128     throws IOException JavaDoc, ServletException JavaDoc
129   {
130     try {
131       HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) request;
132       HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) response;
133
134       res.setContentType("text/html");
135
136       Thread JavaDoc thread = Thread.currentThread();
137       thread.setContextClassLoader(ClassLoader.getSystemClassLoader());
138
139       PrintWriter JavaDoc out = res.getWriter();
140
141       printHeader(out);
142
143       String JavaDoc hostName = req.getParameter("host");
144       String JavaDoc appName = req.getParameter("app");
145
146       printServerHeader(out);
147       printPorts(out);
148       printSrun(out);
149       /*
150     printJNDI(out, _server.getJndiContext());
151       */

152       printApplicationSummary(out, req.getRequestURI());
153       printFooter(out);
154     } catch (IOException JavaDoc e) {
155       throw e;
156     } catch (ServletException JavaDoc e) {
157       throw e;
158     } catch (Exception JavaDoc e) {
159       throw new ServletException JavaDoc(e);
160     }
161   }
162
163     /*
164   private void printApplication(PrintWriter out,
165                                 ApplicationAdmin app,
166                                 String pwd)
167     throws IOException, ServletException
168   {
169     ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
170
171     try {
172       Thread.currentThread().setContextClassLoader(app.getClassLoader());
173
174       printHeader(out);
175
176       printApplicationHeader(out, app, pwd);
177       printJNDI(out, app.getJndiContext());
178
179       printJMXServlets(out, app.getMBeanServer());
180
181       printFooter(out);
182     } finally {
183       Thread.currentThread().setContextClassLoader(oldLoader);
184     }
185   }
186     */

187
188   /**
189    * Prints generic server information.
190    */

191   public void printHeader(PrintWriter JavaDoc out)
192     throws IOException JavaDoc, ServletException JavaDoc
193   {
194   }
195
196   /**
197    * Prints server information.
198    */

199   public void printServerHeader(PrintWriter JavaDoc out)
200     throws Exception JavaDoc
201   {
202     out.println("<b>resin-status</b><br><br>");
203
204     String JavaDoc id = _server.getId();
205
206     out.println("<table border=\"0\">");
207     if (id != null)
208       out.println("<tr><td><b>Server:</b><td>" + id);
209
210     String JavaDoc configFile = _resin.getConfigFile();
211     if (configFile != null)
212       out.println("<tr><td><b>Config:</b><td>" + configFile);
213
214     long startTime = _server.getStartTime().getTime();
215
216     out.println("<tr><td><b>Server Start:</b><td> " +
217                 QDate.formatLocal(startTime));
218
219     long totalMemory = _server.getRuntimeMemory();
220     out.println("<tr><td><b>Total Memory:</b><td> " +
221                 (totalMemory / 1000000) + "." +
222                 (totalMemory / 100000) % 10 +
223                 (totalMemory / 10000) % 10 +
224                 "Meg");
225     long freeMemory = _server.getRuntimeMemoryFree();
226     out.println("<tr><td><b>Free Memory:</b><td> " +
227                 (freeMemory / 1000000) + "." +
228                 (freeMemory / 100000) % 10 +
229                 (freeMemory / 10000) % 10 +
230                 "Meg");
231     
232     long invocationHitCount = _server.getInvocationCacheHitCountTotal();
233     long invocationMissCount = _server.getInvocationCacheMissCountTotal();
234
235     long totalCount = invocationHitCount + invocationMissCount;
236     if (totalCount == 0)
237       totalCount = 1;
238
239     long hitRatio = (10000 * invocationHitCount) / totalCount;
240
241     out.print("<tr><td><b>Invocation Hit Ratio:</b><td> " +
242               (hitRatio / 100) + "." +
243               (hitRatio / 10) % 10 +
244               (hitRatio) % 10 + "%");
245     out.println(" (" + invocationHitCount + "/" + totalCount + ")");
246
247     if (_proxyCache != null) {
248       long proxyHitCount = _proxyCache.getHitCountTotal();
249       long proxyMissCount = _proxyCache.getMissCountTotal();
250
251       totalCount = proxyHitCount + proxyMissCount;
252       if (totalCount == 0)
253     totalCount = 1;
254
255       hitRatio = (10000 * proxyHitCount) / totalCount;
256
257       out.print("<tr><td><b>Proxy Cache Hit Ratio:</b><td> " +
258         (hitRatio / 100) + "." +
259         (hitRatio / 10) % 10 +
260         (hitRatio) % 10 + "%");
261       out.println(" (" + proxyHitCount + "/" + totalCount + ")");
262     }
263
264     out.println("</table>");
265
266     printThreadHeader(out);
267     printConnectionPools(out, "");
268   }
269
270   /**
271    * Prints thread information.
272    */

273   public void printThreadHeader(PrintWriter JavaDoc out)
274     throws Exception JavaDoc
275   {
276     out.println("<table border='3'>");
277
278     ThreadPoolMXBean threadPool = (ThreadPoolMXBean) Jmx.findGlobal("resin:type=ThreadPool");
279     out.println("<tr><th colspan='3'>Threads");
280     out.println(" <th colspan='3'>Config");
281     out.println("<tr><th>Active<th>Idle<th>Total");
282     out.println(" <th>thread-max<th>thread-idle-min");
283     out.println("<tr align='right'>");
284     out.println(" <td>" + threadPool.getThreadActiveCount());
285     out.println(" <td>" + threadPool.getThreadIdleCount());
286     out.println(" <td>" + threadPool.getThreadCount());
287
288     out.println(" <td>" + threadPool.getThreadMax());
289     out.println(" <td>" + threadPool.getThreadIdleMin());
290
291     out.println("</table>");
292   }
293
294   /**
295    * Prints application information.
296    */

297   /*
298   public void printApplicationHeader(PrintWriter out,
299                                      ApplicationAdmin app,
300                                      String pwd)
301     throws IOException, ServletException
302   {
303     HostAdmin host = app.getHostAdmin();
304
305     out.println("<b><a HREF=\"" + pwd + "\">resin-status</a> > ");
306     out.println("<a HREF=\"" + pwd +
307                 "?host=" + host.getName() + "\">" + host.getURL() + "</a> > ");
308     out.println(app.getContextPath() + "</b><br><br>");
309
310     String id = _server.getServerId();
311
312     out.println("<table border=\"0\">");
313     if (id != null)
314       out.println("<tr><td><b>Server:</b><td>" + id);
315     Path config = _server.getConfig();
316     if (config != null)
317       out.println("<tr><td><b>Config:</b><td>" + config.getNativePath());
318
319     out.println("<tr><td><b>Host:</b><td>" + app.getHostAdmin().getURL());
320     out.println("<tr><td><b>Web-App:</b><td>" + app.getContextPath());
321     out.println("<tr><td><b>App-Dir:</b><td>" + app.getAppDir().getNativePath());
322
323     long startTime = _server.getStartTime();
324     long restartTime = app.getStartTime();
325
326     out.println("<tr><td><b>Server Start:</b><td>" + QDate.formatLocal(startTime));
327     out.println("<tr><td><b>Web-App Start:</b><td> " + QDate.formatLocal(restartTime));
328     long totalMemory = Runtime.getRuntime().totalMemory();
329     out.println("<tr><td><b>Total Memory:</b><td> " +
330                 (totalMemory / 1000000) + "." +
331                 (totalMemory / 100000) % 10 +
332                 (totalMemory / 10000) % 10 +
333                 "Meg");
334     long freeMemory = Runtime.getRuntime().freeMemory();
335     out.println("<tr><td><b>Free Memory:</b><td> " +
336                 (freeMemory / 1000000) + "." +
337                 (freeMemory / 100000) % 10 +
338                 (freeMemory / 10000) % 10 +
339                 "Meg");
340
341     out.println("<tr><td><b>Sessions:</b><td>" +
342                 app.getActiveSessionCount());
343
344     out.println("</table>");
345   }
346   */

347
348   public void printPorts(PrintWriter JavaDoc out)
349     throws IOException JavaDoc, ServletException JavaDoc
350   {
351     try {
352       PortMXBean []portList = _server.getPorts();
353
354       if (portList.length > 0) {
355         out.println("<h3>TCP ports</h3>");
356         out.println("<table border='2'>");
357         out.println("<tr><th><th colspan='3'>Threads<th>&nbsp;");
358         out.println("<tr><th>Protocol:Port");
359         out.println(" <th>Active<th>Idle<th>Total");
360         out.println(" <th>Keepalive<th>Select");
361
362         for (int i = 0; i < portList.length; i++) {
363           PortMXBean port = portList[i];
364
365           if (port == null || ! "active".equals(port.getState()))
366             continue;
367
368           String JavaDoc host = port.getAddress();
369           if (host == null)
370             host = "*";
371
372           out.print("<tr><td>");
373           out.print(port.getProtocolName() + "://" + host + ":" + port.getPort());
374           out.println();
375
376           out.print(" <td>" + port.getThreadActiveCount());
377           out.print("<td>" + port.getThreadIdleCount());
378           out.print("<td>" + port.getThreadActiveCount());
379           out.print("<td>" + port.getThreadKeepaliveCount());
380           out.print("<td>" + port.getSelectKeepaliveCount());
381           out.println();
382
383           out.println();
384         }
385         out.println("</table>");
386       }
387     } catch (Exception JavaDoc e) {
388       throw new ServletException JavaDoc(e);
389     }
390   }
391
392   public void printSrun(PrintWriter JavaDoc out)
393     throws IOException JavaDoc, ServletException JavaDoc
394   {
395     try {
396       String JavaDoc[]clusterList = new String JavaDoc[0]; // _server.getClusterObjectNames();
397

398       for (int i = 0; i < clusterList.length; i++) {
399         ClusterMXBean cluster = (ClusterMXBean) Jmx.findGlobal(clusterList[i]);
400
401         if (cluster == null) {
402           out.println("<h3>Cluster " + clusterList[i] + " null</h3>");
403           continue;
404         }
405
406         ObjectName JavaDoc objectName = cluster.getObjectName();
407
408         String JavaDoc clusterName = objectName.getKeyProperty("name");
409
410         out.println("<h3>Cluster " + clusterName + "</h3>");
411         out.println("<table border='2'>");
412         out.println("<tr><th>Host");
413         out.println(" <th>Active");
414
415         ServerConnectorMXBean []servers = cluster.getServers();
416
417         for (int j = 0; j < servers.length; j++) {
418       ServerConnectorMXBean client = servers[j];
419       
420           String JavaDoc host = client.getAddress();
421           String JavaDoc port = String.valueOf(client.getPort());
422
423           out.println("<tr>");
424
425           boolean canConnect = client.ping();
426
427           if (canConnect)
428             out.print("<td bgcolor='#80ff80'>");
429           else
430             out.print("<td>");
431
432           out.print(host + ":" + port);
433
434           if (canConnect)
435             out.println(" (up)");
436           else
437             out.println(" (down)");
438
439           out.println("<td>" + client.getConnectionActiveCount());
440         }
441
442         out.println("</table>");
443       }
444     } catch (Exception JavaDoc e) {
445       throw new ServletException JavaDoc(e);
446     }
447   }
448
449   public void printConnectionPools(PrintWriter JavaDoc out, String JavaDoc context)
450     throws Exception JavaDoc
451   {
452     ObjectName JavaDoc pattern = new ObjectName JavaDoc("resin:*,type=ConnectionPool" + context);
453
454     Set JavaDoc<ObjectName JavaDoc> poolNames;
455     poolNames = _mbeanServer.queryNames(pattern, null);
456
457     if (poolNames.size() == 0)
458       return;
459
460     out.println("<h3>Connection Pools</h3>");
461
462     out.println("<table border='2'>");
463     out.println("<tr><th>&nbsp;<th colspan='3'>Connections<th colspan='2'>Config");
464     out.println("<tr><th>Name<th>Active<th>Idle<th>Total");
465     out.println(" <th>max-connections<th>idle-time");
466
467     Iterator JavaDoc<ObjectName JavaDoc> iter = poolNames.iterator();
468     while (iter.hasNext()) {
469       ObjectName JavaDoc name = iter.next();
470
471       ConnectionPoolMXBean pool = (ConnectionPoolMXBean) Jmx.findGlobal(name);
472
473       if (pool != null) {
474         out.println("<tr><td>" + pool.getName());
475         out.println(" <td>" + pool.getConnectionActiveCount());
476         out.println(" <td>" + pool.getConnectionIdleCount());
477         out.println(" <td>" + pool.getConnectionCount());
478
479         out.println(" <td>" + pool.getMaxConnections());
480         out.println(" <td>" + periodToString(pool.getMaxIdleTime()));
481       }
482     }
483
484     out.println("</table>");
485   }
486
487   private String JavaDoc periodToString(long time)
488   {
489     if (time == 0)
490       return "0s";
491     else if (time % DAY == 0)
492       return (time / DAY) + "d";
493     else if (time % HOUR == 0)
494       return (time / HOUR) + "h";
495     else if (time % MINUTE == 0)
496       return (time / MINUTE) + "min";
497     else if (time % SECOND == 0)
498       return (time / SECOND) + "s";
499     else
500       return time + "ms";
501   }
502
503   /*
504   public void printSrun(PrintWriter out)
505     throws IOException, ServletException
506   {
507     DistributionServer []srunList = _server.getDistributionServerList();
508
509     if (srunList == null || srunList.length == 0)
510       return;
511
512     out.println("<h3>Srun Servers</h3>");
513     out.println("<table border=2>");
514     out.println("<tr><th>Host</th><th>Active Count</th><th>live-time</th><th>dead-time</th><th>request-timeout</th>");
515
516     for (int i = 0; i < srunList.length; i++) {
517       DistributionServer srun = srunList[i];
518       out.print("<tr>");
519
520       boolean isLive = false;
521       try {
522         ReadWritePair pair = srun.open();
523         if (pair != null) {
524           isLive = true;
525           srun.free(pair);
526         }
527       } catch (Throwable e) {
528         dbg.log(e);
529       }
530
531       if (isLive) {
532         out.println("<td bgcolor=\"#66ff66\">" +
533                     (srun.getIndex() + 1) + ". " +
534                     srun.getHost() + ":" + srun.getPort() +
535                     (srun.isBackup() ? "*" : "") +
536                     " (ok)");
537       }
538       else {
539         out.println("<td bgcolor=\"#ff6666\">" +
540                     (srun.getIndex() + 1) + ". " +
541                     srun.getHost() + ":" + srun.getPort() +
542                     (srun.isBackup() ? "*" : "") +
543                     " (down)");
544       }
545       out.println("<td>" + srun.getActiveCount());
546       out.println("<td>" + srun.getLiveTime() / 1000);
547       out.println("<td>" + srun.getDeadTime() / 1000);
548       out.println("<td>" + srun.getTimeout() / 1000);
549     }
550     out.println("</table>");
551   }
552
553   public void printJNDI(PrintWriter out, Context ic)
554     throws IOException, ServletException
555   {
556     printDatabasePools(out, ic);
557     printEJBLocalHomes(out, ic);
558     printEJBRemoteHomes(out, ic);
559   }
560
561   public void printEJBLocalHomes(PrintWriter out, Context ic)
562     throws IOException, ServletException
563   {
564     try {
565       Context cmpCxt = (Context) ic.lookup("java:comp/env/cmp");
566
567       if (cmpCxt == null)
568         return;
569
570       NamingEnumeration list = cmpCxt.list("");
571
572       ArrayList cmpNames = new ArrayList();
573       while (list.hasMoreElements()) {
574         NameClassPair pair = (NameClassPair) list.nextElement();
575
576         cmpNames.add(pair.getName());
577       }
578
579       if (cmpNames.size() == 0)
580         return;
581
582       out.println("<h3>EJB Local Home</h3>");
583
584       out.println("<table border=\"2\">");
585       out.println("<tr><th>Name<th>Home Stub Class");
586
587       Collections.sort(cmpNames);
588
589       for (int i = 0; i < cmpNames.size(); i++) {
590         String name = (String) cmpNames.get(i);
591
592         Object value = cmpCxt.lookup(name);
593         if (! (value instanceof EJBLocalHome))
594           continue;
595
596         EJBLocalHome home = (EJBLocalHome) value;
597         out.print("<tr><td>cmp/" + name);
598         Class homeStub = home.getClass();
599         Class []interfaces = home.getClass().getInterfaces();
600         for (int j = 0; j < interfaces.length; j++) {
601           if (EJBLocalHome.class.isAssignableFrom(interfaces[j])) {
602             homeStub = interfaces[j];
603             break;
604           }
605         }
606         out.print("<td>" + homeStub.getName());
607       }
608       out.println("</table>");
609     } catch (Exception e) {
610       dbg.log(e);
611     }
612   }
613
614   public void printEJBRemoteHomes(PrintWriter out, Context ic)
615     throws IOException, ServletException
616   {
617     try {
618       Context ejbCxt = (Context) ic.lookup("java:comp/env/ejb");
619
620       if (ejbCxt == null)
621         return;
622
623       NamingEnumeration list = ejbCxt.list("");
624
625       ArrayList ejbNames = new ArrayList();
626       while (list.hasMoreElements()) {
627         NameClassPair pair = (NameClassPair) list.nextElement();
628
629         ejbNames.add(pair.getName());
630       }
631
632       if (ejbNames.size() == 0)
633         return;
634
635       out.println("<h3>EJB Home</h3>");
636
637       out.println("<table border=\"2\">");
638       out.println("<tr><th>Name<th>Home Stub Class");
639
640       Collections.sort(ejbNames);
641
642       for (int i = 0; i < ejbNames.size(); i++) {
643         String name = (String) ejbNames.get(i);
644
645         Object value = ejbCxt.lookup(name);
646         if (! (value instanceof EJBHome))
647           continue;
648
649         EJBHome home = (EJBHome) value;
650         out.print("<tr><td>ejb/" + name);
651         Class homeStub = home.getClass();
652         Class []interfaces = home.getClass().getInterfaces();
653         for (int j = 0; j < interfaces.length; j++) {
654           if (EJBHome.class.isAssignableFrom(interfaces[j])) {
655             homeStub = interfaces[j];
656             break;
657           }
658         }
659         out.print("<td>" + homeStub.getName());
660       }
661       out.println("</table>");
662     } catch (Exception e) {
663       dbg.log(e);
664     }
665   }
666
667   public void printJMXServlets(PrintWriter out, MXBeanServer server)
668     throws IOException, ServletException
669   {
670     try {
671       ObjectName queryName = new ObjectName("*:j2eeType=Servlet,*");
672
673       Set servlets = server.queryNames(queryName, null);
674
675       if (servlets.size() == 0)
676         return;
677
678       out.println("<h3>Servlets</h3>");
679
680       Iterator iter = servlets.iterator();
681       while (iter.hasNext()) {
682         ObjectName servletName = (ObjectName) iter.next();
683
684         String name = servletName.getKeyProperty("name");
685         MXBeanInfo mbeanInfo = server.getMXBeanInfo(servletName);
686         MXBeanAttributeInfo []attrs = mbeanInfo.getAttributes();
687
688         out.println("<table border=\"2\">");
689         out.print("<tr><th>Name</th>");
690
691         for (int i = 0; i < attrs.length; i++)
692           out.print("<th>" + attrs[i].getName() + "</th>");
693         out.println("</tr>");
694
695         out.print("<tr><td>" + name + "</td>");
696         for (int i = 0; i < attrs.length; i++) {
697           Object value = server.getAttribute(servletName, attrs[i].getName());
698
699           out.print("<td>" + value + "</td>");
700         }
701
702         out.println("</table>");
703       }
704     } catch (Exception e) {
705       dbg.log(e);
706     }
707   }
708   */

709
710   public void printApplicationSummary(PrintWriter JavaDoc out, String JavaDoc pwd)
711     throws Exception JavaDoc
712   {
713     out.println("<h3>Hosts and Applications</h3>");
714
715     out.println("<table border=\"2\">");
716     out.println("<tr><th>Host<th>Web-App<th>State<th>Sessions");
717
718     ObjectName JavaDoc hostPattern = new ObjectName JavaDoc("resin:*,type=Host");
719
720     Set JavaDoc<ObjectName JavaDoc> names = _mbeanServer.queryNames(hostPattern, null);
721     Iterator JavaDoc<ObjectName JavaDoc> iter = names.iterator();
722
723     ArrayList JavaDoc<HostMXBean> hosts = new ArrayList JavaDoc<HostMXBean>();
724
725     while (iter.hasNext()) {
726       ObjectName JavaDoc name = iter.next();
727
728       // the Host with name=current is a duplicate
729
if ("current".equals(name.getKeyProperty("name")))
730         continue;
731
732       HostMXBean host = (HostMXBean) Jmx.findGlobal(name);
733
734       if (host != null) {
735         hosts.add(host);
736       }
737     }
738
739     Collections.sort(hosts, new HostCompare());
740
741     for (int i = 0; i < hosts.size(); i++) {
742       HostMXBean host = hosts.get(i);
743
744       out.println("<tr><td><b>" + host.getURL() + "</b>");
745
746       // thread.setContextClassLoader(hostLoader);
747

748       String JavaDoc hostName = host.getHostName();
749       if (hostName.equals(""))
750         hostName = "default";
751
752       ObjectName JavaDoc appPattern = new ObjectName JavaDoc("resin:*,Host=" + hostName + ",type=WebApp");
753
754       names = _mbeanServer.queryNames(appPattern, null);
755       iter = names.iterator();
756
757       ArrayList JavaDoc<WebAppMXBean> apps = new ArrayList JavaDoc<WebAppMXBean>();
758
759       while (iter.hasNext()) {
760         ObjectName JavaDoc name = iter.next();
761
762         try {
763           WebAppMXBean app = (WebAppMXBean) Jmx.findGlobal(name);
764
765           if (app != null)
766             apps.add(app);
767         } catch (Exception JavaDoc e) {
768           log.log(Level.WARNING, e.toString());
769           out.println("<tr><td>" + name + "<td>" + e.toString());
770         }
771       }
772
773       Collections.sort(apps, new AppCompare());
774
775       for (int j = 0; j < apps.size(); j++) {
776         WebAppMXBean app = apps.get(j);
777     SessionManagerMXBean session = app.getSessionManager();
778
779         String JavaDoc contextPath = app.getContextPath();
780
781         if (contextPath.equals(""))
782           contextPath = "/";
783
784         out.print("<tr><td><td>");
785         out.print("<a HREF=\"" + pwd + "?host=" + host.getHostName() +
786                   "&app=" + app.getContextPath() + "\">");
787         out.print(contextPath);
788         out.print("</a>");
789
790         String JavaDoc state = app.getState();
791         if (state.equals("active"))
792           out.print("<td bgcolor='#80ff80'>" + app.getState());
793         else
794           out.print("<td>" + app.getState());
795         out.print("<td>" + session.getSessionActiveCount());
796       }
797     }
798
799     out.println("</table>");
800   }
801
802   public void printVirtualHosts(PrintWriter JavaDoc out)
803     throws IOException JavaDoc, ServletException JavaDoc
804   {
805   }
806
807   /**
808    * Prints footer information.
809    */

810   public void printFooter(PrintWriter JavaDoc out)
811     throws IOException JavaDoc, ServletException JavaDoc
812   {
813     /*
814     if (_server.isTesting())
815       out.println("<br><em>Resin test</em>");
816     else
817     */

818       out.println("<br><em>" + com.caucho.Version.FULL_VERSION + "</em>");
819   }
820
821   static class HostCompare implements Comparator JavaDoc<HostMXBean> {
822     public int compare(HostMXBean a, HostMXBean b)
823     {
824       String JavaDoc urlA = a.getURL();
825       String JavaDoc urlB = b.getURL();
826
827       if (urlA == urlB)
828         return 0;
829       else if (urlA == null)
830         return -1;
831       else if (urlB == null)
832         return 1;
833       else
834         return urlA.compareTo(urlB);
835     }
836   }
837
838   static class AppCompare implements Comparator JavaDoc<WebAppMXBean> {
839     public int compare(WebAppMXBean a, WebAppMXBean b)
840     {
841       String JavaDoc cpA = a.getContextPath();
842       String JavaDoc cpB = b.getContextPath();
843
844       return cpA.compareTo(cpB);
845     }
846   }
847 }
848
Popular Tags