Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed to the Apache Software Foundation (ASF) under one or more |
michael@0 | 5 | * contributor license agreements. See the NOTICE file distributed with |
michael@0 | 6 | * this work for additional information regarding copyright ownership. |
michael@0 | 7 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
michael@0 | 8 | * (the "License"); you may not use this file except in compliance with |
michael@0 | 9 | * the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 16 | * See the License for the specific language governing permissions and |
michael@0 | 17 | * limitations under the License. |
michael@0 | 18 | * ==================================================================== |
michael@0 | 19 | * |
michael@0 | 20 | * This software consists of voluntary contributions made by many |
michael@0 | 21 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 22 | * information on the Apache Software Foundation, please see |
michael@0 | 23 | * <http://www.apache.org/>. |
michael@0 | 24 | * |
michael@0 | 25 | */ |
michael@0 | 26 | |
michael@0 | 27 | package ch.boye.httpclientandroidlib.conn.params; |
michael@0 | 28 | |
michael@0 | 29 | import java.util.Map; |
michael@0 | 30 | import java.util.concurrent.ConcurrentHashMap; |
michael@0 | 31 | |
michael@0 | 32 | import ch.boye.httpclientandroidlib.annotation.ThreadSafe; |
michael@0 | 33 | |
michael@0 | 34 | import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; |
michael@0 | 35 | |
michael@0 | 36 | /** |
michael@0 | 37 | * This class maintains a map of HTTP routes to maximum number of connections allowed |
michael@0 | 38 | * for those routes. This class can be used by pooling |
michael@0 | 39 | * {@link ch.boye.httpclientandroidlib.conn.ClientConnectionManager connection managers} for |
michael@0 | 40 | * a fine-grained control of connections on a per route basis. |
michael@0 | 41 | * |
michael@0 | 42 | * @since 4.0 |
michael@0 | 43 | */ |
michael@0 | 44 | @ThreadSafe |
michael@0 | 45 | public final class ConnPerRouteBean implements ConnPerRoute { |
michael@0 | 46 | |
michael@0 | 47 | /** The default maximum number of connections allowed per host */ |
michael@0 | 48 | public static final int DEFAULT_MAX_CONNECTIONS_PER_ROUTE = 2; // Per RFC 2616 sec 8.1.4 |
michael@0 | 49 | |
michael@0 | 50 | private final ConcurrentHashMap<HttpRoute, Integer> maxPerHostMap; |
michael@0 | 51 | |
michael@0 | 52 | private volatile int defaultMax; |
michael@0 | 53 | |
michael@0 | 54 | public ConnPerRouteBean(int defaultMax) { |
michael@0 | 55 | super(); |
michael@0 | 56 | this.maxPerHostMap = new ConcurrentHashMap<HttpRoute, Integer>(); |
michael@0 | 57 | setDefaultMaxPerRoute(defaultMax); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | public ConnPerRouteBean() { |
michael@0 | 61 | this(DEFAULT_MAX_CONNECTIONS_PER_ROUTE); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | /** |
michael@0 | 65 | * @deprecated Use {@link #getDefaultMaxPerRoute()} instead |
michael@0 | 66 | */ |
michael@0 | 67 | @Deprecated |
michael@0 | 68 | public int getDefaultMax() { |
michael@0 | 69 | return this.defaultMax; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | /** |
michael@0 | 73 | * @since 4.1 |
michael@0 | 74 | */ |
michael@0 | 75 | public int getDefaultMaxPerRoute() { |
michael@0 | 76 | return this.defaultMax; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | public void setDefaultMaxPerRoute(int max) { |
michael@0 | 80 | if (max < 1) { |
michael@0 | 81 | throw new IllegalArgumentException |
michael@0 | 82 | ("The maximum must be greater than 0."); |
michael@0 | 83 | } |
michael@0 | 84 | this.defaultMax = max; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | public void setMaxForRoute(final HttpRoute route, int max) { |
michael@0 | 88 | if (route == null) { |
michael@0 | 89 | throw new IllegalArgumentException |
michael@0 | 90 | ("HTTP route may not be null."); |
michael@0 | 91 | } |
michael@0 | 92 | if (max < 1) { |
michael@0 | 93 | throw new IllegalArgumentException |
michael@0 | 94 | ("The maximum must be greater than 0."); |
michael@0 | 95 | } |
michael@0 | 96 | this.maxPerHostMap.put(route, Integer.valueOf(max)); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | public int getMaxForRoute(final HttpRoute route) { |
michael@0 | 100 | if (route == null) { |
michael@0 | 101 | throw new IllegalArgumentException |
michael@0 | 102 | ("HTTP route may not be null."); |
michael@0 | 103 | } |
michael@0 | 104 | Integer max = this.maxPerHostMap.get(route); |
michael@0 | 105 | if (max != null) { |
michael@0 | 106 | return max.intValue(); |
michael@0 | 107 | } else { |
michael@0 | 108 | return this.defaultMax; |
michael@0 | 109 | } |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | public void setMaxForRoutes(final Map<HttpRoute, Integer> map) { |
michael@0 | 113 | if (map == null) { |
michael@0 | 114 | return; |
michael@0 | 115 | } |
michael@0 | 116 | this.maxPerHostMap.clear(); |
michael@0 | 117 | this.maxPerHostMap.putAll(map); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | @Override |
michael@0 | 121 | public String toString() { |
michael@0 | 122 | return this.maxPerHostMap.toString(); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | } |