generics in HttpConn

git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@602981 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Roland Weber 2007-12-10 18:01:12 +00:00
parent c7b26a6450
commit 87ff897181
8 changed files with 62 additions and 57 deletions

View File

@ -53,7 +53,7 @@ import org.apache.http.HttpHost;
public final class SchemeRegistry {
/** The available schemes in this registry. */
private final Map registeredSchemes;
private final Map<String,Scheme> registeredSchemes;
/**
@ -61,7 +61,7 @@ public final class SchemeRegistry {
*/
public SchemeRegistry() {
super();
registeredSchemes = new LinkedHashMap();
registeredSchemes = new LinkedHashMap<String,Scheme>();
}
@ -167,7 +167,7 @@ public final class SchemeRegistry {
* @return List containing registered scheme names.
*/
public synchronized final List getSchemeNames() {
return new ArrayList(registeredSchemes.keySet());
return new ArrayList<String>(registeredSchemes.keySet());
}

View File

@ -156,7 +156,7 @@ public abstract class AbstractVerifier implements HostnameVerifier {
// STRICT implementations of the HostnameVerifier only use the
// first CN provided. All other CNs are ignored.
// (Firefox, wget, curl, Sun Java 1.4, 5, 6 all work this way).
LinkedList names = new LinkedList();
LinkedList<String> names = new LinkedList<String>();
if(cns != null && cns.length > 0 && cns[0] != null) {
names.add(cns[0]);
}
@ -180,9 +180,9 @@ public abstract class AbstractVerifier implements HostnameVerifier {
// establish the socket to the hostname in the certificate.
String hostName = host.trim().toLowerCase();
boolean match = false;
for(Iterator it = names.iterator(); it.hasNext();) {
for(Iterator<String> it = names.iterator(); it.hasNext();) {
// Don't trim the CN, though!
String cn = (String) it.next();
String cn = it.next();
cn = cn.toLowerCase();
// Store CN in StringBuffer in case we need to report an error.
buf.append(" <");
@ -235,7 +235,7 @@ public abstract class AbstractVerifier implements HostnameVerifier {
}
public static String[] getCNs(X509Certificate cert) {
LinkedList cnList = new LinkedList();
LinkedList<String> cnList = new LinkedList<String>();
/*
Sebastian Hauer's original StrictSSLProtocolSocketFactory used
getName() and had the following comment:
@ -292,8 +292,8 @@ public abstract class AbstractVerifier implements HostnameVerifier {
* @return Array of SubjectALT DNS names stored in the certificate.
*/
public static String[] getDNSSubjectAlts(X509Certificate cert) {
LinkedList subjectAltList = new LinkedList();
Collection c = null;
LinkedList<String> subjectAltList = new LinkedList<String>();
Collection<List<?>> c = null;
try {
c = cert.getSubjectAlternativeNames();
}
@ -302,9 +302,9 @@ public abstract class AbstractVerifier implements HostnameVerifier {
cpe.printStackTrace();
}
if(c != null) {
Iterator it = c.iterator();
Iterator<List<?>> it = c.iterator();
while(it.hasNext()) {
List list = (List) it.next();
List<?> list = it.next();
int type = ((Integer) list.get(0)).intValue();
// If type is 2, then we've got a dNSName
if(type == 2) {
@ -337,4 +337,4 @@ public abstract class AbstractVerifier implements HostnameVerifier {
return count;
}
}
}

View File

@ -49,17 +49,16 @@ import org.apache.http.HttpConnection;
* @since 4.0
*/
public class IdleConnectionHandler {
private static final Log LOG = LogFactory.getLog(IdleConnectionHandler.class);
private final Log LOG = LogFactory.getLog(IdleConnectionHandler.class);
/** Holds connections and the time they were added. */
private Map connectionToAdded = new HashMap();
private Map<HttpConnection,Long> connectionToAdded;
/**
*
*/
public IdleConnectionHandler() {
super();
connectionToAdded = new HashMap<HttpConnection,Long>();
}
/**
@ -110,11 +109,12 @@ public class IdleConnectionHandler {
LOG.debug("Checking for connections, idleTimeout: " + idleTimeout);
}
Iterator connectionIter = connectionToAdded.keySet().iterator();
Iterator<HttpConnection> connectionIter =
connectionToAdded.keySet().iterator();
while (connectionIter.hasNext()) {
HttpConnection conn = (HttpConnection) connectionIter.next();
Long connectionTime = (Long) connectionToAdded.get(conn);
HttpConnection conn = connectionIter.next();
Long connectionTime = connectionToAdded.get(conn);
if (connectionTime.longValue() <= idleTimeout) {
if (LOG.isDebugEnabled()) {
LOG.debug("Closing connection, connection time: " + connectionTime);

View File

@ -66,7 +66,7 @@ public abstract class AbstractConnPool implements RefQueueHandler {
* and point to the pool entry for the issued connection.
* GCed connections are detected by the missing pool entries.
*/
protected Set issuedConnections;
protected Set<BasicPoolEntryRef> issuedConnections;
/** The handler for idle connections. */
protected IdleConnectionHandler idleConnHandler;
@ -89,8 +89,12 @@ public abstract class AbstractConnPool implements RefQueueHandler {
protected ConnMgrRef connManager;
/** A reference queue to track loss of pool entries to GC. */
protected ReferenceQueue refQueue;
/**
* A reference queue to track loss of pool entries to GC.
* The same queue is used to track loss of the connection manager,
* so we cannot specialize the type.
*/
protected ReferenceQueue<Object> refQueue;
/** A worker (thread) to track loss of pool entries to GC. */
private RefQueueWorker refWorker;
@ -103,7 +107,8 @@ public abstract class AbstractConnPool implements RefQueueHandler {
/**
* A weak reference to the connection manager, to detect GC.
*/
private static class ConnMgrRef extends WeakReference {
private static class ConnMgrRef
extends WeakReference<ClientConnectionManager> {
/**
* Creates a new reference.
@ -112,7 +117,7 @@ public abstract class AbstractConnPool implements RefQueueHandler {
* @param queue the reference queue, or <code>null</code>
*/
public ConnMgrRef(ClientConnectionManager ccmgr,
ReferenceQueue queue) {
ReferenceQueue<Object> queue) {
super(ccmgr, queue);
}
}
@ -127,12 +132,12 @@ public abstract class AbstractConnPool implements RefQueueHandler {
params = mgr.getParams();
issuedConnections = new HashSet();
issuedConnections = new HashSet<BasicPoolEntryRef>();
idleConnHandler = new IdleConnectionHandler();
boolean conngc = true; //@@@ check parameters to decide
if (conngc) {
refQueue = new ReferenceQueue();
refQueue = new ReferenceQueue<Object>();
refWorker = new RefQueueWorker(refQueue, this);
Thread t = new Thread(refWorker); //@@@ use a thread factory
t.setDaemon(true);
@ -247,9 +252,9 @@ public abstract class AbstractConnPool implements RefQueueHandler {
refWorker.shutdown();
// close all connections that are issued to an application
Iterator iter = issuedConnections.iterator();
Iterator<BasicPoolEntryRef> iter = issuedConnections.iterator();
while (iter.hasNext()) {
BasicPoolEntryRef per = (BasicPoolEntryRef) iter.next();
BasicPoolEntryRef per = iter.next();
iter.remove();
BasicPoolEntry entry = (BasicPoolEntry) per.get();
if (entry != null) {

View File

@ -69,7 +69,7 @@ public class BasicPoolEntry extends AbstractPoolEntry {
*/
public BasicPoolEntry(ClientConnectionOperator op,
HttpRoute route,
ReferenceQueue queue) {
ReferenceQueue<Object> queue) {
//@@@ create connection in base? or delay creation until needed?
super(op.createConnection(), route);
if (route == null) {
@ -96,7 +96,7 @@ public class BasicPoolEntry extends AbstractPoolEntry {
return super.plannedRoute;
}
protected final WeakReference getWeakRef() {
protected final BasicPoolEntryRef getWeakRef() {
return this.reference;
}

View File

@ -43,7 +43,7 @@ import org.apache.http.conn.HttpRoute;
* This reference explicitly keeps the planned route, so the connection
* can be reclaimed if it is lost to garbage collection.
*/
public class BasicPoolEntryRef extends WeakReference {
public class BasicPoolEntryRef extends WeakReference<BasicPoolEntry> {
/** The planned route of the entry. */
private final HttpRoute route;
@ -55,7 +55,8 @@ public class BasicPoolEntryRef extends WeakReference {
* @param entry the pool entry, must not be <code>null</code>
* @param queue the reference queue, or <code>null</code>
*/
public BasicPoolEntryRef(BasicPoolEntry entry, ReferenceQueue queue) {
public BasicPoolEntryRef(BasicPoolEntry entry,
ReferenceQueue<Object> queue) {
super(entry, queue);
if (entry == null) {
throw new IllegalArgumentException

View File

@ -66,17 +66,17 @@ public class ConnPoolByRoute extends AbstractConnPool {
/** The list of free connections */
private LinkedList freeConnections;
private LinkedList<BasicPoolEntry> freeConnections;
/** The list of WaitingThreads waiting for a connection */
private LinkedList waitingThreads;
private LinkedList<WaitingThread> waitingThreads;
/**
* A map of route-specific pools.
* Keys are of class {@link HttpRoute},
* values of class {@link RouteSpecificPool}.
*/
private final Map routeToPool;
private final Map<HttpRoute,RouteSpecificPool> routeToPool;
@ -113,9 +113,9 @@ public class ConnPoolByRoute extends AbstractConnPool {
public ConnPoolByRoute(ClientConnectionManager mgr) {
super(mgr);
freeConnections = new LinkedList();
waitingThreads = new LinkedList();
routeToPool = new HashMap();
freeConnections = new LinkedList<BasicPoolEntry>();
waitingThreads = new LinkedList<WaitingThread>();
routeToPool = new HashMap<HttpRoute,RouteSpecificPool>();
}
@ -131,7 +131,7 @@ public class ConnPoolByRoute extends AbstractConnPool {
protected synchronized RouteSpecificPool getRoutePool(HttpRoute route,
boolean create) {
RouteSpecificPool rospl = (RouteSpecificPool) routeToPool.get(route);
RouteSpecificPool rospl = routeToPool.get(route);
if ((rospl == null) && create) {
// no pool for this route yet (or anymore)
rospl = newRouteSpecificPool(route);
@ -458,15 +458,14 @@ public class ConnPoolByRoute extends AbstractConnPool {
LOG.debug("Notifying thread waiting on pool. "
+ rospl.getRoute());
}
waitingThread = (WaitingThread)
rospl.waitingThreads.removeFirst();
waitingThread = (WaitingThread) rospl.waitingThreads.removeFirst();
waitingThreads.remove(waitingThread);
} else if (!waitingThreads.isEmpty()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Notifying thread waiting on any pool.");
}
waitingThread = (WaitingThread) waitingThreads.removeFirst();
waitingThread = waitingThreads.removeFirst();
waitingThread.pool.waitingThreads.remove(waitingThread);
} else if (LOG.isDebugEnabled()) {
@ -503,19 +502,19 @@ public class ConnPoolByRoute extends AbstractConnPool {
// close all free connections
//@@@ move this to base class?
Iterator iter = freeConnections.iterator();
while (iter.hasNext()) {
BasicPoolEntry entry = (BasicPoolEntry) iter.next();
iter.remove();
Iterator<BasicPoolEntry> ibpe = freeConnections.iterator();
while (ibpe.hasNext()) {
BasicPoolEntry entry = ibpe.next();
ibpe.remove();
closeConnection(entry.getConnection());
}
// interrupt all waiting threads
iter = waitingThreads.iterator();
while (iter.hasNext()) {
WaitingThread waiter = (WaitingThread) iter.next();
iter.remove();
Iterator<WaitingThread> iwth = waitingThreads.iterator();
while (iwth.hasNext()) {
WaitingThread waiter = iwth.next();
iwth.remove();
waiter.interruptedByConnectionPool = true;
waiter.thread.interrupt();
}

View File

@ -48,10 +48,10 @@ public class RouteSpecificPool {
private final HttpRoute route;
/** The list of free entries. */
private LinkedList freeEntries;
private LinkedList<BasicPoolEntry> freeEntries;
/** The list of threads waiting for this pool. */
/*private@@@ currently still default*/ LinkedList waitingThreads;
/*private@@@ currently still default*/ LinkedList<Object> waitingThreads;
/** The number of created entries. */
private int numEntries;
@ -64,8 +64,8 @@ public class RouteSpecificPool {
*/
public RouteSpecificPool(HttpRoute r) {
this.route = r;
this.freeEntries = new LinkedList();
this.waitingThreads = new LinkedList();
this.freeEntries = new LinkedList<BasicPoolEntry>();
this.waitingThreads = new LinkedList<Object>();
this.numEntries = 0;
}
@ -115,7 +115,7 @@ public class RouteSpecificPool {
BasicPoolEntry entry = null;
if (!freeEntries.isEmpty()) {
entry = (BasicPoolEntry) freeEntries.removeLast();
entry = freeEntries.removeLast();
}
return entry;