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