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