de.bb.util
Class SessionManager

java.lang.Object
  extended by de.bb.util.SessionManager

public class SessionManager
extends java.lang.Object

A SessionManager is used to manage sessions. Sessions are objects which live a distinct time. Each time a session is used or touched, the livetime of that session is renewed. If the life time of a session is over, it is removed from the SessionManager. A session object may implement the Session.Callback interface, and if it does, its callback function is called before removal.


Nested Class Summary
static interface SessionManager.Callback
          This interface is used by the SessionManager, which maintains objects which live a distinct time.
 
Constructor Summary
SessionManager(int timeoutMilli)
          Deprecated. use the constructor with long instead!
SessionManager(long timeoutMilli)
          This class is used to maintain sessions, that are objects which live a distinct time.
SessionManager(long timeoutMilli, int maxCountOfSessions)
          This class is used to maintain sessions, that are objects which live a distinct time.
 
Method Summary
 void clear()
          Clears this SessionManager so that it contains no sessions.
 boolean contains(java.lang.Object session)
          Tests if some key maps into the specified value in this SessionManager.
 boolean containsKey(java.lang.Object key)
          Tests whether the specified object is a key in this SessionManager.
 java.util.Iterator elements()
          Returns an enumeration of the values in this SessionManager.
 java.lang.Object get(java.lang.Object key)
          Returns the value to which the specified key is mapped in this SessionManager.
 long getCreationMillis(java.lang.Object key)
          Returns the creation time for the given key.
 long getDeathMillis(java.lang.Object key)
          Returns the death time for the given key.
 int getMaxCount()
          Get current max count of sessions.
 long getTimeout()
          Get current default timout value.
 boolean isEmpty()
          Tests if this SessionManager maps no keys to values.
 java.util.Iterator keys()
          Returns an enumeration of the keys in this SessionManager.
static java.lang.String newKey()
          Create a new session key.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Maps the specified key to the specified value in this SessionManager.
 java.lang.Object put(java.lang.Object key, java.lang.Object value, int timeoutMilli)
          Deprecated.  
 java.lang.Object put(java.lang.Object key, java.lang.Object value, long timeoutMilli)
          Maps the specified key to the specified value in this SessionManager, using the SessionManager's timeout.
 java.lang.Object remove(java.lang.Object key)
          Removes the key (and its corresponding value) from this SessionManager.
 java.lang.Object removeButAsk(java.lang.Object key)
          Removes the key (and its corresponding value) from this SessionManager.
 void setMaxCount(int max)
          Set a new maximum size.
static void setRandom(java.util.Random rnd)
          Set a new random generator for all SessionManagers.
 void setTimeout(long timeoutMilli)
          Set a new default timout value.
 int size()
          Returns the number of keys in this SessionManager.
 java.lang.Object touch(java.lang.Object key)
          Sets the new timeout for the given key with the default timeout.
 java.lang.Object touch(java.lang.Object key, long timeoutMilli)
          Sets the new timeout for the given key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SessionManager

public SessionManager(long timeoutMilli,
                      int maxCountOfSessions)
This class is used to maintain sessions, that are objects which live a distinct time. Each time a session is used or touched, the livetime of that session is renewed. Constructs a SessionManager object.

Parameters:
timeoutMilli - an int specifying the timeout in ms
maxCountOfSessions - limit the count of used sessions.

SessionManager

public SessionManager(long timeoutMilli)
This class is used to maintain sessions, that are objects which live a distinct time. Each time a session is used or touched, the livetime of that session is renewed. Constructs a SessionManager object.

Parameters:
timeoutMilli - an int specifying the timeout in ms

SessionManager

public SessionManager(int timeoutMilli)
Deprecated. use the constructor with long instead!

This class is used to maintain sessions, that are objects which live a distinct time. Each time a session is used or touched, the livetime of that session is renewed. Constructs a SessionManager object.

Parameters:
timeoutMilli - an int specifying the timeout in ms
Method Detail

setRandom

public static void setRandom(java.util.Random rnd)
Set a new random generator for all SessionManagers.

Parameters:
rnd - the new random generator

clear

public void clear()
Clears this SessionManager so that it contains no sessions.


contains

public boolean contains(java.lang.Object session)
Tests if some key maps into the specified value in this SessionManager. This operation is more expensive than the containsKey method.

Parameters:
session - a sessin to search for
Returns:
true if some key maps to the value argument in this SessionManager; false otherwise.

containsKey

public boolean containsKey(java.lang.Object key)
Tests whether the specified object is a key in this SessionManager.

Parameters:
key - possible key.
Returns:
true if the specified object is a key in this SessionManager; false otherwise.

