Index: src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java
===================================================================
--- src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java (revision 560353)
+++ src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java (working copy)
@@ -449,7 +449,7 @@
// we clone the hostConfiguration
// so that it cannot be changed once the connection has been retrieved
hostConfiguration = new HostConfiguration(hostConfiguration);
- HostConnectionPool hostPool = connectionPool.getHostPool(hostConfiguration);
+ HostConnectionPool hostPool = connectionPool.getHostPool(hostConfiguration, true);
WaitingThread waitingThread = null;
boolean useTimeout = (timeout > 0);
@@ -557,8 +557,8 @@
*/
public int getConnectionsInPool(HostConfiguration hostConfiguration) {
synchronized (connectionPool) {
- HostConnectionPool hostPool = connectionPool.getHostPool(hostConfiguration);
- return hostPool.numConnections;
+ HostConnectionPool hostPool = connectionPool.getHostPool(hostConfiguration, false);
+ return (hostPool != null) ? hostPool.numConnections : 0;
}
}
@@ -755,7 +755,7 @@
* @return a new connection or null if none are available
*/
public synchronized HttpConnection createConnection(HostConfiguration hostConfiguration) {
- HostConnectionPool hostPool = getHostPool(hostConfiguration);
+ HostConnectionPool hostPool = getHostPool(hostConfiguration, true);
if (LOG.isDebugEnabled()) {
LOG.debug("Allocating new connection, hostConfig=" + hostConfiguration);
}
@@ -779,7 +779,7 @@
* @param config the host configuration of the connection that was lost
*/
public synchronized void handleLostConnection(HostConfiguration config) {
- HostConnectionPool hostPool = getHostPool(config);
+ HostConnectionPool hostPool = getHostPool(config, true);
hostPool.numConnections--;
if (hostPool.numConnections == 0) mapHosts.remove(config);
@@ -791,15 +791,19 @@
* Get the pool (list) of connections available for the given hostConfig.
*
* @param hostConfiguration the configuraton for the connection pool
- * @return a pool (list) of connections available for the given config
+ * @param create true to create a pool if not found,
+ * false to return null
+ *
+ * @return a pool (list) of connections available for the given config,
+ * or null if neither found nor created
*/
- public synchronized HostConnectionPool getHostPool(HostConfiguration hostConfiguration) {
+ public synchronized HostConnectionPool getHostPool(HostConfiguration hostConfiguration, boolean create) {
LOG.trace("enter HttpConnectionManager.ConnectionPool.getHostPool(HostConfiguration)");
// Look for a list of connections for the given config
HostConnectionPool listConnections = (HostConnectionPool)
mapHosts.get(hostConfiguration);
- if (listConnections == null) {
+ if ((listConnections == null) && create) {
// First time for this config
listConnections = new HostConnectionPool();
listConnections.hostConfiguration = hostConfiguration;
@@ -819,9 +823,9 @@
HttpConnectionWithReference connection = null;
- HostConnectionPool hostPool = getHostPool(hostConfiguration);
+ HostConnectionPool hostPool = getHostPool(hostConfiguration, false);
- if (hostPool.freeConnections.size() > 0) {
+ if ((hostPool != null) && (hostPool.freeConnections.size() > 0)) {
connection = (HttpConnectionWithReference) hostPool.freeConnections.removeLast();
freeConnections.remove(connection);
// store a reference to this connection so that it can be cleaned up
@@ -883,7 +887,7 @@
connection.close();
- HostConnectionPool hostPool = getHostPool(connectionConfiguration);
+ HostConnectionPool hostPool = getHostPool(connectionConfiguration, true);
hostPool.freeConnections.remove(connection);
hostPool.numConnections--;
@@ -915,7 +919,7 @@
* @see #notifyWaitingThread(HostConnectionPool)
*/
public synchronized void notifyWaitingThread(HostConfiguration configuration) {
- notifyWaitingThread(getHostPool(configuration));
+ notifyWaitingThread(getHostPool(configuration, true));
}
/**
@@ -976,7 +980,7 @@
return;
}
- HostConnectionPool hostPool = getHostPool(connectionConfiguration);
+ HostConnectionPool hostPool = getHostPool(connectionConfiguration, true);
// Put the connect back in the available list and notify a waiter
hostPool.freeConnections.add(conn);