Index: src/java/org/apache/commons/httpclient/HostConfiguration.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HostConfiguration.java,v retrieving revision 1.18 diff -u -r1.18 HostConfiguration.java --- src/java/org/apache/commons/httpclient/HostConfiguration.java 17 May 2004 21:46:03 -0000 1.18 +++ src/java/org/apache/commons/httpclient/HostConfiguration.java 6 Jul 2004 02:59:12 -0000 @@ -47,6 +47,12 @@ */ public class HostConfiguration implements Cloneable { + /** + * A host configuration that represents any host configuration. This value is + * meant to be used for configuring default host configuration values. + */ + public static final HostConfiguration ANY_HOST_CONFIGURATION = new HostConfiguration(); + /** The host to use. */ private String host; Index: src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java,v retrieving revision 1.41 diff -u -r1.41 MultiThreadedHttpConnectionManager.java --- src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java 5 Jul 2004 22:46:58 -0000 1.41 +++ src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java 6 Jul 2004 02:59:16 -0000 @@ -295,13 +295,11 @@ * @param maxHostConnections the number of connections allowed for each * hostConfiguration * - * @deprecated Use {@link HttpConnectionManagerParams#MAX_HOST_CONNECTIONS}, + * @deprecated Use {@link HttpConnectionManagerParams#setDefaultMaxConnectionsPerHost(int)}, * {@link HttpConnectionManager#getParams()}. */ public void setMaxConnectionsPerHost(int maxHostConnections) { - this.params.setIntParameter( - HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, - maxHostConnections); + this.params.setDefaultMaxConnectionsPerHost(maxHostConnections); } /** @@ -311,13 +309,11 @@ * @return The maximum number of connections allowed for a given * hostConfiguration. * - * @deprecated Use {@link HttpConnectionManagerParams#MAX_HOST_CONNECTIONS}, + * @deprecated Use {@link HttpConnectionManagerParams#getDefaultMaxConnectionsPerHost()}, * {@link HttpConnectionManager#getParams()}. */ public int getMaxConnectionsPerHost() { - return this.params.getIntParameter( - HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, - DEFAULT_MAX_HOST_CONNECTIONS); + return this.params.getDefaultMaxConnectionsPerHost(); } /** @@ -325,13 +321,11 @@ * * @param maxTotalConnections the maximum number of connections allowed * - * @deprecated Use {@link HttpConnectionManagerParams#MAX_TOTAL_CONNECTIONS}, + * @deprecated Use {@link HttpConnectionManagerParams#setMaxTotalConnections(int)(int)}, * {@link HttpConnectionManager#getParams()}. */ public void setMaxTotalConnections(int maxTotalConnections) { - this.params.setIntParameter( - HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, - maxTotalConnections); + this.params.getMaxTotalConnections(); } /** @@ -339,13 +333,11 @@ * * @return The maximum number of connections allowed * - * @deprecated Use {@link HttpConnectionManagerParams#MAX_TOTAL_CONNECTIONS}, + * @deprecated Use {@link HttpConnectionManagerParams#getMaxTotalConnections()}, * {@link HttpConnectionManager#getParams()}. */ public int getMaxTotalConnections() { - return this.params.getIntParameter( - HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, - DEFAULT_MAX_TOTAL_CONNECTIONS); + return this.params.getMaxTotalConnections(); } /** @@ -430,12 +422,8 @@ HttpConnection connection = null; - int maxHostConnections = this.params.getIntParameter( - HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, - DEFAULT_MAX_HOST_CONNECTIONS); - int maxTotalConnections = this.params.getIntParameter( - HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, - DEFAULT_MAX_TOTAL_CONNECTIONS); + int maxHostConnections = this.params.getMaxConnectionsPerHost(hostConfiguration); + int maxTotalConnections = this.params.getMaxTotalConnections(); synchronized (connectionPool) { Index: src/java/org/apache/commons/httpclient/params/HttpConnectionManagerParams.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/HttpConnectionManagerParams.java,v retrieving revision 1.6 diff -u -r1.6 HttpConnectionManagerParams.java --- src/java/org/apache/commons/httpclient/params/HttpConnectionManagerParams.java 13 May 2004 04:01:22 -0000 1.6 +++ src/java/org/apache/commons/httpclient/params/HttpConnectionManagerParams.java 6 Jul 2004 02:59:17 -0000 @@ -29,6 +29,13 @@ package org.apache.commons.httpclient.params; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.httpclient.HostConfiguration; +import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; + /** * This class represents a collection of HTTP protocol parameters applicable to * {@link org.apache.commons.httpclient.HttpConnectionManager HTTP connection managers}. @@ -37,6 +44,7 @@ * value will be drawn from the parent collection of parameters. * * @author Oleg Kalnichevski + * @author Michael Becke * * @version $Revision: 1.6 $ * @@ -45,19 +53,126 @@ public class HttpConnectionManagerParams extends HttpConnectionParams { /** - * Defines the maximum number of connections allowed per host. + * Defines the maximum number of connections allowed per host configuration. + * These values only apply to the number of connections from a particular instance + * of HttpConnectionManager. *

- * This parameter expects a value of type {@link Integer}. + * This parameter expects a value of type {@link java.util.Map}. The value + * should map instances of {@link org.apache.commons.httpclient.HostConfiguration} + * to {@link Integer integers). The default value can be specified using + * {@link org.apache.commons.httpclient.HostConfiguration#ANY_HOST_CONFIGURATION}. *

*/ public static String MAX_HOST_CONNECTIONS = "http.connection-manager.max-per-host"; /** - * Defines the maximum number of connections allowed overall. + * Defines the maximum number of connections allowed overall. This value only applies + * to the number of connections from a particular instance of HttpConnectionManager. *

* This parameter expects a value of type {@link Integer}. *

*/ public static String MAX_TOTAL_CONNECTIONS = "http.connection-manager.max-total"; + + /** + * Sets the default maximum number of connections allowed for a given + * host config. + * + * @param maxHostConnections The default maximum. + * + * @see #MAX_HOST_CONNECTIONS + */ + public void setDefaultMaxConnectionsPerHost(int maxHostConnections) { + setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, maxHostConnections); + } + + /** + * Sets the maximum number of connections to be used for the given host config. + * + * @param hostConfiguration The host config to set the maximum for. + * @param maxHostConnections The maximum number of connections, > 0 + * + * @see #MAX_HOST_CONNECTIONS + */ + public void setMaxConnectionsPerHost( + HostConfiguration hostConfiguration, + int maxHostConnections) { + + if (maxHostConnections <= 0) { + throw new IllegalArgumentException("maxHostConnections must be greater than 0"); + } + + Map currentValues = (Map) getParameter(MAX_HOST_CONNECTIONS); + // param values are meant to be immutable so we'll make a copy + // to modify + Map newValues = new HashMap(currentValues == null ? Collections.EMPTY_MAP : currentValues); + newValues.put(hostConfiguration, new Integer(maxHostConnections)); + setParameter(MAX_HOST_CONNECTIONS, newValues); + } + + /** + * Gets the default maximum number of connections allowed for a given + * host config. + * + * @return The default maximum. + * + * @see #MAX_HOST_CONNECTIONS + */ + public int getDefaultMaxConnectionsPerHost() { + return getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION); + } + + /** + * Gets the maximum number of connections to be used for a particular host config. If + * the value has not been specified for the given host the default value will be + * returned. + * + * @param hostConfiguration The host config. + * @return The maximum number of connections to be used for the given host config. + * + * @see #MAX_HOST_CONNECTIONS + */ + public int getMaxConnectionsPerHost(HostConfiguration hostConfiguration) { + + Map m = (Map) getParameter(MAX_HOST_CONNECTIONS); + Integer max = (Integer) m.get(hostConfiguration); + if (max == null && hostConfiguration != HostConfiguration.ANY_HOST_CONFIGURATION) { + // the value has not been configured specifically for this host config, + // use the default value + return getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION); + } else { + return ( + max == null + ? MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS + : max.intValue() + ); + } + } + + /** + * Sets the maximum number of connections allowed. + * + * @param maxTotalConnections The maximum number of connections allowed. + * + * @see #MAX_TOTAL_CONNECTIONS + */ + public void setMaxTotalConnections(int maxTotalConnections) { + setIntParameter( + HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, + maxTotalConnections); + } + + /** + * Gets the maximum number of connections allowed. + * + * @return The maximum number of connections allowed. + * + * @see #MAX_TOTAL_CONNECTIONS + */ + public int getMaxTotalConnections() { + return getIntParameter( + HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, + MultiThreadedHttpConnectionManager.DEFAULT_MAX_TOTAL_CONNECTIONS); + } }