• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/HttpConnectionMetricsImpl.java $
3  * $Revision: 548031 $
4  * $Date: 2007-06-17 04:28:38 -0700 (Sun, 17 Jun 2007) $
5  *
6  * ====================================================================
7  * Licensed to the Apache Software Foundation (ASF) under one
8  * or more contributor license agreements.  See the NOTICE file
9  * distributed with this work for additional information
10  * regarding copyright ownership.  The ASF licenses this file
11  * to you under the Apache License, Version 2.0 (the
12  * "License"); you may not use this file except in compliance
13  * with the License.  You may obtain a copy of the License at
14  *
15  *   http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing,
18  * software distributed under the License is distributed on an
19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20  * KIND, either express or implied.  See the License for the
21  * specific language governing permissions and limitations
22  * under the License.
23  * ====================================================================
24  *
25  * This software consists of voluntary contributions made by many
26  * individuals on behalf of the Apache Software Foundation.  For more
27  * information on the Apache Software Foundation, please see
28  * <http://www.apache.org/>.
29  *
30  */
31 
32 package org.apache.http.impl;
33 
34 import java.util.HashMap;
35 import org.apache.http.HttpConnectionMetrics;
36 import org.apache.http.io.HttpTransportMetrics;
37 
38 /**
39  * Implementation of the metrics interface.
40  */
41 public class HttpConnectionMetricsImpl implements HttpConnectionMetrics {
42 
43     public static final String REQUEST_COUNT = "http.request-count";
44     public static final String RESPONSE_COUNT = "http.response-count";
45     public static final String SENT_BYTES_COUNT = "http.sent-bytes-count";
46     public static final String RECEIVED_BYTES_COUNT = "http.received-bytes-count";
47 
48     private final HttpTransportMetrics inTransportMetric;
49     private final HttpTransportMetrics outTransportMetric;
50     private long requestCount = 0;
51     private long responseCount = 0;
52 
53     /**
54      * The cache map for all metrics values.
55      */
56     private HashMap metricsCache;
57 
HttpConnectionMetricsImpl( final HttpTransportMetrics inTransportMetric, final HttpTransportMetrics outTransportMetric)58     public HttpConnectionMetricsImpl(
59             final HttpTransportMetrics inTransportMetric,
60             final HttpTransportMetrics outTransportMetric) {
61         super();
62         this.inTransportMetric = inTransportMetric;
63         this.outTransportMetric = outTransportMetric;
64     }
65 
66     /* ------------------  Public interface method -------------------------- */
67 
getReceivedBytesCount()68     public long getReceivedBytesCount() {
69         if (this.inTransportMetric != null) {
70             return this.inTransportMetric.getBytesTransferred();
71         } else {
72             return -1;
73         }
74     }
75 
getSentBytesCount()76     public long getSentBytesCount() {
77         if (this.outTransportMetric != null) {
78             return this.outTransportMetric.getBytesTransferred();
79         } else {
80             return -1;
81         }
82     }
83 
getRequestCount()84     public long getRequestCount() {
85         return this.requestCount;
86     }
87 
incrementRequestCount()88     public void incrementRequestCount() {
89         this.requestCount++;
90     }
91 
getResponseCount()92     public long getResponseCount() {
93         return this.responseCount;
94     }
95 
incrementResponseCount()96     public void incrementResponseCount() {
97         this.responseCount++;
98     }
99 
getMetric(final String metricName)100     public Object getMetric(final String metricName) {
101         Object value = null;
102         if (this.metricsCache != null) {
103             value = this.metricsCache.get(metricName);
104         }
105         if (value == null) {
106             if (REQUEST_COUNT.equals(metricName)) {
107                 value = new Long(requestCount);
108             } else if (RESPONSE_COUNT.equals(metricName)) {
109                 value = new Long(responseCount);
110             } else if (RECEIVED_BYTES_COUNT.equals(metricName)) {
111                 if (this.inTransportMetric != null) {
112                     return new Long(this.inTransportMetric.getBytesTransferred());
113                 } else {
114                     return null;
115                 }
116             } else if (SENT_BYTES_COUNT.equals(metricName)) {
117                 if (this.outTransportMetric != null) {
118                     return new Long(this.outTransportMetric.getBytesTransferred());
119                 } else {
120                     return null;
121                 }
122             }
123         }
124         return value;
125     }
126 
setMetric(final String metricName, Object obj)127     public void setMetric(final String metricName, Object obj) {
128         if (this.metricsCache == null) {
129             this.metricsCache = new HashMap();
130         }
131         this.metricsCache.put(metricName, obj);
132     }
133 
reset()134     public void reset() {
135         if (this.outTransportMetric != null) {
136             this.outTransportMetric.reset();
137         }
138         if (this.inTransportMetric != null) {
139             this.inTransportMetric.reset();
140         }
141         this.requestCount = 0;
142         this.responseCount = 0;
143         this.metricsCache = null;
144     }
145 
146 }
147