Coverage Report - org.melati.test.SessionAnalysisServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
SessionAnalysisServlet
71%
53/74
50%
9/18
5.5
 
 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  1
 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  1
     melati.getResponse().setContentType("text/html");
 79  1
     output = melati.getWriter();
 80  1
     Date now = new Date();
 81  1
     println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD " 
 82  
                  + "HTML 4.01 Transitional//EN\">\n");
 83  1
     println("<html>\n");
 84  1
     println("<head>\n");
 85  1
     println("<title>Transaction Analysis</title>\n");
 86  1
     String repeat = melati.getRequest().getParameter("repeat");
 87  1
     if (repeat != null)
 88  0
       println("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"" 
 89  
                     + repeat + "; URL="
 90  0
                     + melati.getRequest().getRequestURI() + "?repeat="
 91  
                     + repeat + "\">");
 92  2
     println("  <link rel='stylesheet' title='Default' href='" + 
 93  1
                  melati.getConfig().getStaticURL() + "/admin.css' \n" +
 94  
                  "type='text/css' media='screen'>\n");
 95  1
     println("</head>");
 96  1
     println("<body>");
 97  1
     println("<h1>Transactions Analysis</h1>");
 98  1
     println("<p>Run at " + now + "</p>\n");
 99  2
     println("<p>JVM Free memory: " +
 100  1
    NumberFormat.getInstance().format(Runtime.getRuntime().freeMemory())
 101  
                   + "</p>\n"
 102  
                   + "<p>JVM Total memory: " +
 103  1
    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  1
     println("<h2>Poem sessions</h2>\n");
 111  
 
 112  1
     Enumeration<SessionToken> e = PoemThread.openSessions().elements();
 113  
 
 114  1
     int totalSessions = 0;
 115  1
     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  0
                  + token.getTransaction() + "<br>(Database:"
 124  0
                  + 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  1
     println("<h4>Poem sessions in use: " + totalSessions + "</h4>\n");
 137  
 
 138  1
     println("<h2>Initialised Databases</h2>");
 139  1
     println("<table border=1 cellspacing=0 cellpadding=1>");
 140  1
     println("<tr><th>Database</th><th>PoemTransaction</th>");
 141  1
     println("<th>Free</th><th>Blocked</th></tr>\n");
 142  
     
 143  1
     int totalDbs = 0;
 144  1
     Enumeration<Database> dbs = org.melati.LogicalDatabase.initialisedDatabases().
 145  1
                                                    elements();
 146  4
     while(dbs.hasMoreElements()) {
 147  3
       totalDbs++;
 148  3
       Database db = dbs.nextElement();
 149  3
       println("<tr>");
 150  3
       println(" <td>" + db.getDisplayName() + "</td>");
 151  3
       println(" <td>" + db + "</td>");
 152  3
       println(" <td>" + db.getFreeTransactionsCount() + "</td>");
 153  3
       println(" <td>" + (db.transactionsMax() - db.getFreeTransactionsCount()) + "</td>");
 154  
       //println(" <td>" + db.getLastQuery() + "</td>");
 155  3
       println("</tr>");
 156  27
       for(int i=0; i < db.transactionsMax(); i++) {
 157  24
         boolean isFree = db.isFree(db.poemTransaction(i));
 158  24
         Transaction blockedOn = db.poemTransaction(i).getBlockedOn();
 159  48
         println("<tr><td>&nbsp</td>\n"
 160  24
                      + "<td>" + db.poemTransaction(i) + "</td>\n"
 161  
                      + "<td bgcolor=" + (isFree ? "green" : "red") + ">"
 162  
                      + isFree + "</td>\n"
 163  
                      + "<td bgcolor=" + (blockedOn != null ? "red" : "green") + ">"
 164  0
                      + (blockedOn != null  ? blockedOn.toString() : "&nbsp;") + "</td>\n"
 165  
                      + "</tr>\n");
 166  
       }
 167  3
     }
 168  1
     println("</table>\n");
 169  1
     println("<h4>Initialised databases in use: " + totalDbs + "</h4>");
 170  
     
 171  
     
 172  1
     println("</body>\n");
 173  1
     println("</html>\n");
 174  1
   }
 175  
 
 176  
   void println(String in) throws IOException { 
 177  62
     output.write(in + "\n");
 178  62
   }
 179  
 }
 180  
 
 181  
 
 182  
 
 183  
 
 184  
 
 185