tgt的源码:
public final class DefaultTicketRegistry
extends AbstractTicketRegistry
{
private final Map<String, Ticket> cache;
public DefaultTicketRegistry()
{
this.cache = new ConcurrentHashMap();
}
public DefaultTicketRegistry(int initialCapacity, float loadFactor, int concurrencyLevel)
{
this.cache = new ConcurrentHashMap(initialCapacity, loadFactor, concurrencyLevel);
}
public void addTicket(Ticket ticket)
{
Assert.notNull(ticket, "ticket cannot be null");
this.logger.debug("Added ticket [{}] to registry.", ticket.getId());
this.cache.put(ticket.getId(), ticket);
}
public Ticket getTicket(String ticketId)
{
if (ticketId == null) {
return null;
}
this.logger.debug("Attempting to retrieve ticket [{}]", ticketId);
Ticket ticket = (Ticket)this.cache.get(ticketId);
if (ticket != null) {
this.logger.debug("Ticket [{}] found in registry.", ticketId);
}
return ticket;
}
public boolean deleteTicket(String ticketId)
{
if (ticketId == null) {
return false;
}
this.logger.debug("Removing ticket [{}] from registry", ticketId);
return this.cache.remove(ticketId) != null;
}
public Collection<Ticket> getTickets()
{
return Collections.unmodifiableCollection(this.cache.values());
}
public int sessionCount()
{
int count = 0;
for (Ticket t : this.cache.values()) {
if ((t instanceof TicketGrantingTicket)) {
count++;
}
}
return count;
}
public int serviceTicketCount()
{
int count = 0;
for (Ticket t : this.cache.values()) {
if ((t instanceof ServiceTicket)) {
count++;
}
}
return count;
}
}
public abstract class AbstractTicketRegistry
implements TicketRegistry, TicketRegistryState
{
protected final Logger logger = LoggerFactory.getLogger(getClass());
public final <T extends Ticket> T getTicket(String ticketId, Class<? extends Ticket> clazz)
{
Assert.notNull(clazz, "clazz cannot be null");
Ticket ticket = getTicket(ticketId);
if (ticket == null) {
return null;
}
if (!clazz.isAssignableFrom(ticket.getClass())) {
throw new ClassCastException("Ticket [" + ticket.getId() + " is of type " + ticket.getClass() + " when we were expecting " + clazz);
}
return (T)ticket;
}
public int sessionCount()
{
this.logger.debug("sessionCount() operation is not implemented by the ticket registry instance {}. Returning unknown as {}", getClass().getName(), Integer.valueOf(-2147483648));
return -2147483648;
}
public int serviceTicketCount()
{
this.logger.debug("serviceTicketCount() operation is not implemented by the ticket registry instance {}. Returning unknown as {}", getClass().getName(), Integer.valueOf(-2147483648));
return -2147483648;
}
}
public abstract interface TicketRegistry
{
public abstract void addTicket(Ticket paramTicket);
public abstract <T extends Ticket> T getTicket(String paramString, Class<? extends Ticket> paramClass);
public abstract Ticket getTicket(String paramString);
public abstract boolean deleteTicket(String paramString);
public abstract Collection<Ticket> getTickets();
}
public abstract interface TicketRegistryState
{
public abstract int sessionCount();
public abstract int serviceTicketCount();
}
带过期时间的map
public class Cas20ProxyReceivingTicketValidationFilter
extends AbstractTicketValidationFilter
{
private static final String[] RESERVED_INIT_PARAMS = { "proxyGrantingTicketStorageClass", "proxyReceptorUrl", "acceptAnyProxy", "allowedProxyChains", "casServerUrlPrefix", "proxyCallbackUrl", "renew", "exceptionOnValidationFailure", "redirectAfterValidation", "useSession", "serverName", "service", "artifactParameterName", "serviceParameterName", "encodeServiceUrl", "millisBetweenCleanUps", "hostnameVerifier", "encoding", "config" };
private static final int DEFAULT_MILLIS_BETWEEN_CLEANUPS = 60000;
private String proxyReceptorUrl;
private Timer timer;
private TimerTask timerTask;
private int millisBetweenCleanUps;
private ProxyGrantingTicketStorage proxyGrantingTicketStorage = new ProxyGrantingTicketStorageImpl();
public void init()
{
super.init();
CommonUtils.assertNotNull(this.proxyGrantingTicketStorage, "proxyGrantingTicketStorage cannot be null.");
if (this.timer == null) {
this.timer = new Timer(true);
}
if (this.timerTask == null) {
this.timerTask = new CleanUpTimerTask(this.proxyGrantingTicketStorage);
}
this.timer.schedule(this.timerTask, this.millisBetweenCleanUps, this.millisBetweenCleanUps);
}
}
import java.util.TimerTask;
public final class CleanUpTimerTask
extends TimerTask
{
private final ProxyGrantingTicketStorage proxyGrantingTicketStorage;
public CleanUpTimerTask(ProxyGrantingTicketStorage proxyGrantingTicketStorage)
{
this.proxyGrantingTicketStorage = proxyGrantingTicketStorage;
}
public void run()
{
this.proxyGrantingTicketStorage.cleanUp();
}
}
public final class ProxyGrantingTicketStorageImpl
implements ProxyGrantingTicketStorage
{
private final Log log = LogFactory.getLog(getClass());
private static final long DEFAULT_TIMEOUT = 60000L;
private final ConcurrentMap<String, ProxyGrantingTicketHolder> cache = new ConcurrentHashMap();
private long timeout;
public ProxyGrantingTicketStorageImpl()
{
this(60000L);
}
public ProxyGrantingTicketStorageImpl(long timeout)
{
this.timeout = timeout;
}
public String retrieve(String proxyGrantingTicketIou)
{
ProxyGrantingTicketHolder holder = (ProxyGrantingTicketHolder)this.cache.get(proxyGrantingTicketIou);
if (holder == null)
{
this.log.info("No Proxy Ticket found for [" + proxyGrantingTicketIou + "].");
return null;
}
this.cache.remove(proxyGrantingTicketIou);
if (this.log.isDebugEnabled()) {
this.log.debug("Returned ProxyGrantingTicket of [" + holder.getProxyGrantingTicket() + "]");
}
return holder.getProxyGrantingTicket();
}
public void save(String proxyGrantingTicketIou, String proxyGrantingTicket)
{
ProxyGrantingTicketHolder holder = new ProxyGrantingTicketHolder(proxyGrantingTicket);
if (this.log.isDebugEnabled()) {
this.log.debug("Saving ProxyGrantingTicketIOU and ProxyGrantingTicket combo: [" + proxyGrantingTicketIou + ", " + proxyGrantingTicket + "]");
}
this.cache.put(proxyGrantingTicketIou, holder);
}
public void cleanUp()
{
for (Map.Entry<String, ProxyGrantingTicketHolder> holder : this.cache.entrySet()) {
if (((ProxyGrantingTicketHolder)holder.getValue()).isExpired(this.timeout)) {
this.cache.remove(holder.getKey());
}
}
}
private static final class ProxyGrantingTicketHolder
{
private final String proxyGrantingTicket;
private final long timeInserted;
protected ProxyGrantingTicketHolder(String proxyGrantingTicket)
{
this.proxyGrantingTicket = proxyGrantingTicket;
this.timeInserted = System.currentTimeMillis();
}
public String getProxyGrantingTicket()
{
return this.proxyGrantingTicket;
}
final boolean isExpired(long timeout)
{
return System.currentTimeMillis() - this.timeInserted > timeout;
}
}
}
每一次操作都要更新状态:
protected final void updateState()
{
this.previousLastTimeUsed = this.lastTimeUsed;
this.lastTimeUsed = System.currentTimeMillis();
this.countOfUses += 1;
}