org.apache.commons.messagelet.impl
Class HttpSessionImpl

java.lang.Object
  extended by org.apache.commons.messagelet.impl.HttpSessionImpl
All Implemented Interfaces:
java.io.Serializable, javax.servlet.http.HttpSession

public class HttpSessionImpl
extends java.lang.Object
implements javax.servlet.http.HttpSession, java.io.Serializable

Based on the Catalina StandardSession class. Standard implementation of the HttpSession interface. This object is serializable, so that it can be stored in persistent storage or transferred to a different JVM for distributable session support.

Version:
$Revision: 155459 $ $Date: 2005-02-26 14:24:44 +0100(sab, 26 feb 2005) $
Author:
Craig R. McClanahan, Sean Legassick, Jon S. Stevens, James Strachan
See Also:
Serialized Form

Field Summary
private  java.util.HashMap attributes
          The collection of user data attributes associated with this Session.
private  java.lang.String authType
          The authentication type used to authenticate our cached Principal, if any.
private  long creationTime
          The time this session was created, in milliseconds since midnight, January 1, 1970 GMT.
private  int debug
          The debugging detail level for this component.
private  boolean distributable
          Is this session distributable.
private  boolean expiring
          We are currently processing a session expiration, so bypass certain IllegalStateException tests.
private  java.lang.String id
          The session identifier of this Session.
private  boolean isNew
          Flag indicating whether this session is new or not.
private  boolean isValid
          Flag indicating whether this session is valid or not.
private  long lastAccessedTime
          The last accessed time for this Session.
private  java.util.ArrayList listeners
          The session event listeners for this Session.
private  int maxInactiveInterval
          The maximum time interval, in seconds, between client requests before the servlet container may invalidate this session.
private static java.lang.String NOT_SERIALIZED
          The dummy attribute value serialized when a NotSerializableException is encountered in writeObject().
private  java.util.HashMap notes
          Internal notes associated with this session by Catalina components and event listeners.
private  java.security.Principal principal
          The authenticated Principal associated with this session, if any.
protected  javax.servlet.ServletContext servletContext
          The ServletContext
private  java.beans.PropertyChangeSupport support
          The property change support for this component.
private  long thisAccessedTime
          The current accessed time for this session.
 
Constructor Summary
HttpSessionImpl(javax.servlet.ServletContext servletContext)
           
 
Method Summary
 void access()
          Update the accessed time information for this session.
 void activate()
          Perform internal processing required to activate this session.
 void expire()
          Perform the internal processing required to invalidate this session, without triggering an exception if the session has already expired.
 void fireSessionEvent(java.lang.String type, java.lang.Object data)
          Notify all session event listeners that a particular event has occurred for this Session.
 java.lang.Object getAttribute(java.lang.String name)
          Return the object bound with the specified name in this session, or null if no object is bound with that name.
 java.util.Enumeration getAttributeNames()
          Return an Enumeration of String objects containing the names of the objects bound to this session.
 java.lang.String getAuthType()
          Return the authentication type used to authenticate our cached Principal, if any.
 long getCreationTime()
          Return the time when this session was created, in milliseconds since midnight, January 1, 1970 GMT.
 java.lang.String getId()
          Return the session identifier for this session.
 long getLastAccessedTime()
          Return the last time the client sent a request associated with this session, as the number of milliseconds since midnight, January 1, 1970 GMT.
 int getMaxInactiveInterval()
          Return the maximum time interval, in seconds, between client requests before the servlet container will invalidate the session.
 java.lang.Object getNote(java.lang.String name)
          Return the object bound with the specified name to the internal notes for this session, or null if no such binding exists.
 java.util.Iterator getNoteNames()
          Return an Iterator containing the String names of all notes bindings that exist for this session.
 java.security.Principal getPrincipal()
          Return the authenticated Principal that is associated with this Session.
 javax.servlet.ServletContext getServletContext()
          Return the ServletContext to which this session belongs.
 javax.servlet.http.HttpSessionContext getSessionContext()
          Deprecated. As of Version 2.1, this method is deprecated and has no replacement. It will be removed in a future version of the Java Servlet API.
 java.lang.Object getValue(java.lang.String name)
          Deprecated. As of Version 2.2, this method is replaced by getAttribute()
 java.lang.String[] getValueNames()
          Deprecated. As of Version 2.2, this method is replaced by getAttributeNames()
 void invalidate()
          Invalidates this session and unbinds any objects bound to it.
 boolean isNew()
          Return true if the client does not yet know about the session, or if the client chooses not to join the session.
 boolean isValid()
          Return the isValid flag for this session.
