View Javadoc

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