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