Coverage Report

##### Coverage Report - org.melati.test.SessionAnalysisServlet

Classes in this File | Line Coverage | Branch Coverage | Complexity
SessionAnalysisServlet |
73% | 48/65
50% | 9/18
| 5.5;5.5

1 |  |
     /*
2 |  |
      * $Source: /usr/cvsroot/melati/melati/src/site/resources/withWebmacro/org.melati.test.SessionAnalysisServlet.html,v $
3 |  |
      * $Revision: 1.1 $
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 | 2 |
     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 | 2 |
         melati.getResponse().setContentType("text/html");
79 | 2 |
         output = melati.getWriter();
80 | 2 |
         Date now = new Date();
81 | 2 |
         println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
82 |  |
                      + "HTML 4.01 Transitional//EN\">\n");
83 | 2 |
         println("<html>\n");
84 | 2 |
         println("<head>\n");
85 | 2 |
         println("<title>Transaction Analysis</title>\n");
86 | 2 |
         String repeat = melati.getRequest().getParameter("repeat");
87 | 2 |
         if (repeat != null)
88 | 0 |
           println("<META HTTP-EQUIV=\"Refresh\" CONTENT=\""
89 |  |
                         + repeat + "; URL="
90 |  |
                         + melati.getRequest().getRequestURI() + "?repeat="
91 |  |
                         + repeat + "\">");
92 | 2 |
         println("  <link rel='stylesheet' title='Default' href='" +
93 |  |
                      melati.getConfig().getStaticURL() + "/admin.css' \n" +
94 |  |
                      "type='text/css' media='screen'>\n");
95 | 2 |
         println("</head>");
96 | 2 |
         println("<body>");
97 | 2 |
         println("<h1>Transactions Analysis</h1>");
98 | 2 |
         println("<p>Run at " + now + "</p>\n");
99 | 2 |
         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 | 2 |
         println("<h2>Poem sessions</h2>\n");
111 |  |
112 | 2 |
         Enumeration<SessionToken> e = PoemThread.openSessions().elements();
113 |  |
114 | 2 |
         int totalSessions = 0;
115 | 2 |
         while(e.hasMoreElements()) {
116 | 0 |
           totalSessions++;
117 | 0 |
           SessionToken token = e.nextElement();
118 | 0 |
           println("<table border='1' cellspacing='0' cellpadding='1'>");
119 | 0 |
           println(" <tr><th colspan='2'>Session: " + token + "</td></tr>");
120 | 0 |
           println(" <tr><th>Running for</th><td>" + (now.getTime() - token.getStarted()) + " ms</td></tr>");
121 | 0 |
           println(" <tr><th>Thread</th><td>" + token.getThread() + "</td></tr>");
122 | 0 |
           println(" <tr><th>PoemTransaction</th><td>"
123 |  |
                      + token.getTransaction() + "<br>(Database:"
124 |  |
                      + token.getTransaction().getDatabase() + ")</td></tr>");
125 | 0 |
           println(" <tr><th>PoemTask</th><td>" + token.getTask() + "</td></tr>\n");
126 | 0 |
           Enumeration<Object> o = token.toTidy().elements();
127 | 0 |
           if(o.hasMoreElements()) {
128 | 0 |
             println("<tr><th>Open: </th><td>");
129 | 0 |
             while (o.hasMoreElements()) {
130 | 0 |
               println(o.nextElement() + "<br>");
131 |  |
             }
132 | 0 |
             println("</td></tr>\n");
133 |  |
           }
134 | 0 |
           println("</table>\n");
135 | 0 |
         }
136 | 2 |
         println("<h4>Poem sessions in use: " + totalSessions + "</h4>\n");
137 |  |
138 | 2 |
         println("<h2>Initialised Databases</h2>");
139 | 2 |
         println("<table border=1 cellspacing=0 cellpadding=1>");
140 | 2 |
         println("<tr><th>Database</th><th>PoemTransaction</th>");
141 | 2 |
         println("<th>Free</th><th>Blocked</th></tr>\n");
142 |  |
143 | 2 |
         int totalDbs = 0;
144 | 2 |
         Enumeration<Database> dbs = org.melati.LogicalDatabase.initialisedDatabases().
145 |  |
                                                        elements();
146 | 14 |
         while(dbs.hasMoreElements()) {
147 | 12 |
           totalDbs++;
148 | 12 |
           Database db = dbs.nextElement();
149 | 12 |
           println("<tr>");
150 | 12 |
           println(" <td>" + db.getDisplayName() + "</td>");
151 | 12 |
           println(" <td>" + db + "</td>");
152 | 12 |
           println(" <td>" + db.getFreeTransactionsCount() + "</td>");
153 | 12 |
           println(" <td>" + (db.transactionsMax() - db.getFreeTransactionsCount()) + "</td>");
154 |  |
           //println(" <td>" + db.getLastQuery() + "</td>");
155 | 12 |
           println("</tr>");
156 | 60 |
           for(int i=0; i < db.transactionsMax(); i++) {
157 | 48 |
             boolean isFree = db.isFree(db.poemTransaction(i));
158 | 48 |
             Transaction blockedOn = db.poemTransaction(i).getBlockedOn();
159 | 48 |
             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 | 12 |
         }
168 | 2 |
         println("</table>\n");
169 | 2 |
         println("<h4>Initialised databases in use: " + totalDbs + "</h4>");
170 |  |
171 |  |
172 | 2 |
         println("</body>\n");
173 | 2 |
         println("</html>\n");
174 | 2 |
       }
175 |  |
176 |  |
       void println(String in) throws IOException {
177 | 160 |
         output.write(in + "\n");
178 | 160 |
       }
179 |  |
     }
180 |  |
181 |  |
182 |  |
183 |  |
184 |  |
185 |  |