From 7ec734211f7053f16fb53206d4cc82f76941c608 Mon Sep 17 00:00:00 2001 From: Roland Weber Date: Sat, 29 Dec 2007 15:16:51 +0000 Subject: [PATCH] HTTPCLIENT-725: use factory methods git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@607426 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/impl/conn/tsccm/ConnPoolByRoute.java | 99 ++++++++++++++----- 1 file changed, 72 insertions(+), 27 deletions(-) diff --git a/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ConnPoolByRoute.java b/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ConnPoolByRoute.java index 0870b83f5..aa978ad60 100644 --- a/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ConnPoolByRoute.java +++ b/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ConnPoolByRoute.java @@ -73,17 +73,17 @@ public class ConnPoolByRoute extends AbstractConnPool { /** The list of free connections */ - private Queue freeConnections; + protected Queue freeConnections; /** The list of WaitingThreads waiting for a connection */ - private Queue waitingThreads; + protected Queue waitingThreads; /** * A map of route-specific pools. * Keys are of class {@link HttpRoute}, * values of class {@link RouteSpecificPool}. */ - private final Map routeToPool; + protected final Map routeToPool; @@ -95,10 +95,68 @@ public class ConnPoolByRoute extends AbstractConnPool { public ConnPoolByRoute(ClientConnectionManager mgr) { super(mgr); - //@@@ use factory method, at least for waitingThreads - freeConnections = new LinkedList(); - waitingThreads = new LinkedList(); - routeToPool = new HashMap(); + freeConnections = createFreeConnQueue(); + waitingThreads = createWaitingThreadQueue(); + routeToPool = createRouteToPoolMap(); + } + + + /** + * Creates the queue for {@link #freeConnections}. + * Called once by the constructor. + * + * @return a queue + */ + protected Queue createFreeConnQueue() { + return new LinkedList(); + } + + /** + * Creates the queue for {@link #waitingThreads}. + * Called once by the constructor. + * + * @return a queue + */ + protected Queue createWaitingThreadQueue() { + return new LinkedList(); + } + + /** + * Creates the map for {@link #routeToPool}. + * Called once by the constructor. + * + * @return a map + */ + protected Map createRouteToPoolMap() { + return new HashMap(); + } + + + /** + * Creates a new route-specific pool. + * Called by {@link #getRoutePool} when necessary. + * + * @param route the route + * + * @return the new pool + */ + protected RouteSpecificPool newRouteSpecificPool(HttpRoute route) { + return new RouteSpecificPool(route); + } + + + /** + * Creates a new waiting thread. + * Called by {@link #getRoutePool} when necessary. + * + * @param cond the condition to wait for + * @param rospl the route specific pool, or null + * + * @return a waiting thread representation + */ + protected WaitingThread newWaitingThread(Condition cond, + RouteSpecificPool rospl) { + return new WaitingThread(cond, rospl); } @@ -133,19 +191,6 @@ public class ConnPoolByRoute extends AbstractConnPool { } - /** - * Creates a new route-specific pool. - * Called by {@link #getRoutePool getRoutePool}, if necessary. - * - * @param route the route - * - * @return the new pool - */ - protected RouteSpecificPool newRouteSpecificPool(HttpRoute route) { - return new RouteSpecificPool(route); - } - - //@@@ consider alternatives for gathering statistics public int getConnectionsInPool(HttpRoute route) { @@ -219,9 +264,8 @@ public class ConnPoolByRoute extends AbstractConnPool { } if (waitingThread == null) { - //@@@ use factory method? - waitingThread = new WaitingThread - (poolLock.newCondition(), rospl); + waitingThread = + newWaitingThread(poolLock.newCondition(), rospl); } boolean success = false; @@ -363,15 +407,16 @@ public class ConnPoolByRoute extends AbstractConnPool { LOG.debug("Creating new connection. " + rospl.getRoute()); } - BasicPoolEntry entry = null; + // the entry will create the connection when needed + BasicPoolEntry entry = + new BasicPoolEntry(op, rospl.getRoute(), refQueue); + try { poolLock.lock(); - // the entry will create the connection when needed - entry = new BasicPoolEntry(op, rospl.getRoute(), refQueue); rospl.createdEntry(entry); numConnections++; - + issuedConnections.add(entry.getWeakRef()); } finally {