KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > webapp > StatisticsFilterChain


1 /*
2  * Copyright (c) 1998-2005 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 Sam
28  */

29
30
31 package com.caucho.server.webapp;
32
33 import com.caucho.server.connection.AbstractHttpRequest;
34 import com.caucho.server.connection.Connection;
35 import com.caucho.server.port.TcpConnection;
36 import com.caucho.util.Alarm;
37 import com.caucho.vfs.ClientDisconnectException;
38
39 import javax.servlet.FilterChain JavaDoc;
40 import javax.servlet.ServletException JavaDoc;
41 import javax.servlet.ServletRequest JavaDoc;
42 import javax.servlet.ServletResponse JavaDoc;
43 import java.io.IOException JavaDoc;
44
45 public class StatisticsFilterChain
46   implements FilterChain JavaDoc
47 {
48   private final FilterChain JavaDoc _next;
49   private WebApp _webApp;
50
51   public StatisticsFilterChain(FilterChain JavaDoc next, WebApp webApp)
52   {
53     _next = next;
54     _webApp = webApp;
55   }
56
57   public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
58     throws ServletException JavaDoc, IOException JavaDoc
59   {
60     if (request instanceof AbstractHttpRequest) {
61       AbstractHttpRequest httpRequest = (AbstractHttpRequest) request;
62
63       Connection connection = httpRequest.getConnection();
64
65       if (connection instanceof TcpConnection) {
66         TcpConnection tcpConnection = (TcpConnection) connection;
67
68         long time = Alarm.getExactTime();
69
70         long readBytes = -1;
71         long writeBytes = -1;
72
73         readBytes = tcpConnection.getSocket().getTotalReadBytes();
74         writeBytes = tcpConnection.getSocket().getTotalWriteBytes();
75
76         ClientDisconnectException clientDisconnectException = null;
77
78         try {
79           _next.doFilter(request, response);
80         }
81         catch (ClientDisconnectException ex) {
82           clientDisconnectException = ex;
83         }
84
85         time = Alarm.getExactTime() - time;
86
87         readBytes = tcpConnection.getSocket().getTotalReadBytes() - readBytes;
88         writeBytes = tcpConnection.getSocket().getTotalReadBytes() - writeBytes;
89
90         _webApp.updateStatistics(time, (int) readBytes, (int) writeBytes, clientDisconnectException != null);
91
92         if (clientDisconnectException != null)
93           throw clientDisconnectException;
94       }
95     }
96     else {
97       _next.doFilter(request, response);
98     }
99   }
100 }
101
Popular Tags