View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with 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,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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.Contract;
33  import org.apache.http.annotation.ThreadingBehavior;
34  import org.apache.http.conn.routing.HttpRoute;
35  import org.apache.http.util.Args;
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 org.apache.http.pool.ConnPoolControl}
46   */
47  @Deprecated
48  @Contract(threading = ThreadingBehavior.SAFE)
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(final 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(final int max) {
80          Args.positive(max, "Default max per route");
81          this.defaultMax = max;
82      }
83  
84      public void setMaxForRoute(final HttpRoute route, final int max) {
85          Args.notNull(route, "HTTP route");
86          Args.positive(max, "Max per route");
87          this.maxPerHostMap.put(route, Integer.valueOf(max));
88      }
89  
90      @Override
91      public int getMaxForRoute(final HttpRoute route) {
92          Args.notNull(route, "HTTP route");
93          final Integer max = this.maxPerHostMap.get(route);
94          if (max != null) {
95              return max.intValue();
96          } else {
97              return this.defaultMax;
98          }
99      }
100 
101     public void setMaxForRoutes(final Map<HttpRoute, Integer> map) {
102         if (map == null) {
103             return;
104         }
105         this.maxPerHostMap.clear();
106         this.maxPerHostMap.putAll(map);
107     }
108 
109     @Override
110     public String toString() {
111         return this.maxPerHostMap.toString();
112     }
113 
114 }