private  java.lang.String[] keys()
          Return the names of all currently defined session attributes as an array of Strings.
protected  void log(java.lang.String message)
          Log a message to the current ServletContext
protected  void log(java.lang.String message, java.lang.Throwable throwable)
          Log a message to the current ServletContext
 void passivate()
          Perform the internal processing required to passivate this session.
 void putValue(java.lang.String name, java.lang.Object value)
          Deprecated. As of Version 2.2, this method is replaced by setAttribute()
private  void readObject(java.io.ObjectInputStream stream)
          Read a serialized version of this session object from the specified object input stream.
(package private)  void readObjectData(java.io.ObjectInputStream stream)
          Read a serialized version of the contents of this session object from the specified object input stream, without requiring that the StandardSession itself have been serialized.
 void recycle()
          Release all object references, and initialize instance variables, in preparation for reuse of this object.
 void removeAttribute(java.lang.String name)
          Remove the object bound with the specified name from this session.
 void removeNote(java.lang.String name)
          Remove any object bound to the specified name in the internal notes for this session.
 void removeValue(java.lang.String name)
          Deprecated. As of Version 2.2, this method is replaced by removeAttribute()
 void setAttribute(java.lang.String name, java.lang.Object value)
          Bind an object to this session, using the specified name.
 void setAuthType(java.lang.String authType)
          Set the authentication type used to authenticate our cached Principal, if any.
 void setCreationTime(long time)
          Set the creation time for this session.
 void setId(java.lang.String id)
          Set the session identifier for this session.
 void setMaxInactiveInterval(int interval)
          Set the maximum time interval, in seconds, between client requests before the servlet container will invalidate the session.
 void setNew(boolean isNew)
          Set the isNew flag for this session.
 void setNote(java.lang.String name, java.lang.Object value)
          Bind an object to a specified name in the internal notes associated with this session, replacing any existing binding for this name.
 void setPrincipal(java.security.Principal principal)
          Set the authenticated Principal that is associated with this Session.
 void setValid(boolean isValid)
          Set the isValid flag for this session.
 java.lang.String toString()
          Return a string representation of this object.
private  void writeObject(java.io.ObjectOutputStream stream)
          Write a serialized version of this session object to the specified object output stream.
(package private)  void writeObjectData(java.io.ObjectOutputStream stream)
          Write a serialized version of the contents of this session object to the specified object output stream, without requiring that the StandardSession itself have been serialized.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NOT_SERIALIZED

private static final java.lang.String NOT_SERIALIZED
The dummy attribute value serialized when a NotSerializableException is encountered in writeObject().

See Also:
Constant Field Values

attributes

private java.util.HashMap attributes
The collection of user data attributes associated with this Session.


authType

private transient java.lang.String authType
The authentication type used to authenticate our cached Principal, if any. NOTE: This value is not included in the serialized version of this object.


creationTime

private long creationTime
The time this session was created, in milliseconds since midnight, January 1, 1970 GMT.


debug

private transient int debug
The debugging detail level for this component. NOTE: This value is not included in the serialized version of this object.


expiring

private transient boolean expiring
We are currently processing a session expiration, so bypass certain IllegalStateException tests. NOTE: This value is not included in the serialized version of this object.


id

private java.lang.String id
The session identifier of this Session.


lastAccessedTime

private long lastAccessedTime
The last accessed time for this Session.


listeners

private transient java.util.ArrayList listeners
The session event listeners for this Session.


maxInactiveInterval

private int maxInactiveInterval
The maximum time interval, in seconds, between client requests before the servlet container may invalidate this session. A negative time indicates that the session should never time out.


isNew

