michael@0: /* michael@0: * ==================================================================== michael@0: * michael@0: * Licensed to the Apache Software Foundation (ASF) under one or more michael@0: * contributor license agreements. See the NOTICE file distributed with michael@0: * this work for additional information regarding copyright ownership. michael@0: * The ASF licenses this file to You under the Apache License, Version 2.0 michael@0: * (the "License"); you may not use this file except in compliance with michael@0: * the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.conn.params; michael@0: michael@0: import java.util.Map; michael@0: import java.util.concurrent.ConcurrentHashMap; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.ThreadSafe; michael@0: michael@0: import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; michael@0: michael@0: /** michael@0: * This class maintains a map of HTTP routes to maximum number of connections allowed michael@0: * for those routes. This class can be used by pooling michael@0: * {@link ch.boye.httpclientandroidlib.conn.ClientConnectionManager connection managers} for michael@0: * a fine-grained control of connections on a per route basis. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @ThreadSafe michael@0: public final class ConnPerRouteBean implements ConnPerRoute { michael@0: michael@0: /** The default maximum number of connections allowed per host */ michael@0: public static final int DEFAULT_MAX_CONNECTIONS_PER_ROUTE = 2; // Per RFC 2616 sec 8.1.4 michael@0: michael@0: private final ConcurrentHashMap maxPerHostMap; michael@0: michael@0: private volatile int defaultMax; michael@0: michael@0: public ConnPerRouteBean(int defaultMax) { michael@0: super(); michael@0: this.maxPerHostMap = new ConcurrentHashMap(); michael@0: setDefaultMaxPerRoute(defaultMax); michael@0: } michael@0: michael@0: public ConnPerRouteBean() { michael@0: this(DEFAULT_MAX_CONNECTIONS_PER_ROUTE); michael@0: } michael@0: michael@0: /** michael@0: * @deprecated Use {@link #getDefaultMaxPerRoute()} instead michael@0: */ michael@0: @Deprecated michael@0: public int getDefaultMax() { michael@0: return this.defaultMax; michael@0: } michael@0: michael@0: /** michael@0: * @since 4.1 michael@0: */ michael@0: public int getDefaultMaxPerRoute() { michael@0: return this.defaultMax; michael@0: } michael@0: michael@0: public void setDefaultMaxPerRoute(int max) { michael@0: if (max < 1) { michael@0: throw new IllegalArgumentException michael@0: ("The maximum must be greater than 0."); michael@0: } michael@0: this.defaultMax = max; michael@0: } michael@0: michael@0: public void setMaxForRoute(final HttpRoute route, int max) { michael@0: if (route == null) { michael@0: throw new IllegalArgumentException michael@0: ("HTTP route may not be null."); michael@0: } michael@0: if (max < 1) { michael@0: throw new IllegalArgumentException michael@0: ("The maximum must be greater than 0."); michael@0: } michael@0: this.maxPerHostMap.put(route, Integer.valueOf(max)); michael@0: } michael@0: michael@0: public int getMaxForRoute(final HttpRoute route) { michael@0: if (route == null) { michael@0: throw new IllegalArgumentException michael@0: ("HTTP route may not be null."); michael@0: } michael@0: Integer max = this.maxPerHostMap.get(route); michael@0: if (max != null) { michael@0: return max.intValue(); michael@0: } else { michael@0: return this.defaultMax; michael@0: } michael@0: } michael@0: michael@0: public void setMaxForRoutes(final Map map) { michael@0: if (map == null) { michael@0: return; michael@0: } michael@0: this.maxPerHostMap.clear(); michael@0: this.maxPerHostMap.putAll(map); michael@0: } michael@0: michael@0: @Override michael@0: public String toString() { michael@0: return this.maxPerHostMap.toString(); michael@0: } michael@0: michael@0: }