elements

public java.util.Iterator elements()
Returns an enumeration of the values in this SessionManager. Use the Enumeration methods on the returned object to fetch the elements sequentially.

Returns:
an enumeration of the values in this SessionManager.

get

public java.lang.Object get(java.lang.Object key)
Returns the value to which the specified key is mapped in this SessionManager.

Parameters:
key - a key in the SessionManager.
Returns:
the value to which the key is mapped in this SessionManager; null if the key is not mapped to any value in this SessionManager.

getMaxCount

public int getMaxCount()
Get current max count of sessions.

Returns:
current max count of sessions
See Also:
setMaxCount(int)

getTimeout

public long getTimeout()
Get current default timout value.

Returns:
current default timout value.
See Also:
setTimeout(long)

isEmpty

public boolean isEmpty()
Tests if this SessionManager maps no keys to values.

Returns:
true if this SessionManager maps no keys to values; false otherwise.

keys

public java.util.Iterator keys()
Returns an enumeration of the keys in this SessionManager.

Returns:
an enumeration of the keys in this SessionManager.

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Maps the specified key to the specified value in this SessionManager. If the key is null a new key is generated and returned. The value can NOT be null. The value can be retrieved by calling the get method with a key that is equal to the original key.

Parameters:
key - the key or null, then a new key is created.
value - the value.
Returns:
the used key to store the value.

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value,
                            int timeoutMilli)
Deprecated. 

Maps the specified key to the specified value in this SessionManager, using the SessionManager's timeout. If the key is null a new key is generated and returned. The value can NOT be null. The value can be retrieved by calling the get method with a key that is equal to the original key.

Parameters:
key - the key or null, then a new key is created.
value - the value.
timeoutMilli - a distinct timeout value for the given key
Returns:
the used key to store the value.

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value,
                            long timeoutMilli)
Maps the specified key to the specified value in this SessionManager, using the SessionManager's timeout. If the key is null a new key is generated and returned. The value can NOT be null. The value can be retrieved by calling the get method with a key that is equal to the original key.

Parameters:
key - the key or null, then a new key is created.
value - the value.
timeoutMilli - a distinct timeout value for the given key
Returns:
the used key to store the value.

getCreationMillis

public long getCreationMillis(java.lang.Object key)
Returns the creation time for the given key. This is the system time in milli seconds when the first put with that key occured.

Parameters:
key - the key
Returns:
creation time or -1 on invalid key.

getDeathMillis

public long getDeathMillis(java.lang.Object key)
Returns the death time for the given key. This is the system time in milli seconds when the object gets invalid. If the stored object implements the Callback interface, thie implementation of the Callback decides whether its lifetime gets renewed,

Parameters:
key - the key
Returns:
death time or -1 on invalid key.

removeButAsk

public java.lang.Object removeButAsk(java.lang.Object key)
Removes the key (and its corresponding value) from this SessionManager. This method does nothing if the key is not in the SessionManager. If value is an instance of Callback, its dontRemove() method is called, with key as param, then the removal depends on the result of that function.

Parameters:
key - the key that needs to be removed.
Returns:
the value to which the key had been mapped in this SessionManager, or null if the key did not have a mapping.

remove

public java.lang.Object remove(java.lang.Object key)
Removes the key (and its corresponding value) from this SessionManager. This method does nothing if the key is not in the SessionManager.

Parameters:
key - the key that needs to be removed.
Returns:
the value to which the key had been mapped in this SessionManager, or null if the key did not have a mapping.

setMaxCount

public void setMaxCount(int max)
Set a new maximum size. The new size only affects future invokations of put(...)!

Parameters:
max - the new max count of sessions
See Also:
getMaxCount()

setTimeout

public void setTimeout(long timeoutMilli)
Set a new default timout value. The new timout only affects future invokations of put(...) or touch(...)!

Parameters:
timeoutMilli - the new default timout value
See Also:
getTimeout()

size

public int size()
Returns the number of keys in this SessionManager.

Returns:
the number of keys in this SessionManager.

touch

public java.lang.Object touch(java.lang.Object key)
Sets the new timeout for the given key with the default timeout.

Parameters:
key - the key
Returns:
the key or null if the key was not found in the SessionManager.

touch

public java.lang.Object touch(java.lang.Object key,
                              long timeoutMilli)
Sets the new timeout for the given key.

Parameters:
key - the key
timeoutMilli - the new timeout to set
Returns:
the key or null if the key was not found in the SessionManager.

newKey

public static java.lang.String newKey()
Create a new session key.

Returns:
a new created String, containing a