private boolean isNew
Flag indicating whether this session is new or not.


isValid

private boolean isValid
Flag indicating whether this session is valid or not.


notes

private transient java.util.HashMap notes
Internal notes associated with this session by Catalina components and event listeners. IMPLEMENTATION NOTE: This object is not saved and restored across session serializations!


principal

private transient java.security.Principal principal
The authenticated Principal associated with this session, if any. IMPLEMENTATION NOTE: This object is not saved and restored across session serializations!


support

private transient java.beans.PropertyChangeSupport support
The property change support for this component. NOTE: This value is not included in the serialized version of this object.


thisAccessedTime

private long thisAccessedTime
The current accessed time for this session.


servletContext

protected javax.servlet.ServletContext servletContext
The ServletContext


distributable

private boolean distributable
Is this session distributable. If so then its values must be Serializable.

Constructor Detail

HttpSessionImpl

public HttpSessionImpl(javax.servlet.ServletContext servletContext)
Method Detail

getAuthType

public java.lang.String getAuthType()
Return the authentication type used to authenticate our cached Principal, if any.


setAuthType

public void setAuthType(java.lang.String authType)
Set the authentication type used to authenticate our cached Principal, if any.

Parameters:
authType - The new cached authentication type

setCreationTime

public void setCreationTime(long time)
Set the creation time for this session. This method is called by the Manager when an existing Session instance is reused.

Parameters:
time - The new creation time

getId

public java.lang.String getId()
Return the session identifier for this session.

Specified by:
getId in interface javax.servlet.http.HttpSession

setId

public void setId(java.lang.String id)
Set the session identifier for this session.

Parameters:
id - The new session identifier

getLastAccessedTime

public long getLastAccessedTime()
Return the last time the client sent a request associated with this session, as the number of milliseconds since midnight, January 1, 1970 GMT. Actions that your application takes, such as getting or setting a value associated with the session, do not affect the access time.

Specified by:
getLastAccessedTime in interface javax.servlet.http.HttpSession

getMaxInactiveInterval

public int getMaxInactiveInterval()
Return the maximum time interval, in seconds, between client requests before the servlet container will invalidate the session. A negative time indicates that the session should never time out.

Specified by:
getMaxInactiveInterval in interface javax.servlet.http.HttpSession
Throws:
java.lang.IllegalStateException - if this method is called on an invalidated session

setMaxInactiveInterval

public void setMaxInactiveInterval(int interval)
Set the maximum time interval, in seconds, between client requests before the servlet container will invalidate the session. A negative time indicates that the session should never time out.

Specified by:
setMaxInactiveInterval in interface javax.servlet.http.HttpSession
Parameters:
interval - The new maximum interval

setNew

public void setNew(boolean isNew)
Set the isNew flag for this session.

Parameters:
isNew - The new value for the isNew flag

getPrincipal

public java.security.Principal getPrincipal()
Return the authenticated Principal that is associated with this Session. This provides an Authenticator with a means to cache a previously authenticated Principal, and avoid potentially expensive Realm.authenticate() calls on every request. If there is no current associated Principal, return null.


setPrincipal

public void setPrincipal(java.security.Principal principal)
Set the authenticated Principal that is associated with this Session. This provides an Authenticator with a means to cache a previously authenticated Principal, and avoid potentially expensive Realm.authenticate() calls on every request.

Parameters:
principal - The new Principal, or null if none

isValid

public boolean isValid()
Return the isValid flag for this session.


setValid

public void setValid(boolean isValid)
Set the isValid flag for this session.

Parameters:
isValid - The new value for the isValid flag

access

public void access()
Update the accessed time information for this session. This method should be called by the context when a request comes in for a particular session, even if the application does not reference it.


expire

public void expire()
Perform the internal processing required to invalidate this session, without triggering an exception if the session has already expired.


passivate

public void passivate()
Perform the internal processing required to passivate this session.


activate

public void activate()
Perform internal processing required to activate this session.


getNote

public java.lang.Object getNote(java.lang.String name)
Return the object bound with the specified name to the internal notes for this session, or null if no such binding exists.

Parameters:
name - Name of the note to be returned

