org.apache.myfaces.trinidadinternal.util
Class TokenCache

java.lang.Object
  extended by org.apache.myfaces.trinidadinternal.util.TokenCache
All Implemented Interfaces:
java.io.Serializable

public class TokenCache
extends java.lang.Object
implements java.io.Serializable

A simple LRU tokenized cache. The cache is responsible for storing tokens, but the storage of the values referred to by those tokens is not handled by this class. Instead, the user of this class has to provide a Map instance to each call.

The design seems odd, but is intentional - this way, a session Map can be used directly as the storage target for values, while the TokenCache simply maintains the logic of which tokens should still be available. Storing values directly in the cache object (instead of directly on the session) causes HttpSession failover difficulties.

TokenCache also supports the concept of "pinning", whereby one token can be pinned to another. The pinned token will not be removed from the cache until all tokens that it is pinned to are also out of the cache.

See Also:
Serialized Form

Field Summary
static char SEPARATOR_CHAR
          Character guaranteed to not be used in tokens
 
Constructor Summary
TokenCache(int size)
          Create a TokenCache that will store the last "size" entries.
 
Method Summary
<V> java.lang.String
addNewEntry(V value, java.util.Map<java.lang.String,V> targetStore)
          Create a new token; and use that token to store a value into a target Map.
<V> java.lang.String
addNewEntry(V value, java.util.Map<java.lang.String,V> targetStore, java.lang.String pinnedToken)
          Create a new token; and use that token to store a value into a target Map.
<V> void
clear(java.util.Map<java.lang.String,V> targetStore)
          Clear a cache, without resetting the token.
static TokenCache getTokenCacheFromSession(javax.faces.context.ExternalContext extContext, java.lang.String cacheName, boolean createIfNeeded, int defaultSize)
          Gets a TokenCache from the session, creating it if needed.
 boolean isAvailable(java.lang.String token)
          Returns true if an entry is still available.
 void reattachOwner(java.util.Map<java.lang.String,java.lang.Object> owner)
          Reattaches the owner after Serialization since the owner might not be Serializable or it might not be a good idea to serialize the owner.
<V> V
removeOldEntry(java.lang.String token, java.util.Map<java.lang.String,V> targetStore)
          Removes a value from the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SEPARATOR_CHAR

public static final char SEPARATOR_CHAR
Character guaranteed to not be used in tokens

See Also:
Constant Field Values
Constructor Detail

TokenCache

public TokenCache(int size)
Create a TokenCache that will store the last "size" entries. This version should not be used if the token cache is externally accessible (since the seed always starts at 0). Use the constructor with the long seed instead.

Method Detail

getTokenCacheFromSession

public static TokenCache getTokenCacheFromSession(javax.faces.context.ExternalContext extContext,
                                                  java.lang.String cacheName,
                                                  boolean createIfNeeded,
                                                  int defaultSize)
Gets a TokenCache from the session, creating it if needed.


reattachOwner

public void reattachOwner(java.util.Map<java.lang.String,java.lang.Object> owner)
Reattaches the owner after Serialization since the owner might not be Serializable or it might not be a good idea to serialize the owner.

Parameters:
owner -
Throws:
java.lang.NullPointerException - if owner is null

addNewEntry

public <V> java.lang.String addNewEntry(V value,
                                        java.util.Map<java.lang.String,V> targetStore)
Create a new token; and use that token to store a value into a target Map. The least recently used values from the cache may be removed.

Parameters:
value - the value being added to the target store
targetStore - the map used for storing the value
Returns:
the token used to store the value

addNewEntry

public <V> java.lang.String addNewEntry(V value,
                                        java.util.Map<java.lang.String,V> targetStore,
                                        java.lang.String pinnedToken)
Create a new token; and use that token to store a value into a target Map. The least recently used values from the cache may be removed.

Parameters:
value - the value being added to the target store
targetStore - the map used for storing the value
pinnedToken - a token, that if still in the cache, will not be freed until this current token is also freed
Returns:
the token used to store the value

isAvailable

public boolean isAvailable(java.lang.String token)
Returns true if an entry is still available. This method has a side-effect: by virtue of accessing the token, it is now at the top of the most-recently-used list.


removeOldEntry

public <V> V removeOldEntry(java.lang.String token,
                            java.util.Map<java.lang.String,V> targetStore)
Removes a value from the cache.

Returns:
previous value associated with the token, if any

clear

public <V> void clear(java.util.Map<java.lang.String,V> targetStore)
Clear a cache, without resetting the token.



Copyright © 2001-2011 The Apache Software Foundation. All Rights Reserved.