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