getNoteNames

public java.util.Iterator getNoteNames()
Return an Iterator containing the String names of all notes bindings that exist for this session.


recycle

public void recycle()
Release all object references, and initialize instance variables, in preparation for reuse of this object.


removeNote

public void removeNote(java.lang.String name)
Remove any object bound to the specified name in the internal notes for this session.

Parameters:
name - Name of the note to be removed

setNote

public void setNote(java.lang.String name,
                    java.lang.Object value)
Bind an object to a specified name in the internal notes associated with this session, replacing any existing binding for this name.

Parameters:
name - Name to which the object should be bound
value - Object to be bound to the specified name

toString

public java.lang.String toString()
Return a string representation of this object.

Overrides:
toString in class java.lang.Object

readObjectData

void readObjectData(java.io.ObjectInputStream stream)
              throws java.lang.ClassNotFoundException,
                     java.io.IOException
Read a serialized version of the contents of this session object from the specified object input stream, without requiring that the StandardSession itself have been serialized.

Parameters:
stream - The object input stream to read from
Throws:
java.lang.ClassNotFoundException - if an unknown class is specified
java.io.IOException - if an input/output error occurs

writeObjectData

void writeObjectData(java.io.ObjectOutputStream stream)
               throws java.io.IOException
Write a serialized version of the contents of this session object to the specified object output stream, without requiring that the StandardSession itself have been serialized.

Parameters:
stream - The object output stream to write to
Throws:
java.io.IOException - if an input/output error occurs

getCreationTime

public long getCreationTime()
Return the time when this session was created, in milliseconds since midnight, January 1, 1970 GMT.

Specified by:
getCreationTime in interface javax.servlet.http.HttpSession
Throws:
java.lang.IllegalStateException - if this method is called on an invalidated session

getServletContext

public javax.servlet.ServletContext getServletContext()
Return the ServletContext to which this session belongs.

Specified by:
getServletContext in interface javax.servlet.http.HttpSession

getSessionContext

public javax.servlet.http.HttpSessionContext getSessionContext()
Deprecated. As of Version 2.1, this method is deprecated and has no replacement. It will be removed in a future version of the Java Servlet API.

Return the session context with which this session is associated.

Specified by:
getSessionContext in interface javax.servlet.http.HttpSession

getAttribute

public java.lang.Object getAttribute(java.lang.String name)
Return the object bound with the specified name in this session, or null if no object is bound with that name.

Specified by:
getAttribute in interface javax.servlet.http.HttpSession
Parameters:
name - Name of the attribute to be returned
Throws:
java.lang.IllegalStateException - if this method is called on an invalidated session

getAttributeNames

public java.util.Enumeration getAttributeNames()
Return an Enumeration of String objects containing the names of the objects bound to this session.

Specified by:
getAttributeNames in interface javax.servlet.http.HttpSession
Throws:
java.lang.IllegalStateException - if this method is called on an invalidated session

getValue

public java.lang.Object getValue(java.lang.String name)
Deprecated. As of Version 2.2, this method is replaced by getAttribute()

Return the object bound with the specified name in this session, or null if no object is bound with that name.

Specified by:
getValue in interface javax.servlet.http.HttpSession
Parameters:
name - Name of the value to be returned
Throws:
java.lang.IllegalStateException - if this method is called on an invalidated session

getValueNames

public java.lang.String[] getValueNames()
Deprecated. As of Version 2.2, this method is replaced by getAttributeNames()

Return the set of names of objects bound to this session. If there are no such objects, a zero-length array is returned.

Specified by:
getValueNames in interface javax.servlet.http.HttpSession
Throws:
java.lang.IllegalStateException - if this method is called on an invalidated session

invalidate

public void invalidate()
Invalidates this session and unbinds any objects bound to it.

Specified by:
invalidate in interface javax.servlet.http.HttpSession
Throws:
java.lang.IllegalStateException - if this method is called on an invalidated session

isNew

public boolean isNew()
Return true if the client does not yet know about the session, or if the client chooses not to join the session. For example, if the server used only cookie-based sessions, and the client has disabled the use of cookies, then a session would be new on each request.

