KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > coyote > RequestInfo


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.coyote;
19
20
21 /**
22  * Structure holding the Request and Response objects. It also holds statistical
23  * informations about request processing and provide management informations
24  * about the requests beeing processed.
25  *
26  * Each thread uses a Request/Response pair that is recycled on each request.
27  * This object provides a place to collect global low-level statistics - without
28  * having to deal with synchronization ( since each thread will have it's own
29  * RequestProcessorMX ).
30  *
31  * TODO: Request notifications will be registered here.
32  *
33  * @author Costin Manolache
34  */

35 public class RequestInfo {
36     RequestGroupInfo global=null;
37
38     // ----------------------------------------------------------- Constructors
39

40     public RequestInfo( Request req) {
41         this.req=req;
42     }
43
44     public RequestGroupInfo getGlobalProcessor() {
45         return global;
46     }
47     
48     public void setGlobalProcessor(RequestGroupInfo global) {
49         if( global != null) {
50             this.global=global;
51             global.addRequestProcessor( this );
52         } else {
53             if (this.global != null) {
54                 this.global.removeRequestProcessor( this );
55                 this.global = null;
56             }
57         }
58     }
59
60
61     // ----------------------------------------------------- Instance Variables
62
Request req;
63     Response res;
64     int stage = Constants.STAGE_NEW;
65
66     // -------------------- Information about the current request -----------
67
// This is usefull for long-running requests only
68

69     public String JavaDoc getMethod() {
70         return req.method().toString();
71     }
72
73     public String JavaDoc getCurrentUri() {
74         return req.requestURI().toString();
75     }
76
77     public String JavaDoc getCurrentQueryString() {
78         return req.queryString().toString();
79     }
80
81     public String JavaDoc getProtocol() {
82         return req.protocol().toString();
83     }
84
85     public String JavaDoc getVirtualHost() {
86         return req.serverName().toString();
87     }
88
89     public int getServerPort() {
90         return req.getServerPort();
91     }
92
93     public String JavaDoc getRemoteAddr() {
94         req.action(ActionCode.ACTION_REQ_HOST_ADDR_ATTRIBUTE, null);
95         return req.remoteAddr().toString();
96     }
97
98     public int getContentLength() {
99         return req.getContentLength();
100     }
101
102     public long getRequestBytesReceived() {
103         return req.getBytesRead();
104     }
105
106     public long getRequestBytesSent() {
107         return req.getResponse().getBytesWritten();
108     }
109
110     public long getRequestProcessingTime() {
111         return (System.currentTimeMillis() - req.getStartTime());
112     }
113
114     // -------------------- Statistical data --------------------
115
// Collected at the end of each request.
116
private long bytesSent;
117     private long bytesReceived;
118
119     // Total time = divide by requestCount to get average.
120
private long processingTime;
121     // The longest response time for a request
122
private long maxTime;
123     // URI of the request that took maxTime
124
private String JavaDoc maxRequestUri;
125
126     private int requestCount;
127     // number of response codes >= 400
128
private int errorCount;
129
130
131     /** Called by the processor before recycling the request. It'll collect
132      * statistic information.
133      */

134     void updateCounters() {
135         bytesReceived+=req.getBytesRead();
136         bytesSent+=req.getResponse().getBytesWritten();
137
138         requestCount++;
139         if( req.getResponse().getStatus() >=400 )
140             errorCount++;
141         long t0=req.getStartTime();
142         long t1=System.currentTimeMillis();
143         long time=t1-t0;
144         processingTime+=time;
145         if( maxTime < time ) {
146             maxTime=time;
147             maxRequestUri=req.requestURI().toString();
148         }
149     }
150
151     public int getStage() {
152         return stage;
153     }
154
155     public void setStage(int stage) {
156         this.stage = stage;
157     }
158
159     public long getBytesSent() {
160         return bytesSent;
161     }
162
163     public void setBytesSent(long bytesSent) {
164         this.bytesSent = bytesSent;
165     }
166
167     public long getBytesReceived() {
168         return bytesReceived;
169     }
170
171     public void setBytesReceived(long bytesReceived) {
172         this.bytesReceived = bytesReceived;
173     }
174
175     public long getProcessingTime() {
176         return processingTime;
177     }
178
179     public void setProcessingTime(long processingTime) {
180         this.processingTime = processingTime;
181     }
182
183     public long getMaxTime() {
184         return maxTime;
185     }
186
187     public void setMaxTime(long maxTime) {
188         this.maxTime = maxTime;
189     }
190
191     public String JavaDoc getMaxRequestUri() {
192         return maxRequestUri;
193     }
194
195     public void setMaxRequestUri(String JavaDoc maxRequestUri) {
196         this.maxRequestUri = maxRequestUri;
197     }
198
199     public int getRequestCount() {
200         return requestCount;
201     }
202
203     public void setRequestCount(int requestCount) {
204         this.requestCount = requestCount;
205     }
206
207     public int getErrorCount() {
208         return errorCount;
209     }
210
211     public void setErrorCount(int errorCount) {
212         this.errorCount = errorCount;
213     }
214
215
216 }
217
Popular Tags