View Javadoc

1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/params/HttpConnectionManagerParams.java,v 1.9 2004/09/13 16:25:20 olegk Exp $
3    * $Revision: 354155 $
4    * $Date: 2005-12-05 15:18:10 -0500 (Mon, 05 Dec 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.commons.httpclient.params;
31  
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  import org.apache.commons.httpclient.HostConfiguration;
36  import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
37  
38  /***
39   * This class represents a collection of HTTP protocol parameters applicable to 
40   * {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection managers}. 
41   * Protocol parameters may be linked together to form a hierarchy. If a particular 
42   * parameter value has not been explicitly defined in the collection itself, its 
43   * value will be drawn from the parent collection of parameters.
44   * 
45   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
46   * @author Michael Becke
47   * 
48   * @version $Revision: 354155 $
49   * 
50   * @since 3.0
51   */
52  public class HttpConnectionManagerParams extends HttpConnectionParams {
53  
54      /*** 
55       * Defines the maximum number of connections allowed per host configuration. 
56       * These values only apply to the number of connections from a particular instance 
57       * of HttpConnectionManager.
58       * <p>
59       * This parameter expects a value of type {@link java.util.Map}.  The value
60       * should map instances of {@link org.apache.commons.httpclient.HostConfiguration}
61       * to {@link Integer integers}.  The default value can be specified using
62       * {@link org.apache.commons.httpclient.HostConfiguration#ANY_HOST_CONFIGURATION}.
63       * </p>
64       */
65      public static final String MAX_HOST_CONNECTIONS = "http.connection-manager.max-per-host";
66  
67      /*** 
68       * Defines the maximum number of connections allowed overall. This value only applies
69       * to the number of connections from a particular instance of HttpConnectionManager.
70       * <p>
71       * This parameter expects a value of type {@link Integer}.
72       * </p>
73       */
74      public static final String MAX_TOTAL_CONNECTIONS = "http.connection-manager.max-total";
75      
76      /***
77       * Sets the default maximum number of connections allowed for a given
78       * host config.
79       *
80       * @param maxHostConnections The default maximum.
81       * 
82       * @see #MAX_HOST_CONNECTIONS
83       */
84      public void setDefaultMaxConnectionsPerHost(int maxHostConnections) {
85          setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, maxHostConnections);
86      }
87  
88      /***
89       * Sets the maximum number of connections to be used for the given host config.
90       * 
91       * @param hostConfiguration The host config to set the maximum for.  Use 
92       * {@link HostConfiguration#ANY_HOST_CONFIGURATION} to configure the default value
93       * per host.
94       * @param maxHostConnections The maximum number of connections, <code>> 0</code>
95       * 
96       * @see #MAX_HOST_CONNECTIONS
97       */
98      public void setMaxConnectionsPerHost(
99          HostConfiguration hostConfiguration,
100         int maxHostConnections) {
101         
102         if (maxHostConnections <= 0) {
103             throw new IllegalArgumentException("maxHostConnections must be greater than 0");
104         }
105         
106         Map currentValues = (Map) getParameter(MAX_HOST_CONNECTIONS);
107         // param values are meant to be immutable so we'll make a copy
108         // to modify
109         Map newValues = null;
110         if (currentValues == null) {
111             newValues = new HashMap();
112         } else {
113             newValues = new HashMap(currentValues);
114         }
115         newValues.put(hostConfiguration, new Integer(maxHostConnections));
116         setParameter(MAX_HOST_CONNECTIONS, newValues);
117     }
118     
119     /***
120      * Gets the default maximum number of connections allowed for a given
121      * host config.
122      *
123      * @return The default maximum.
124      * 
125      * @see #MAX_HOST_CONNECTIONS
126      */
127     public int getDefaultMaxConnectionsPerHost() {
128         return getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION);
129     }
130 
131     /***
132      * Gets the maximum number of connections to be used for a particular host config.  If
133      * the value has not been specified for the given host the default value will be
134      * returned.
135      * 
136      * @param hostConfiguration The host config.
137      * @return The maximum number of connections to be used for the given host config.
138      * 
139      * @see #MAX_HOST_CONNECTIONS
140      */
141     public int getMaxConnectionsPerHost(HostConfiguration hostConfiguration) {
142         
143         Map m = (Map) getParameter(MAX_HOST_CONNECTIONS);
144         if (m == null) {
145             // MAX_HOST_CONNECTIONS have not been configured, using the default value
146             return MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS;
147         } else {
148             Integer max = (Integer) m.get(hostConfiguration);
149             if (max == null && hostConfiguration != HostConfiguration.ANY_HOST_CONFIGURATION) {
150                 // the value has not been configured specifically for this host config,
151                 // use the default value
152                 return getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION);
153             } else {
154                 return (
155                         max == null 
156                         ? MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS 
157                         : max.intValue()
158                     );
159             }
160         }
161     }
162 
163     /***
164      * Sets the maximum number of connections allowed.
165      *
166      * @param maxTotalConnections The maximum number of connections allowed.
167      * 
168      * @see #MAX_TOTAL_CONNECTIONS
169      */
170     public void setMaxTotalConnections(int maxTotalConnections) {
171         setIntParameter(
172             HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
173             maxTotalConnections);
174     }
175 
176     /***
177      * Gets the maximum number of connections allowed.
178      *
179      * @return The maximum number of connections allowed.
180      * 
181      * @see #MAX_TOTAL_CONNECTIONS
182      */
183     public int getMaxTotalConnections() {
184         return getIntParameter(
185             HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
186             MultiThreadedHttpConnectionManager.DEFAULT_MAX_TOTAL_CONNECTIONS);
187     }
188 
189 }