Specified by:
isNew in interface javax.servlet.http.HttpSession
Throws:
java.lang.IllegalStateException - if this method is called on an invalidated session

putValue

public void putValue(java.lang.String name,
                     java.lang.Object value)
Deprecated. As of Version 2.2, this method is replaced by setAttribute()

Bind an object to this session, using the specified name. If an object of the same name is already bound to this session, the object is replaced.

After this method executes, and if the object implements HttpSessionBindingListener, the container calls valueBound() on the object.

Specified by:
putValue in interface javax.servlet.http.HttpSession
Parameters:
name - Name to which the object is bound, cannot be null
value - Object to be bound, cannot be null
Throws:
java.lang.IllegalStateException - if this method is called on an invalidated session

removeAttribute

public void removeAttribute(java.lang.String name)
Remove the object bound with the specified name from this session. If the session does not have an object bound with this name, this method does nothing.

After this method executes, and if the object implements HttpSessionBindingListener, the container calls valueUnbound() on the object.

Specified by:
removeAttribute in interface javax.servlet.http.HttpSession
Parameters:
name - Name of the object to remove from this session.
Throws:
java.lang.IllegalStateException - if this method is called on an invalidated session

removeValue

public void removeValue(java.lang.String name)
Deprecated. As of Version 2.2, this method is replaced by removeAttribute()

Remove the object bound with the specified name from this session. If the session does not have an object bound with this name, this method does nothing.

After this method executes, and if the object implements HttpSessionBindingListener, the container calls valueUnbound() on the object.

Specified by:
removeValue in interface javax.servlet.http.HttpSession
Parameters:
name - Name of the object to remove from this session.
Throws:
java.lang.IllegalStateException - if this method is called on an invalidated session

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.Object value)
Bind an object to this session, using the specified name. If an object of the same name is already bound to this session, the object is replaced.

After this method executes, and if the object implements HttpSessionBindingListener, the container calls valueBound() on the object.

Specified by:
setAttribute in interface javax.servlet.http.HttpSession
Parameters:
name - Name to which the object is bound, cannot be null
value - Object to be bound, cannot be null
Throws:
java.lang.IllegalArgumentException - if an attempt is made to add a non-serializable object in an environment marked distributable.
java.lang.IllegalStateException - if this method is called on an invalidated session

readObject

private void readObject(java.io.ObjectInputStream stream)
                 throws java.lang.ClassNotFoundException,
                        java.io.IOException
Read a serialized version of this session object from the specified object input stream.

IMPLEMENTATION NOTE: The reference to the owning Manager is not restored by this method, and must be set explicitly.

Parameters:
stream - The input stream to read from
Throws:
java.lang.ClassNotFoundException - if an unknown class is specified
java.io.IOException - if an input/output error occurs

writeObject

private void writeObject(java.io.ObjectOutputStream stream)
                  throws java.io.IOException
Write a serialized version of this session object to the specified object output stream.

IMPLEMENTATION NOTE: The owning Manager will not be stored in the serialized representation of this Session. After calling readObject(), you must set the associated Manager explicitly.

IMPLEMENTATION NOTE: Any attribute that is not Serializable will be unbound from the session, with appropriate actions if it implements HttpSessionBindingListener. If you do not want any such attributes, be sure the distributable property of the associated Manager is set to true.

Parameters:
stream - The output stream to write to
Throws:
java.io.IOException - if an input/output error occurs

fireSessionEvent

public void fireSessionEvent(java.lang.String type,
                             java.lang.Object data)
Notify all session event listeners that a particular event has occurred for this Session. The default implementation performs this notification synchronously using the calling thread.

Parameters:
type - Event type
data - Event data

keys

private java.lang.String[] keys()
Return the names of all currently defined session attributes as an array of Strings. If there are no defined attributes, a zero-length array is returned.


log

protected void log(java.lang.String message)
Log a message to the current ServletContext

Parameters:
message - Message to be logged

log

protected void log(java.lang.String message,
                   java.lang.Throwable throwable)
Log a message to the current ServletContext

Parameters:
message - Message to be logged
throwable - Associated exception