1 /* 2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/params/ConnPerRouteBean.java $ 3 * $Revision: 652947 $ 4 * $Date: 2008-05-02 16:15:40 -0700 (Fri, 02 May 2008) $ 5 * 6 * ==================================================================== 7 * 8 * Licensed to the Apache Software Foundation (ASF) under one or more 9 * contributor license agreements. See the NOTICE file distributed with 10 * this work for additional information regarding copyright ownership. 11 * The ASF licenses this file to You under the Apache License, Version 2.0 12 * (the "License"); you may not use this file except in compliance with 13 * 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, software 18 * distributed under the License is distributed on an "AS IS" BASIS, 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 * See the License for the specific language governing permissions and 21 * limitations under the License. 22 * ==================================================================== 23 * 24 * This software consists of voluntary contributions made by many 25 * individuals on behalf of the Apache Software Foundation. For more 26 * information on the Apache Software Foundation, please see 27 * <http://www.apache.org/>. 28 * 29 */ 30 31 package org.apache.http.conn.params; 32 33 import java.util.HashMap; 34 import java.util.Map; 35 36 import org.apache.http.conn.routing.HttpRoute; 37 38 /** 39 * This class maintains a map of HTTP routes to maximum number of connections allowed 40 * for those routes. This class can be used by pooling 41 * {@link org.apache.http.conn.ClientConnectionManager connection managers} for 42 * a fine-grained control of connections on a per route basis. 43 * 44 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> 45 * 46 * @version $Revision: 652947 $ 47 * 48 * @since 4.0 49 */ 50 public final class ConnPerRouteBean implements ConnPerRoute { 51 52 /** The default maximum number of connections allowed per host */ 53 public static final int DEFAULT_MAX_CONNECTIONS_PER_ROUTE = 2; // Per RFC 2616 sec 8.1.4 54 55 private final Map<HttpRoute, Integer> maxPerHostMap; 56 57 private int defaultMax; 58 ConnPerRouteBean(int defaultMax)59 public ConnPerRouteBean(int defaultMax) { 60 super(); 61 this.maxPerHostMap = new HashMap<HttpRoute, Integer>(); 62 setDefaultMaxPerRoute(defaultMax); 63 } 64 ConnPerRouteBean()65 public ConnPerRouteBean() { 66 this(DEFAULT_MAX_CONNECTIONS_PER_ROUTE); 67 } 68 getDefaultMax()69 public int getDefaultMax() { 70 return this.defaultMax; 71 } 72 setDefaultMaxPerRoute(int max)73 public void setDefaultMaxPerRoute(int max) { 74 if (max < 1) { 75 throw new IllegalArgumentException 76 ("The maximum must be greater than 0."); 77 } 78 this.defaultMax = max; 79 } 80 setMaxForRoute(final HttpRoute route, int max)81 public void setMaxForRoute(final HttpRoute route, int max) { 82 if (route == null) { 83 throw new IllegalArgumentException 84 ("HTTP route may not be null."); 85 } 86 if (max < 1) { 87 throw new IllegalArgumentException 88 ("The maximum must be greater than 0."); 89 } 90 this.maxPerHostMap.put(route, Integer.valueOf(max)); 91 } 92 getMaxForRoute(final HttpRoute route)93 public int getMaxForRoute(final HttpRoute route) { 94 if (route == null) { 95 throw new IllegalArgumentException 96 ("HTTP route may not be null."); 97 } 98 Integer max = this.maxPerHostMap.get(route); 99 if (max != null) { 100 return max.intValue(); 101 } else { 102 return this.defaultMax; 103 } 104 } 105 setMaxForRoutes(final Map<HttpRoute, Integer> map)106 public void setMaxForRoutes(final Map<HttpRoute, Integer> map) { 107 if (map == null) { 108 return; 109 } 110 this.maxPerHostMap.clear(); 111 this.maxPerHostMap.putAll(map); 112 } 113 114 } 115