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 package org.apache.coyote;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.tomcat.util.modeler.BaseModelMBean;
23
24 /** This can be moved to top level ( eventually with a better name ).
25  *  It is currently used only as a JMX artifact, to aggregate the data
26  *  collected from each RequestProcessor thread.
27  */

28 public class RequestGroupInfo extends BaseModelMBean {
29     private final List<RequestInfo> processors = new ArrayList<>();
30     private long deadMaxTime = 0;
31     private long deadProcessingTime = 0;
32     private int deadRequestCount = 0;
33     private int deadErrorCount = 0;
34     private long deadBytesReceived = 0;
35     private long deadBytesSent = 0;
36
37     public synchronized void addRequestProcessor( RequestInfo rp ) {
38         processors.add( rp );
39     }
40
41     public synchronized void removeRequestProcessor( RequestInfo rp ) {
42         if( rp != null ) {
43             if( deadMaxTime < rp.getMaxTime() )
44                 deadMaxTime = rp.getMaxTime();
45             deadProcessingTime += rp.getProcessingTime();
46             deadRequestCount += rp.getRequestCount();
47             deadErrorCount += rp.getErrorCount();
48             deadBytesReceived += rp.getBytesReceived();
49             deadBytesSent += rp.getBytesSent();
50
51             processors.remove( rp );
52         }
53     }
54
55     public synchronized long getMaxTime() {
56         long maxTime = deadMaxTime;
57         for (RequestInfo rp : processors) {
58             if (maxTime < rp.getMaxTime()) {
59                 maxTime=rp.getMaxTime();
60             }
61         }
62         return maxTime;
63     }
64
65     // Used to reset the times
66     public synchronized void setMaxTime(long maxTime) {
67         deadMaxTime = maxTime;
68         for (RequestInfo rp : processors) {
69             rp.setMaxTime(maxTime);
70         }
71     }
72
73     public synchronized long getProcessingTime() {
74         long time = deadProcessingTime;
75         for (RequestInfo rp : processors) {
76             time += rp.getProcessingTime();
77         }
78         return time;
79     }
80
81     public synchronized void setProcessingTime(long totalTime) {
82         deadProcessingTime = totalTime;
83         for (RequestInfo rp : processors) {
84             rp.setProcessingTime( totalTime );
85         }
86     }
87
88     public synchronized int getRequestCount() {
89         int requestCount = deadRequestCount;
90         for (RequestInfo rp : processors) {
91             requestCount += rp.getRequestCount();
92         }
93         return requestCount;
94     }
95
96     public synchronized void setRequestCount(int requestCount) {
97         deadRequestCount = requestCount;
98         for (RequestInfo rp : processors) {
99             rp.setRequestCount( requestCount );
100         }
101     }
102
103     public synchronized int getErrorCount() {
104         int requestCount = deadErrorCount;
105         for (RequestInfo rp : processors) {
106             requestCount += rp.getErrorCount();
107         }
108         return requestCount;
109     }
110
111     public synchronized void setErrorCount(int errorCount) {
112         deadErrorCount = errorCount;
113         for (RequestInfo rp : processors) {
114             rp.setErrorCount( errorCount);
115         }
116     }
117
118     public synchronized long getBytesReceived() {
119         long bytes = deadBytesReceived;
120         for (RequestInfo rp : processors) {
121             bytes += rp.getBytesReceived();
122         }
123         return bytes;
124     }
125
126     public synchronized void setBytesReceived(long bytesReceived) {
127         deadBytesReceived = bytesReceived;
128         for (RequestInfo rp : processors) {
129             rp.setBytesReceived( bytesReceived );
130         }
131     }
132
133     public synchronized long getBytesSent() {
134         long bytes=deadBytesSent;
135         for (RequestInfo rp : processors) {
136             bytes += rp.getBytesSent();
137         }
138         return bytes;
139     }
140
141     public synchronized void setBytesSent(long bytesSent) {
142         deadBytesSent = bytesSent;
143         for (RequestInfo rp : processors) {
144             rp.setBytesSent( bytesSent );
145         }
146     }
147
148     public void resetCounters() {
149         this.setBytesReceived(0);
150         this.setBytesSent(0);
151         this.setRequestCount(0);
152         this.setProcessingTime(0);
153         this.setMaxTime(0);
154         this.setErrorCount(0);
155     }
156 }
157