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 12 Jul 2004 03:22:22 -0000 @@ -47,6 +47,13 @@ */ public class HostConfiguration implements Cloneable { + /** + * A value to represent any host configuration, instead of using something like + * null. This value should be treated as immutable and only used in + * lookups and other such places to represent "any" host config. + */ + 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 12 Jul 2004 03:22:26 -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 12 Jul 2004 03:22:26 -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,133 @@ 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. Use + * {@link HostConfiguration#ANY_HOST_CONFIGURATION} to configure the default value + * per host. + * @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); + if (m == null) { + // MAX_HOST_CONNECTIONS have not been configured, using the default value + return MultiThreadedHttpConnectionManager.DEFAULT_MAX_HOST_CONNECTIONS; + } else { + 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); + } } Index: src/test/org/apache/commons/httpclient/TestHttpConnectionManager.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpConnectionManager.java,v retrieving revision 1.22 diff -u -r1.22 TestHttpConnectionManager.java --- src/test/org/apache/commons/httpclient/TestHttpConnectionManager.java 25 Jun 2004 03:34:56 -0000 1.22 +++ src/test/org/apache/commons/httpclient/TestHttpConnectionManager.java 12 Jul 2004 03:22:28 -0000 @@ -219,8 +219,7 @@ public void testWriteRequestReleaseConnection() { MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); - connectionManager.getParams().setIntParameter( - HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1); + connectionManager.getParams().setDefaultMaxConnectionsPerHost(1); HttpClient client = createHttpClient(connectionManager); @@ -253,8 +252,7 @@ public void testReleaseConnection() { MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); - connectionManager.getParams().setIntParameter( - HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1); + connectionManager.getParams().setDefaultMaxConnectionsPerHost(1); HttpClient client = createHttpClient(connectionManager); // we shouldn't have to wait if a connection is available @@ -303,8 +301,7 @@ public void testResponseAutoRelease() throws Exception { MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); - connectionManager.getParams().setIntParameter( - HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1); + connectionManager.getParams().setDefaultMaxConnectionsPerHost(1); HttpClient client = createHttpClient(connectionManager); // we shouldn't have to wait if a connection is available @@ -341,10 +338,8 @@ public void testConnectionReclaiming() { MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); - connectionManager.getParams().setIntParameter( - HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1); - connectionManager.getParams().setIntParameter( - HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 1); + connectionManager.getParams().setDefaultMaxConnectionsPerHost(1); + connectionManager.getParams().setMaxTotalConnections(1); HostConfiguration host1 = new HostConfiguration(); host1.setHost("host1", -1, "http"); @@ -373,10 +368,8 @@ public void testShutdownAll() { MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); - connectionManager.getParams().setIntParameter( - HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1); - connectionManager.getParams().setIntParameter( - HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 1); + connectionManager.getParams().setDefaultMaxConnectionsPerHost(1); + connectionManager.getParams().setMaxTotalConnections(1); HostConfiguration host1 = new HostConfiguration(); host1.setHost("host1", -1, "http"); @@ -419,10 +412,8 @@ public void testShutdown() { MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); - connectionManager.getParams().setIntParameter( - HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1); - connectionManager.getParams().setIntParameter( - HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 1); + connectionManager.getParams().setDefaultMaxConnectionsPerHost(1); + connectionManager.getParams().setMaxTotalConnections(1); HostConfiguration host1 = new HostConfiguration(); host1.setHost("host1", -1, "http"); @@ -465,10 +456,8 @@ public void testMaxConnections() { MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); - connectionManager.getParams().setIntParameter( - HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1); - connectionManager.getParams().setIntParameter( - HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 2); + connectionManager.getParams().setDefaultMaxConnectionsPerHost(1); + connectionManager.getParams().setMaxTotalConnections(2); HostConfiguration host1 = new HostConfiguration(); host1.setHost("host1", -1, "http"); @@ -503,10 +492,8 @@ public void testHostReusePreference() { final MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); - connectionManager.getParams().setIntParameter( - HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1); - connectionManager.getParams().setIntParameter( - HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 1); + connectionManager.getParams().setDefaultMaxConnectionsPerHost(1); + connectionManager.getParams().setMaxTotalConnections(1); final HostConfiguration host1 = new HostConfiguration(); host1.setHost("host1", -1, "http"); @@ -553,8 +540,7 @@ public void testMaxConnectionsPerServer() { MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); - connectionManager.getParams().setIntParameter( - HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 1); + connectionManager.getParams().setDefaultMaxConnectionsPerHost(1); HttpClient client = createHttpClient(connectionManager); // we shouldn't have to wait if a connection is available @@ -674,8 +660,7 @@ public void testTimeout() { MultiThreadedHttpConnectionManager mgr = new MultiThreadedHttpConnectionManager(); - mgr.getParams().setIntParameter( - HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, 2); + mgr.getParams().setDefaultMaxConnectionsPerHost(2); try{ HostConfiguration hostConfig = new HostConfiguration();