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          @Override
87          public int getMaxForRoute(final HttpRoute route) {
88              return ConnPerRouteBean.DEFAULT_MAX_CONNECTIONS_PER_ROUTE;
89          }
90  
91      };
92  
93      /**
94       * Sets lookup interface for maximum number of connections allowed per route.
95       *
96       * @param params HTTP parameters
97       * @param connPerRoute lookup interface for maximum number of connections allowed
98       *        per route
99       */
100     public static void setMaxConnectionsPerRoute(final HttpParams params,
101                                                 final ConnPerRoute connPerRoute) {
102         Args.notNull(params, "HTTP parameters");
103         params.setParameter(MAX_CONNECTIONS_PER_ROUTE, connPerRoute);
104     }
105 
106     /**
107      * Returns lookup interface for maximum number of connections allowed per route.
108      *
109      * @param params HTTP parameters
110      *
111      * @return lookup interface for maximum number of connections allowed per route.
112      */
113     public static ConnPerRoute getMaxConnectionsPerRoute(final HttpParams params) {
114         Args.notNull(params, "HTTP parameters");
115         ConnPerRoute connPerRoute = (ConnPerRoute) params.getParameter(MAX_CONNECTIONS_PER_ROUTE);
116         if (connPerRoute == null) {
117             connPerRoute = DEFAULT_CONN_PER_ROUTE;
118         }
119         return connPerRoute;
120     }
121 
122     /**
123      * Sets the maximum number of connections allowed.
124      *
125      * @param params HTTP parameters
126      * @param maxTotalConnections The maximum number of connections allowed.
127      */
128     public static void setMaxTotalConnections(
129             final HttpParams params,
130             final int maxTotalConnections) {
131         Args.notNull(params, "HTTP parameters");
132         params.setIntParameter(MAX_TOTAL_CONNECTIONS, maxTotalConnections);
133     }
134 
135     /**
136      * Gets the maximum number of connections allowed.
137      *
138      * @param params HTTP parameters
139      *
140      * @return The maximum number of connections allowed.
141      */
142     public static int getMaxTotalConnections(
143             final HttpParams params) {
144         Args.notNull(params, "HTTP parameters");
145         return params.getIntParameter(MAX_TOTAL_CONNECTIONS, DEFAULT_MAX_TOTAL_CONNECTIONS);
146     }
147 
148 }