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 org.apache.http.annotation.Immutable;
30  import org.apache.http.conn.routing.HttpRoute;
31  import org.apache.http.params.HttpParams;
32  import org.apache.http.util.Args;
33  
34  /**
35   * An adaptor for manipulating HTTP connection management
36   * parameters in {@link HttpParams}.
37   *
38   * @since 4.0
39   *
40   * @see ConnManagerPNames
41   *
42   * @deprecated (4.1) use configuration methods of the specific connection manager implementation.
43   */
44  @Deprecated
45  @Immutable
46  public final class ConnManagerParams implements ConnManagerPNames {
47  
48      /** The default maximum number of connections allowed overall */
49      public static final int DEFAULT_MAX_TOTAL_CONNECTIONS = 20;
50  
51      /**
52       * Returns the timeout in milliseconds used when retrieving a
53       * {@link org.apache.http.conn.ManagedClientConnection} from the
54       * {@link org.apache.http.conn.ClientConnectionManager}.
55       *
56       * @return timeout in milliseconds.
57       *
58       * @deprecated (4.1)  use {@link
59       *   org.apache.http.params.HttpConnectionParams#getConnectionTimeout(HttpParams)}
60       */
61      @Deprecated
62      public static long getTimeout(final HttpParams params) {
63          Args.notNull(params, "HTTP parameters");
64          return params.getLongParameter(TIMEOUT, 0);
65      }
66  
67      /**
68       * Sets the timeout in milliseconds used when retrieving a
69       * {@link org.apache.http.conn.ManagedClientConnection} from the
70       * {@link org.apache.http.conn.ClientConnectionManager}.
71       *
72       * @param timeout the timeout in milliseconds
73       *
74       * @deprecated (4.1)  use {@link
75       *   org.apache.http.params.HttpConnectionParams#setConnectionTimeout(HttpParams, int)}
76       */
77      @Deprecated
78      public static void setTimeout(final HttpParams params, final long timeout) {
79          Args.notNull(params, "HTTP parameters");
80          params.setLongParameter(TIMEOUT, timeout);
81      }
82  
83      /** The default maximum number of connections allowed per host */
84      private static final ConnPerRoute DEFAULT_CONN_PER_ROUTE = new ConnPerRoute() {
85  
86          public int getMaxForRoute(final HttpRoute route) {
87              return ConnPerRouteBean.DEFAULT_MAX_CONNECTIONS_PER_ROUTE;
88          }
89  
90      };
91  
92      /**
93       * Sets lookup interface for maximum number of connections allowed per route.
94       *
95       * @param params HTTP parameters
96       * @param connPerRoute lookup interface for maximum number of connections allowed
97       *        per route
98       */
99      public static void setMaxConnectionsPerRoute(final HttpParams params,
100                                                 final ConnPerRoute connPerRoute) {
101         Args.notNull(params, "HTTP parameters");
102         params.setParameter(MAX_CONNECTIONS_PER_ROUTE, connPerRoute);
103     }
104 
105     /**
106      * Returns lookup interface for maximum number of connections allowed per route.
107      *
108      * @param params HTTP parameters
109      *
110      * @return lookup interface for maximum number of connections allowed per route.
111      */
112     public static ConnPerRoute getMaxConnectionsPerRoute(final HttpParams params) {
113         Args.notNull(params, "HTTP parameters");
114         ConnPerRoute connPerRoute = (ConnPerRoute) params.getParameter(MAX_CONNECTIONS_PER_ROUTE);
115         if (connPerRoute == null) {
116             connPerRoute = DEFAULT_CONN_PER_ROUTE;
117         }
118         return connPerRoute;
119     }
120 
121     /**
122      * Sets the maximum number of connections allowed.
123      *
124      * @param params HTTP parameters
125      * @param maxTotalConnections The maximum number of connections allowed.
126      */
127     public static void setMaxTotalConnections(
128             final HttpParams params,
129             final int maxTotalConnections) {
130         Args.notNull(params, "HTTP parameters");
131         params.setIntParameter(MAX_TOTAL_CONNECTIONS, maxTotalConnections);
132     }
133 
134     /**
135      * Gets the maximum number of connections allowed.
136      *
137      * @param params HTTP parameters
138      *
139      * @return The maximum number of connections allowed.
140      */
141     public static int getMaxTotalConnections(
142             final HttpParams params) {
143         Args.notNull(params, "HTTP parameters");
144         return params.getIntParameter(MAX_TOTAL_CONNECTIONS, DEFAULT_MAX_TOTAL_CONNECTIONS);
145     }
146 
147 }