View Javadoc

1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/java/org/apache/commons/httpclient/params/HttpConnectionManagerParams.java $
3    * $Revision$
4    * $Date$
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.commons.httpclient.params;
32  
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  import org.apache.commons.httpclient.HostConfiguration;
37  import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
38  
39  /***
40   * This class represents a collection of HTTP protocol parameters applicable to 
41   * {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection managers}. 
42   * Protocol parameters may be linked together to form a hierarchy. If a particular 
43   * parameter value has not been explicitly defined in the collection itself, its 
44   * value will be drawn from the parent collection of parameters.
45   * 
46   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
47   * @author Michael Becke
48   * 
49   * @version $Revision$
50   * 
51   * @since 3.0
52   */
53  public class HttpConnectionManagerParams extends HttpConnectionParams {
54  
55      /*** 
56       * Defines the maximum number of connections allowed per host configuration. 
57       * These values only apply to the number of connections from a particular instance 
58       * of HttpConnectionManager.
59       * <p>
60       * This parameter expects a value of type {@link java.util.Map}.  The value
61       * should map instances of {@link org.apache.commons.httpclient.HostConfiguration}
62       * to {@link Integer integers}.  The default value can be specified using
63       * {@link org.apache.commons.httpclient.HostConfiguration#ANY_HOST_CONFIGURATION}.
64       * </p>
65       */
66      public static final String MAX_HOST_CONNECTIONS = "http.connection-manager.max-per-host";
67  
68      /*** 
69       * Defines the maximum number of connections allowed overall. This value only applies
70       * to the number of connections from a particular instance of HttpConnectionManager.
71       * <p>
72       * This parameter expects a value of type {@link Integer}.
73       * </p>
74       */
75      public static final String MAX_TOTAL_CONNECTIONS = "http.connection-manager.max-total";
76      
77      /***
78       * Sets the default maximum number of connections allowed for a given
79       * host config.
80       *
81       * @param maxHostConnections The default maximum.
82       * 
83       * @see #MAX_HOST_CONNECTIONS
84       */
85      public void setDefaultMaxConnectionsPerHost(int maxHostConnections) {
86          setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, maxHostConnections);
87      }
88  
89      /***
90       * Sets the maximum number of connections to be used for the given host config.
91       * 
92       * @param hostConfiguration The host config to set the maximum for.  Use 
93       * {@link HostConfiguration#ANY_HOST_CONFIGURATION} to configure the default value
94       * per host.
95       * @param maxHostConnections The maximum number of connections, <code>> 0</code>
96       * 
97       * @see #MAX_HOST_CONNECTIONS
98       */
99      public void setMaxConnectionsPerHost(
100         HostConfiguration hostConfiguration,
101         int maxHostConnections) {
102         
103         if (maxHostConnections <= 0) {
104             throw new IllegalArgumentException("maxHostConnections must be greater than 0");
105         }
106         
107         Map currentValues = (Map) getParameter(MAX_HOST_CONNECTIONS);
108         // param values are meant to be immutable so we'll make a copy
109         // to modify
110         Map newValues = null;
111         if (currentValues == null) {
112             newValues = new HashMap();
113         } else {
114             newValues = new HashMap(currentValues);
115         }
116         newValues.put(hostConfiguration, new Integer(maxHostConnections));
117         setParameter(MAX_HOST_CONNECTIONS, newValues);
118     }
119     
120     /***
121      * Gets the default maximum number of connections allowed for a given
122      * host config.
123      *
124      * @return The default maximum.
125      * 
126      * @see #MAX_HOST_CONNECTIONS
127      */
128     public int getDefaultMaxConnectionsPerHost() {
129         return getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION);
130     }
131 
132     /***
133      * Gets the maximum number of connections to be used for a particular host config.  If
134      * the value has not been specified for the given host the default value will be
135      * returned.
136      * 
137      * @param hostConfiguration The host config.
138      * @return The maximum number of connections to be used for the given host config.
139      * 
140      * @see #MAX_HOST_CONNECTIONS
141      */
142     public int getMaxConnectionsPerHost(HostConfiguration hostConfiguration) {
143         
144         Map m = (Map) getParameter(MAX_HOST_CONNECTIONS);
145         if (m == null) {
146             // MAX_HOST_CONNECTIONS have not been configured, using the default value
147             return MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS;
148         } else {
149             Integer max = (Integer) m.get(hostConfiguration);
150             if (max == null && hostConfiguration != HostConfiguration.ANY_HOST_CONFIGURATION) {
151                 // the value has not been configured specifically for this host config,
152                 // use the default value
153                 return getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION);
154             } else {
155                 return (
156                         max == null 
157                         ? MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS 
158                         : max.intValue()
159                     );
160             }
161         }
162     }
163 
164     /***
165      * Sets the maximum number of connections allowed.
166      *
167      * @param maxTotalConnections The maximum number of connections allowed.
168      * 
169      * @see #MAX_TOTAL_CONNECTIONS
170      */
171     public void setMaxTotalConnections(int maxTotalConnections) {
172         setIntParameter(
173             HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
174             maxTotalConnections);
175     }
176 
177     /***
178      * Gets the maximum number of connections allowed.
179      *
180      * @return The maximum number of connections allowed.
181      * 
182      * @see #MAX_TOTAL_CONNECTIONS
183      */
184     public int getMaxTotalConnections() {
185         return getIntParameter(
186             HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
187             MultiThreadedHttpConnectionManager.DEFAULT_MAX_TOTAL_CONNECTIONS);
188     }
189 
190 }