View Javadoc

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 org.apache.http.conn.params;
28  
29  import java.util.Map;
30  import java.util.concurrent.ConcurrentHashMap;
31  
32  import org.apache.http.annotation.ThreadSafe;
33  
34  import org.apache.http.conn.routing.HttpRoute;
35  import org.apache.http.pool.ConnPoolControl;
36  
37  /**
38   * This class maintains a map of HTTP routes to maximum number of connections allowed
39   * for those routes. This class can be used by pooling
40   * {@link org.apache.http.conn.ClientConnectionManager connection managers} for
41   * a fine-grained control of connections on a per route basis.
42   *
43   * @since 4.0
44   *
45   * @deprecated (4.2)  use {@link ConnPoolControl}
46   */
47  @Deprecated 
48  @ThreadSafe
49  public final class ConnPerRouteBean implements ConnPerRoute {
50  
51      /** The default maximum number of connections allowed per host */
52      public static final int DEFAULT_MAX_CONNECTIONS_PER_ROUTE = 2;   // Per RFC 2616 sec 8.1.4
53  
54      private final ConcurrentHashMap<HttpRoute, Integer> maxPerHostMap;
55  
56      private volatile int defaultMax;
57  
58      public ConnPerRouteBean(int defaultMax) {
59          super();
60          this.maxPerHostMap = new ConcurrentHashMap<HttpRoute, Integer>();
61          setDefaultMaxPerRoute(defaultMax);
62      }
63  
64      public ConnPerRouteBean() {
65          this(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
66      }
67  
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 }