KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > server > StatsLogger


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.server;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.mortbay.jetty.Server;
29
30 import com.sslexplorer.boot.ContextHolder;
31 import com.sslexplorer.boot.Util;
32
33 public class StatsLogger extends Thread JavaDoc {
34     final static Log log = LogFactory.getLog(StatsLogger.class);
35     
36     private Server server;
37     private int update;
38     
39     public StatsLogger(Server server, int update) {
40         super("StatsLogger");
41         server.setStatsOn(true);
42         setDaemon(true);
43         this.server = server;
44         this.update = update;
45         start();
46     }
47
48     public void run() {
49         FileOutputStream JavaDoc out = null;
50         try {
51             out = new FileOutputStream JavaDoc(new File JavaDoc(ContextHolder.getContext().getLogDirectory(), "stats.csv"));
52             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(out, true);
53             pw.println("connections,connectionsOpen,connectionsOpenMax,connectionsDurationAve," + "connectionsDurationMax,connectionsRequestsAve,connectionsRequestsMax,"
54                 + "errors,requests,requestsActive,requestsActiveMax,requestsDurectionAve,requestsDurationMax");
55             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
56             while (true) {
57                 sb.append(server.getConnections());
58                 sb.append(",");
59                 sb.append(server.getConnectionsOpen());
60                 sb.append(",");
61                 sb.append(server.getConnectionsOpenMax());
62                 sb.append(",");
63                 sb.append(server.getConnectionsDurationAve());
64                 sb.append(",");
65                 sb.append(server.getConnectionsDurationMax());
66                 sb.append(",");
67                 sb.append(server.getConnectionsRequestsAve());
68                 sb.append(",");
69                 sb.append(server.getConnectionsRequestsMax());
70                 sb.append(",");
71                 sb.append(server.getErrors());
72                 sb.append(",");
73                 sb.append(server.getRequests());
74                 sb.append(",");
75                 sb.append(server.getRequestsActive());
76                 sb.append(",");
77                 sb.append(server.getRequestsActiveMax());
78                 sb.append(",");
79                 sb.append(server.getRequestsDurationAve());
80                 sb.append(",");
81                 sb.append(server.getRequestsDurationMax());
82                 pw.println(sb.toString());
83                 sb.setLength(0);
84                 Thread.sleep(update);
85             }
86         } catch (Exception JavaDoc e) {
87             log.error("Failed to create stats files.", e);
88         } finally {
89             Util.closeStream(out);
90         }
91     }
92
93 }
94
Popular Tags