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