1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
39
40
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
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
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 }