001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027 028package org.apache.http.impl; 029 030import java.util.HashMap; 031import java.util.Map; 032 033import org.apache.http.HttpConnectionMetrics; 034import org.apache.http.io.HttpTransportMetrics; 035 036/** 037 * Default implementation of the {@link HttpConnectionMetrics} interface. 038 * 039 * @since 4.0 040 */ 041public class HttpConnectionMetricsImpl implements HttpConnectionMetrics { 042 043 public static final String REQUEST_COUNT = "http.request-count"; 044 public static final String RESPONSE_COUNT = "http.response-count"; 045 public static final String SENT_BYTES_COUNT = "http.sent-bytes-count"; 046 public static final String RECEIVED_BYTES_COUNT = "http.received-bytes-count"; 047 048 private final HttpTransportMetrics inTransportMetric; 049 private final HttpTransportMetrics outTransportMetric; 050 private long requestCount = 0; 051 private long responseCount = 0; 052 053 /** 054 * The cache map for all metrics values. 055 */ 056 private Map<String, Object> metricsCache; 057 058 public HttpConnectionMetricsImpl( 059 final HttpTransportMetrics inTransportMetric, 060 final HttpTransportMetrics outTransportMetric) { 061 super(); 062 this.inTransportMetric = inTransportMetric; 063 this.outTransportMetric = outTransportMetric; 064 } 065 066 /* ------------------ Public interface method -------------------------- */ 067 068 @Override 069 public long getReceivedBytesCount() { 070 if (this.inTransportMetric != null) { 071 return this.inTransportMetric.getBytesTransferred(); 072 } else { 073 return -1; 074 } 075 } 076 077 @Override 078 public long getSentBytesCount() { 079 if (this.outTransportMetric != null) { 080 return this.outTransportMetric.getBytesTransferred(); 081 } else { 082 return -1; 083 } 084 } 085 086 @Override 087 public long getRequestCount() { 088 return this.requestCount; 089 } 090 091 public void incrementRequestCount() { 092 this.requestCount++; 093 } 094 095 @Override 096 public long getResponseCount() { 097 return this.responseCount; 098 } 099 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}