com.billpringle.utils
Class WrpSession

java.lang.Object
  extended by com.billpringle.utils.WrpSession

public class WrpSession
extends java.lang.Object

This class encapsulates a singleton session object.

This is a singleton class that should be created using the static method WrpSession.initInstance() when a user authenticates with the system. Any subsequent calls to WrpSession.initInstance() will be ignored.

After that, a method can test to determine if the session has expired by calling the static WrpSession.getUserId() method, which will return -1 if expired, or the user ID that was passed to WrpSession.initInstance() at creation time.

This class also provides a HashMap for storing session level objects. The HashMap is defined using a key string and an object value, so any Java object (including arrays and complex objects) can be stored by one method and retrieved by another method.

© 2011 by Bill Pringle, all rights reserved.

Author:
Bill Pringle

Field Summary
private static WrpSession ref
          the actual singleton object
private static java.util.HashMap<java.lang.String,java.lang.Object> sessionMap
          HashMap for storing session information
private static int userId
          the user id for session - initialized
 
Constructor Summary
private WrpSession()
          Private constructor to prevent multiple instances.
 
Method Summary
 java.lang.Object clone()
           
static WrpSession getInstance()
          Get the singleton instance.
static java.lang.Object getMap(java.lang.String key)
          Get session attribute.
static int getUserid()
           
static WrpSession initInstance(int userId)
          Initialize the singleton.
static java.lang.Object setMap(java.lang.String key, java.lang.Object value)
          Store a session attribute.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ref

private static WrpSession ref
the actual singleton object


userId

private static int userId
the user id for session - initialized


sessionMap

private static java.util.HashMap<java.lang.String,java.lang.Object> sessionMap
HashMap for storing session information

Constructor Detail

WrpSession

private WrpSession()
Private constructor to prevent multiple instances. This method is called from the initInstance() method.

Method Detail

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Overrides:
clone in class java.lang.Object
Throws:
java.lang.CloneNotSupportedException

getInstance

public static WrpSession getInstance()
Get the singleton instance. If the singleton has not been initialized, this will return null.

Returns:
the singleton, or null if not initialized

initInstance

public static WrpSession initInstance(int userId)
Initialize the singleton. This method should be called first, and will create the singleton instance and initialize it.

Parameters:
userId - the current logged-in user ID

getUserid

public static int getUserid()
Returns:
the userid

getMap

public static java.lang.Object getMap(java.lang.String key)
Get session attribute.

Parameters:
key - attribute name
value - attribute value

setMap

public static java.lang.Object setMap(java.lang.String key,
                                      java.lang.Object value)
Store a session attribute.

Parameters:
key - attribute name
value - attribute value
Returns:
old attribute value, null if none