001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.coyote;
018:
019: /**
020: * Structure holding the Request and Response objects. It also holds statistical
021: * informations about request processing and provide management informations
022: * about the requests beeing processed.
023: *
024: * Each thread uses a Request/Response pair that is recycled on each request.
025: * This object provides a place to collect global low-level statistics - without
026: * having to deal with synchronization ( since each thread will have it's own
027: * RequestProcessorMX ).
028: *
029: * TODO: Request notifications will be registered here.
030: *
031: * @author Costin Manolache
032: */
033: public class RequestInfo {
034: RequestGroupInfo global = null;
035:
036: // ----------------------------------------------------------- Constructors
037:
038: public RequestInfo(Request req) {
039: this .req = req;
040: }
041:
042: public RequestGroupInfo getGlobalProcessor() {
043: return global;
044: }
045:
046: public void setGlobalProcessor(RequestGroupInfo global) {
047: if (global != null) {
048: this .global = global;
049: global.addRequestProcessor(this );
050: } else {
051: if (this .global != null) {
052: this .global.removeRequestProcessor(this );
053: this .global = null;
054: }
055: }
056: }
057:
058: // ----------------------------------------------------- Instance Variables
059: Request req;
060: Response res;
061: int stage = Constants.STAGE_NEW;
062:
063: // -------------------- Information about the current request -----------
064: // This is usefull for long-running requests only
065:
066: public String getMethod() {
067: return req.method().toString();
068: }
069:
070: public String getCurrentUri() {
071: return req.requestURI().toString();
072: }
073:
074: public String getCurrentQueryString() {
075: return req.queryString().toString();
076: }
077:
078: public String getProtocol() {
079: return req.protocol().toString();
080: }
081:
082: public String getVirtualHost() {
083: return req.serverName().toString();
084: }
085:
086: public int getServerPort() {
087: return req.getServerPort();
088: }
089:
090: public String getRemoteAddr() {
091: req.action(ActionCode.ACTION_REQ_HOST_ADDR_ATTRIBUTE, null);
092: return req.remoteAddr().toString();
093: }
094:
095: public int getContentLength() {
096: return req.getContentLength();
097: }
098:
099: public long getRequestBytesReceived() {
100: return req.getBytesRead();
101: }
102:
103: public long getRequestBytesSent() {
104: return req.getResponse().getBytesWritten();
105: }
106:
107: public long getRequestProcessingTime() {
108: return (System.currentTimeMillis() - req.getStartTime());
109: }
110:
111: // -------------------- Statistical data --------------------
112: // Collected at the end of each request.
113: private long bytesSent;
114: private long bytesReceived;
115:
116: // Total time = divide by requestCount to get average.
117: private long processingTime;
118: // The longest response time for a request
119: private long maxTime;
120: // URI of the request that took maxTime
121: private String maxRequestUri;
122:
123: private int requestCount;
124: // number of response codes >= 400
125: private int errorCount;
126:
127: /** Called by the processor before recycling the request. It'll collect
128: * statistic information.
129: */
130: void updateCounters() {
131: bytesReceived += req.getBytesRead();
132: bytesSent += req.getResponse().getBytesWritten();
133:
134: requestCount++;
135: if (req.getResponse().getStatus() >= 400)
136: errorCount++;
137: long t0 = req.getStartTime();
138: long t1 = System.currentTimeMillis();
139: long time = t1 - t0;
140: processingTime += time;
141: if (maxTime < time) {
142: maxTime = time;
143: maxRequestUri = req.requestURI().toString();
144: }
145: }
146:
147: public int getStage() {
148: return stage;
149: }
150:
151: public void setStage(int stage) {
152: this .stage = stage;
153: }
154:
155: public long getBytesSent() {
156: return bytesSent;
157: }
158:
159: public void setBytesSent(long bytesSent) {
160: this .bytesSent = bytesSent;
161: }
162:
163: public long getBytesReceived() {
164: return bytesReceived;
165: }
166:
167: public void setBytesReceived(long bytesReceived) {
168: this .bytesReceived = bytesReceived;
169: }
170:
171: public long getProcessingTime() {
172: return processingTime;
173: }
174:
175: public void setProcessingTime(long processingTime) {
176: this .processingTime = processingTime;
177: }
178:
179: public long getMaxTime() {
180: return maxTime;
181: }
182:
183: public void setMaxTime(long maxTime) {
184: this .maxTime = maxTime;
185: }
186:
187: public String getMaxRequestUri() {
188: return maxRequestUri;
189: }
190:
191: public void setMaxRequestUri(String maxRequestUri) {
192: this .maxRequestUri = maxRequestUri;
193: }
194:
195: public int getRequestCount() {
196: return requestCount;
197: }
198:
199: public void setRequestCount(int requestCount) {
200: this .requestCount = requestCount;
201: }
202:
203: public int getErrorCount() {
204: return errorCount;
205: }
206:
207: public void setErrorCount(int errorCount) {
208: this.errorCount = errorCount;
209: }
210:
211: }
|