View Javadoc
1   /*
2    * $Source$
3    * $Revision$
4    *
5    * Copyright (C) 2001 Myles Chippendale
6    *
7    * Part of Melati (http://melati.org), a framework for the rapid
8    * development of clean, maintainable web applications.
9    *
10   * Melati is free software; Permission is granted to copy, distribute
11   * and/or modify this software under the terms either:
12   *
13   * a) the GNU General Public License as published by the Free Software
14   *    Foundation; either version 2 of the License, or (at your option)
15   *    any later version,
16   *
17   *    or
18   *
19   * b) any version of the Melati Software License, as published
20   *    at http://melati.org
21   *
22   * You should have received a copy of the GNU General Public License and
23   * the Melati Software License along with this program;
24   * if not, write to the Free Software Foundation, Inc.,
25   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26   * GNU General Public License and visit http://melati.org to obtain the
27   * Melati Software License.
28   *
29   * Feel free to contact the Developers of Melati (http://melati.org),
30   * if you would like to work out a different arrangement than the options
31   * outlined here.  It is our intention to allow Melati to be used by as
32   * wide an audience as possible.
33   *
34   * This program is distributed in the hope that it will be useful,
35   * but WITHOUT ANY WARRANTY; without even the implied warranty of
36   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   * GNU General Public License for more details.
38   *
39   * Contact details for copyright holder:
40   *
41   *     Mylesc Chippendale <mylesc At paneris.org>
42   *     http://paneris.org/
43   *     29 Stanley Road, Oxford, OX4 1QY, UK
44   */
45  
46  package org.melati.test;
47  
48  import java.io.IOException;
49  import java.text.NumberFormat;
50  import java.util.Date;
51  import java.util.Enumeration;
52  
53  import javax.servlet.ServletException;
54  
55  import org.melati.Melati;
56  import org.melati.poem.Database;
57  import org.melati.poem.PoemThread;
58  import org.melati.poem.SessionToken;
59  import org.melati.poem.transaction.Transaction;
60  import org.melati.servlet.ConfigServlet;
61  import org.melati.util.MelatiWriter;
62  
63  /**
64   * Displays information about the status of this JVM and the databases
65   * running in it. Well, with JServ it's for this servlet zone.
66   *
67   * It shows us information about any Poem sessions running and
68   * each transaction (think 'connection to') in a database.
69   */
70  
71  public class SessionAnalysisServlet extends ConfigServlet {
72    private static final long serialVersionUID = 1L;
73    private MelatiWriter output;
74  
75    protected void doConfiguredRequest(Melati melati)
76        throws ServletException, IOException {
77  
78      melati.getResponse().setContentType("text/html");
79      output = melati.getWriter();
80      Date now = new Date();
81      println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD " 
82                   + "HTML 4.01 Transitional//EN\">\n");
83      println("<html>\n");
84      println("<head>\n");
85      println("<title>Transaction Analysis</title>\n");
86      String repeat = melati.getRequest().getParameter("repeat");
87      if (repeat != null)
88        println("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"" 
89                      + repeat + "; URL="
90                      + melati.getRequest().getRequestURI() + "?repeat="
91                      + repeat + "\">");
92      println("  <link rel='stylesheet' title='Default' href='" + 
93                   melati.getConfig().getStaticURL() + "/admin.css' \n" +
94                   "type='text/css' media='screen'>\n");
95      println("</head>");
96      println("<body>");
97      println("<h1>Transactions Analysis</h1>");
98      println("<p>Run at " + now + "</p>\n");
99      println("<p>JVM Free memory: " +
100    NumberFormat.getInstance().format(Runtime.getRuntime().freeMemory())
101                   + "</p>\n"
102                   + "<p>JVM Total memory: " +
103    NumberFormat.getInstance().format(Runtime.getRuntime().totalMemory())
104                   + "</p>\n"
105                   + "<form action=''>Reload every "  
106                   + "<input name='repeat' size='5' value='"
107                   + repeat + "'> seconds <input type=submit></form>\n");
108 
109     
110     println("<h2>Poem sessions</h2>\n");
111 
112     Enumeration<SessionToken> e = PoemThread.openSessions().elements();
113 
114     int totalSessions = 0;
115     while(e.hasMoreElements()) {
116       totalSessions++;
117       SessionToken token = e.nextElement();
118       println("<table border='1' cellspacing='0' cellpadding='1'>");
119       println(" <tr><th colspan='2'>Session: " + token + "</td></tr>");
120       println(" <tr><th>Running for</th><td>" + (now.getTime() - token.getStarted()) + " ms</td></tr>");
121       println(" <tr><th>Thread</th><td>" + token.getThread() + "</td></tr>");
122       println(" <tr><th>PoemTransaction</th><td>"
123                  + token.getTransaction() + "<br>(Database:"
124                  + token.getTransaction().getDatabase() + ")</td></tr>");
125       println(" <tr><th>PoemTask</th><td>" + token.getTask() + "</td></tr>\n");
126       Enumeration<Object> o = token.toTidy().elements();
127       if(o.hasMoreElements()) {
128         println("<tr><th>Open: </th><td>");
129         while (o.hasMoreElements()) {
130           println(o.nextElement() + "<br>");
131         }
132         println("</td></tr>\n");
133       }
134       println("</table>\n");
135     }
136     println("<h4>Poem sessions in use: " + totalSessions + "</h4>\n");
137 
138     println("<h2>Initialised Databases</h2>");
139     println("<table border=1 cellspacing=0 cellpadding=1>");
140     println("<tr><th>Database</th><th>PoemTransaction</th>");
141     println("<th>Free</th><th>Blocked</th></tr>\n");
142     
143     int totalDbs = 0;
144     Enumeration<Database> dbs = org.melati.LogicalDatabase.initialisedDatabases().
145                                                    elements();
146     while(dbs.hasMoreElements()) {
147       totalDbs++;
148       Database db = dbs.nextElement();
149       println("<tr>");
150       println(" <td>" + db.getDisplayName() + "</td>");
151       println(" <td>" + db + "</td>");
152       println(" <td>" + db.getFreeTransactionsCount() + "</td>");
153       println(" <td>" + (db.transactionsMax() - db.getFreeTransactionsCount()) + "</td>");
154       //println(" <td>" + db.getLastQuery() + "</td>");
155       println("</tr>");
156       for(int i=0; i < db.transactionsMax(); i++) {
157         boolean isFree = db.isFree(db.poemTransaction(i));
158         Transaction blockedOn = db.poemTransaction(i).getBlockedOn();
159         println("<tr><td>&nbsp</td>\n"
160                      + "<td>" + db.poemTransaction(i) + "</td>\n"
161                      + "<td bgcolor=" + (isFree ? "green" : "red") + ">"
162                      + isFree + "</td>\n"
163                      + "<td bgcolor=" + (blockedOn != null ? "red" : "green") + ">"
164                      + (blockedOn != null  ? blockedOn.toString() : "&nbsp;") + "</td>\n"
165                      + "</tr>\n");
166       }
167     }
168     println("</table>\n");
169     println("<h4>Initialised databases in use: " + totalDbs + "</h4>");
170     
171     
172     println("</body>\n");
173     println("</html>\n");
174   }
175 
176   void println(String in) throws IOException { 
177     output.write(in + "\n");
178   }
179 }
180 
181 
182 
183 
184 
185