Home | History | Annotate | Download | only in params
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/params/ConnManagerParams.java $
      3  * $Revision: 658785 $
      4  * $Date: 2008-05-21 10:47:40 -0700 (Wed, 21 May 2008) $
      5  *
      6  * ====================================================================
      7  *
      8  *  Licensed to the Apache Software Foundation (ASF) under one or more
      9  *  contributor license agreements.  See the NOTICE file distributed with
     10  *  this work for additional information regarding copyright ownership.
     11  *  The ASF licenses this file to You under the Apache License, Version 2.0
     12  *  (the "License"); you may not use this file except in compliance with
     13  *  the License.  You may obtain a copy of the License at
     14  *
     15  *      http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  *  Unless required by applicable law or agreed to in writing, software
     18  *  distributed under the License is distributed on an "AS IS" BASIS,
     19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     20  *  See the License for the specific language governing permissions and
     21  *  limitations under the License.
     22  * ====================================================================
     23  *
     24  * This software consists of voluntary contributions made by many
     25  * individuals on behalf of the Apache Software Foundation.  For more
     26  * information on the Apache Software Foundation, please see
     27  * <http://www.apache.org/>.
     28  *
     29  */
     30 
     31 package org.apache.http.conn.params;
     32 
     33 import org.apache.http.conn.routing.HttpRoute;
     34 import org.apache.http.params.HttpParams;
     35 
     36 /**
     37  * This class represents a collection of HTTP protocol parameters applicable
     38  * to client-side
     39  * {@link org.apache.http.conn.ClientConnectionManager connection managers}.
     40  *
     41  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     42  * @author Michael Becke
     43  *
     44  * @version $Revision: 658785 $
     45  *
     46  * @since 4.0
     47  *
     48  * @see ConnManagerPNames
     49  */
     50 public final class ConnManagerParams implements ConnManagerPNames {
     51 
     52     /** The default maximum number of connections allowed overall */
     53     public static final int DEFAULT_MAX_TOTAL_CONNECTIONS = 20;
     54 
     55     /**
     56      * Returns the timeout in milliseconds used when retrieving a
     57      * {@link org.apache.http.conn.ManagedClientConnection} from the
     58      * {@link org.apache.http.conn.ClientConnectionManager}.
     59      *
     60      * @return timeout in milliseconds.
     61      */
     62     public static long getTimeout(final HttpParams params) {
     63         if (params == null) {
     64             throw new IllegalArgumentException("HTTP parameters may not be null");
     65         }
     66         return params.getLongParameter(TIMEOUT, 0);
     67     }
     68 
     69     /**
     70      * Sets the timeout in milliseconds used when retrieving a
     71      * {@link org.apache.http.conn.ManagedClientConnection} from the
     72      * {@link org.apache.http.conn.ClientConnectionManager}.
     73      *
     74      * @param timeout the timeout in milliseconds
     75      */
     76     public static void setTimeout(final HttpParams params, long timeout) {
     77         if (params == null) {
     78             throw new IllegalArgumentException("HTTP parameters may not be null");
     79         }
     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(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      * @see ConnManagerPNames#MAX_CONNECTIONS_PER_ROUTE
    100      */
    101     public static void setMaxConnectionsPerRoute(final HttpParams params,
    102                                                 final ConnPerRoute connPerRoute) {
    103         if (params == null) {
    104             throw new IllegalArgumentException
    105                 ("HTTP parameters must not be null.");
    106         }
    107         params.setParameter(MAX_CONNECTIONS_PER_ROUTE, connPerRoute);
    108     }
    109 
    110     /**
    111      * Returns lookup interface for maximum number of connections allowed per route.
    112      *
    113      * @param params HTTP parameters
    114      *
    115      * @return lookup interface for maximum number of connections allowed per route.
    116      *
    117      * @see ConnManagerPNames#MAX_CONNECTIONS_PER_ROUTE
    118      */
    119     public static ConnPerRoute getMaxConnectionsPerRoute(final HttpParams params) {
    120         if (params == null) {
    121             throw new IllegalArgumentException
    122                 ("HTTP parameters must not be null.");
    123         }
    124         ConnPerRoute connPerRoute = (ConnPerRoute) params.getParameter(MAX_CONNECTIONS_PER_ROUTE);
    125         if (connPerRoute == null) {
    126             connPerRoute = DEFAULT_CONN_PER_ROUTE;
    127         }
    128         return connPerRoute;
    129     }
    130 
    131 
    132     /**
    133      * Sets the maximum number of connections allowed.
    134      *
    135      * @param params HTTP parameters
    136      * @param maxTotalConnections The maximum number of connections allowed.
    137      *
    138      * @see ConnManagerPNames#MAX_TOTAL_CONNECTIONS
    139      */
    140     public static void setMaxTotalConnections(
    141             final HttpParams params,
    142             int maxTotalConnections) {
    143         if (params == null) {
    144             throw new IllegalArgumentException
    145                 ("HTTP parameters must not be null.");
    146         }
    147         params.setIntParameter(MAX_TOTAL_CONNECTIONS, maxTotalConnections);
    148     }
    149 
    150     /**
    151      * Gets the maximum number of connections allowed.
    152      *
    153      * @param params HTTP parameters
    154      *
    155      * @return The maximum number of connections allowed.
    156      *
    157      * @see ConnManagerPNames#MAX_TOTAL_CONNECTIONS
    158      */
    159     public static int getMaxTotalConnections(
    160             final HttpParams params) {
    161         if (params == null) {
    162             throw new IllegalArgumentException
    163                 ("HTTP parameters must not be null.");
    164         }
    165         return params.getIntParameter(MAX_TOTAL_CONNECTIONS, DEFAULT_MAX_TOTAL_CONNECTIONS);
    166     }
    167 
    168 
    169 }
    170