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.io.HttpTransportMetrics;
35  
36  /**
37   * Default implementation of the {@link HttpConnectionMetrics} interface.
38   *
39   * @since 4.0
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 Map<String, Object> metricsCache;
57  
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  
68      @Override
69      public long getReceivedBytesCount() {
70          return this.inTransportMetric != null ? this.inTransportMetric.getBytesTransferred() : -1;
71      }
72  
73      @Override
74      public long getSentBytesCount() {
75          return this.outTransportMetric != null ? this.outTransportMetric.getBytesTransferred() : -1;
76      }
77  
78      @Override
79      public long getRequestCount() {
80          return this.requestCount;
81      }
82  
83      public void incrementRequestCount() {
84          this.requestCount++;
85      }
86  
87      @Override
88      public long getResponseCount() {
89          return this.responseCount;
90      }
91  
92      public void incrementResponseCount() {
93          this.responseCount++;
94      }
95  
96      @Override
97      public Object getMetric(final String metricName) {
98          Object value = null;
99          if (this.metricsCache != null) {
100             value = this.metricsCache.get(metricName);
101         }
102         if (value == null) {
103             if (REQUEST_COUNT.equals(metricName)) {
104                 value = Long.valueOf(requestCount);
105             } else if (RESPONSE_COUNT.equals(metricName)) {
106                 value = Long.valueOf(responseCount);
107             } else if (RECEIVED_BYTES_COUNT.equals(metricName)) {
108                 return this.inTransportMetric != null
109                                 ? Long.valueOf(this.inTransportMetric.getBytesTransferred())
110                                 : null;
111             } else if (SENT_BYTES_COUNT.equals(metricName)) {
112                 return this.outTransportMetric != null
113                                 ? Long.valueOf(this.outTransportMetric.getBytesTransferred())
114                                 : null;
115             }
116         }
117         return value;
118     }
119 
120     public void setMetric(final String metricName, final Object obj) {
121         if (this.metricsCache == null) {
122             this.metricsCache = new HashMap<String, Object>();
123         }
124         this.metricsCache.put(metricName, obj);
125     }
126 
127     @Override
128     public void reset() {
129         if (this.outTransportMetric != null) {
130             this.outTransportMetric.reset();
131         }
132         if (this.inTransportMetric != null) {
133             this.inTransportMetric.reset();
134         }
135         this.requestCount = 0;
136         this.responseCount = 0;
137         this.metricsCache = null;
138     }
139 
140 }