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          if (this.inTransportMetric != null) {
71              return this.inTransportMetric.getBytesTransferred();
72          } else {
73              return -1;
74          }
75      }
76  
77      @Override
78      public long getSentBytesCount() {
79          if (this.outTransportMetric != null) {
80              return this.outTransportMetric.getBytesTransferred();
81          } else {
82              return -1;
83          }
84      }
85  
86      @Override
87      public long getRequestCount() {
88          return this.requestCount;
89      }
90  
91      public void incrementRequestCount() {
92          this.requestCount++;
93      }
94  
95      @Override
96      public long getResponseCount() {
97          return this.responseCount;
98      }
99  
100     public void incrementResponseCount() {
101         this.responseCount++;
102     }
103 
104     @Override
105     public Object getMetric(final String metricName) {
106         Object value = null;
107         if (this.metricsCache != null) {
108             value = this.metricsCache.get(metricName);
109         }
110         if (value == null) {
111             if (REQUEST_COUNT.equals(metricName)) {
112                 value = Long.valueOf(requestCount);
113             } else if (RESPONSE_COUNT.equals(metricName)) {
114                 value = Long.valueOf(responseCount);
115             } else if (RECEIVED_BYTES_COUNT.equals(metricName)) {
116                 if (this.inTransportMetric != null) {
117                     return Long.valueOf(this.inTransportMetric.getBytesTransferred());
118                 } else {
119                     return null;
120                 }
121             } else if (SENT_BYTES_COUNT.equals(metricName)) {
122                 if (this.outTransportMetric != null) {
123                     return Long.valueOf(this.outTransportMetric.getBytesTransferred());
124                 } else {
125                     return null;
126                 }
127             }
128         }
129         return value;
130     }
131 
132     public void setMetric(final String metricName, final Object obj) {
133         if (this.metricsCache == null) {
134             this.metricsCache = new HashMap<String, Object>();
135         }
136         this.metricsCache.put(metricName, obj);
137     }
138 
139     @Override
140     public void reset() {
141         if (this.outTransportMetric != null) {
142             this.outTransportMetric.reset();
143         }
144         if (this.inTransportMetric != null) {
145             this.inTransportMetric.reset();
146         }
147         this.requestCount = 0;
148         this.responseCount = 0;
149         this.metricsCache = null;
150     }
151 
152 }