CASC-79 (not enough synchronization on HashMap)

also, made nested classes static (they didn't have to be non-static, so this version is slightly more flexible and takes up slightly less memory: see Effective Java 2nd Edition Item 22)
This commit is contained in:
Brad Cupit 2009-02-11 19:44:33 +00:00
parent 7191670c4e
commit c71748d104
1 changed files with 8 additions and 17 deletions

View File

@ -5,10 +5,9 @@
*/
package org.jasig.cas.client.proxy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
@ -21,6 +20,7 @@ import org.apache.commons.logging.LogFactory;
* A cleanup thread is run periodically to clean out the HashMap.
*
* @author Scott Battaglia
* @author Brad Cupit (brad [at] lsu {dot} edu)
* @version $Revision: 11729 $ $Date: 2007-09-26 14:22:30 -0400 (Tue, 26 Sep 2007) $
* @since 3.0
*/
@ -37,7 +37,7 @@ public final class ProxyGrantingTicketStorageImpl implements
/**
* Map that stores the PGTIOU to PGT mappings.
*/
private final Map cache = new HashMap();
private final Map cache = Collections.synchronizedMap(new HashMap());
/**
* Constructor set the timeout to the default value.
@ -91,7 +91,7 @@ public final class ProxyGrantingTicketStorageImpl implements
this.cache.put(proxyGrantingTicketIou, holder);
}
private final class ProxyGrantingTicketHolder {
private static final class ProxyGrantingTicketHolder {
private final String proxyGrantingTicket;
@ -111,7 +111,7 @@ public final class ProxyGrantingTicketStorageImpl implements
}
}
private final class ProxyGrantingTicketCleanupThread extends Thread {
private static final class ProxyGrantingTicketCleanupThread extends Thread {
private final long timeout;
@ -132,24 +132,15 @@ public final class ProxyGrantingTicketStorageImpl implements
// nothing to do
}
final List itemsToRemove = new ArrayList();
synchronized (this.cache) {
for (final Iterator iter = this.cache.keySet().iterator(); iter
for (final Iterator iter = this.cache.values().iterator(); iter
.hasNext();) {
final Object key = iter.next();
final ProxyGrantingTicketHolder holder = (ProxyGrantingTicketHolder) this.cache
.get(key);
final ProxyGrantingTicketHolder holder = (ProxyGrantingTicketHolder) iter.next();
if (holder.isExpired(this.timeout)) {
itemsToRemove.add(key);
iter.remove();
}
}
for (final Iterator iter = itemsToRemove.iterator(); iter
.hasNext();) {
this.cache.remove(iter.next());
}
}
}
}