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.ThreadSafe;
33  import org.apache.http.conn.routing.HttpRoute;
34  import org.apache.http.util.Args;
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 org.apache.http.conn.ClientConnectionManager connection managers} for
40   * a fine-grained control of connections on a per route basis.
41   *
42   * @since 4.0
43   *
44   * @deprecated (4.2)  use {@link org.apache.http.pool.ConnPoolControl}
45   */
46  @Deprecated
47  @ThreadSafe
48  public final class ConnPerRouteBean implements ConnPerRoute {
49  
50      /** The default maximum number of connections allowed per host */
51      public static final int DEFAULT_MAX_CONNECTIONS_PER_ROUTE = 2;   // Per RFC 2616 sec 8.1.4
52  
53      private final ConcurrentHashMap<HttpRoute, Integer> maxPerHostMap;
54  
55      private volatile int defaultMax;
56  
57      public ConnPerRouteBean(final int defaultMax) {
58          super();
59          this.maxPerHostMap = new ConcurrentHashMap<HttpRoute, Integer>();
60          setDefaultMaxPerRoute(defaultMax);
61      }
62  
63      public ConnPerRouteBean() {
64          this(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
65      }
66  
67      public int getDefaultMax() {
68          return this.defaultMax;
69      }
70  
71      /**
72       * @since 4.1
73       */
74      public int getDefaultMaxPerRoute() {
75          return this.defaultMax;
76      }
77  
78      public void setDefaultMaxPerRoute(final int max) {
79          Args.positive(max, "Default max per route");
80          this.defaultMax = max;
81      }
82  
83      public void setMaxForRoute(final HttpRoute route, final int max) {
84          Args.notNull(route, "HTTP route");
85          Args.positive(max, "Max per route");
86          this.maxPerHostMap.put(route, Integer.valueOf(max));
87      }
88  
89      public int getMaxForRoute(final HttpRoute route) {
90          Args.notNull(route, "HTTP route");
91          final Integer max = this.maxPerHostMap.get(route);
92          if (max != null) {
93              return max.intValue();
94          } else {
95              return this.defaultMax;
96          }
97      }
98  
99      public void setMaxForRoutes(final Map<HttpRoute, Integer> map) {
100         if (map == null) {
101             return;
102         }
103         this.maxPerHostMap.clear();
104         this.maxPerHostMap.putAll(map);
105     }
106 
107     @Override
108     public String toString() {
109         return this.maxPerHostMap.toString();
110     }
111 
112 }