1 19 20 package com.maverick.http; 21 22 import java.io.IOException ; 23 import java.net.UnknownHostException ; 24 import java.text.MessageFormat ; 25 import java.util.Enumeration ; 26 import java.util.Vector ; 27 28 35 public class HttpConnectionManager { 36 37 private Vector connections = new Vector (); 38 HttpClient client; 39 40 static int newIdx = 0; 41 static int reuseIdx = 0; 42 43 private int maxPoolSize = Integer.MAX_VALUE; 44 45 org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(HttpConnectionManager.class); 47 48 50 public HttpConnectionManager(HttpClient client) { 51 this.client = client; 52 } 53 54 62 public synchronized HttpConnection getConnection() throws IOException , UnknownHostException , HttpException, 63 UnsupportedAuthenticationException, AuthenticationCancelledException { 64 65 HttpConnection con; 66 67 if (connections.size() == 0) { 68 log.info(Messages.getString("HttpConnectionManager.creatingNewConnection")); con = new HttpConnection(client); 72 } else { 73 con = (HttpConnection) connections.elementAt(0); 74 connections.removeElementAt(0); 75 log.info(MessageFormat.format(Messages.getString("HttpConnectionManager.reusingConnection"), new Object [] { new Integer (connections.size()), con.toString() })); con.verify(); 79 } 80 log.info("Returning pooled connection"); return con; 84 } 85 86 92 public synchronized void setMaxPoolSize(int maxPoolSize) { 93 this.maxPoolSize = maxPoolSize; 94 } 95 96 101 public synchronized void releaseConnection(HttpConnection con) { 102 if (con.canReuse() && !con.isClosed()) { 104 if (maxPoolSize == 0 || connections.size() < maxPoolSize) 105 connections.addElement(con); 106 else 107 con.close(); 108 log.info(MessageFormat.format(Messages.getString("HttpConnectionManager.releasedToPool"), new Object [] { new Integer (connections.size()), con.toString() })); con.updateState(); 113 } else { 114 log.info(Messages.getString("HttpConnectionManager.willNotReuse")); } 118 119 } 120 121 124 public synchronized void releaseAllConnections() { 125 for (Enumeration e1 = connections.elements(); e1.hasMoreElements();) { 126 HttpConnection con = (HttpConnection) e1.nextElement(); 127 releaseConnection(con); 128 } 129 } 130 131 141 public void checkConnection(HttpConnection con) throws UnknownHostException , IOException , HttpException, 142 UnsupportedAuthenticationException, AuthenticationCancelledException { 143 if (con.isClosed || !con.canReuse) { 144 con.reconnect(); 145 } 146 } 147 148 151 public synchronized void closeConnections() { 152 153 for (Enumeration e1 = connections.elements(); e1.hasMoreElements();) { 154 HttpConnection con = (HttpConnection) e1.nextElement(); 155 con.close(); 156 } 157 } 158 159 164 public synchronized int getConnectionCount() { 165 return connections.size(); 166 } 167 168 173 public synchronized int getOpenConnectionCount() { 174 int open = 0; 175 for (Enumeration e1 = connections.elements(); e1.hasMoreElements();) { 176 HttpConnection con = (HttpConnection) e1.nextElement(); 177 if (!con.isClosed()) { 178 open++; 179 } 180 } 181 return open; 182 183 } 184 185 } 186 | Popular Tags |