org.apache.shiro.session.mgt
Class AbstractNativeSessionManager

java.lang.Object
  extended by org.apache.shiro.session.mgt.AbstractSessionManager
      extended by org.apache.shiro.session.mgt.AbstractNativeSessionManager
All Implemented Interfaces:
NativeSessionManager, SessionManager
Direct Known Subclasses:
AbstractValidatingSessionManager

public abstract class AbstractNativeSessionManager
extends AbstractSessionManager
implements NativeSessionManager

Abstract implementation supporting the NativeSessionManager interface, supporting SessionListeners and application of the globalSessionTimeout.

Since:
1.0

Field Summary
 
Fields inherited from class org.apache.shiro.session.mgt.AbstractSessionManager
DEFAULT_GLOBAL_SESSION_TIMEOUT, MILLIS_PER_HOUR, MILLIS_PER_MINUTE, MILLIS_PER_SECOND
 
Constructor Summary
AbstractNativeSessionManager()
           
 
Method Summary
protected  void afterStopped(Session session)
           
protected  void applyGlobalSessionTimeout(Session session)
           
protected  Session beforeInvalidNotification(Session session)
          Returns the session instance to use to pass to registered SessionListeners for notification that the session has been invalidated (stopped or expired).
 void checkValid(SessionKey key)
          Returns quietly if the associated session is valid (it exists and is not stopped or expired) or throws an InvalidSessionException indicating that the session id is invalid.
protected  Session createExposedSession(Session session, SessionContext context)
           
protected  Session createExposedSession(Session session, SessionKey key)
           
protected abstract  Session createSession(SessionContext context)
          Creates a new Session Session instance based on the specified (possibly null) initialization data.
protected abstract  Session doGetSession(SessionKey key)
           
 Object getAttribute(SessionKey sessionKey, Object attributeKey)
          Returns the object bound to the associated session identified by the specified attribute key.
 Collection<Object> getAttributeKeys(SessionKey key)
          Returns all attribute keys maintained by the target session or an empty collection if there are no attributes.
 String getHost(SessionKey key)
          Returns the host name or IP string of the host where the session was started, if known.
 Date getLastAccessTime(SessionKey key)
          Returns the time the associated Session last interacted with the system.
 Session getSession(SessionKey key)
          Retrieves the session corresponding to the specified contextual data (such as a session ID if applicable), or null if no Session could be found.
 Collection<SessionListener> getSessionListeners()
           
 Date getStartTimestamp(SessionKey key)
          Returns the time the associated Session started (was created).
 long getTimeout(SessionKey key)
          Returns the time in milliseconds that the associated session may remain idle before expiring.
 boolean isValid(SessionKey key)
          Returns true if the associated session is valid (it exists and is not stopped nor expired), false otherwise.
protected  void notifyExpiration(Session session)
           
protected  void notifyStart(Session session)
          Notifies any interested SessionListeners that a Session has started.
protected  void notifyStop(Session session)
           
protected  void onChange(Session s)
           
protected  void onStart(Session session, SessionContext context)
          Template method that allows subclasses to react to a new session being created.
protected  void onStop(Session session)
           
protected  void onStop(Session session, SessionKey key)
           
 Object removeAttribute(SessionKey sessionKey, Object attributeKey)
          Removes (unbinds) the object bound to associated Session under the given attributeKey.
 void setAttribute(SessionKey sessionKey, Object attributeKey, Object value)
          Binds the specified value to the associated session uniquely identified by the attributeKey.
 void setSessionListeners(Collection<SessionListener> listeners)
           
 void setTimeout(SessionKey key, long maxIdleTimeInMillis)
          Sets the time in milliseconds that the associated session may remain idle before expiring.
 Session start(SessionContext context)
          Starts a new session based on the specified contextual initialization data, which can be used by the underlying implementation to determine how exactly to create the internal Session instance.
 void stop(SessionKey key)
          Explicitly stops the associated session, thereby releasing all of its resources.
 void touch(SessionKey key)
          Updates the last accessed time of the session identified by sessionId.
 
Methods inherited from class org.apache.shiro.session.mgt.AbstractSessionManager
getGlobalSessionTimeout, setGlobalSessionTimeout
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractNativeSessionManager

public AbstractNativeSessionManager()
Method Detail

setSessionListeners

public void setSessionListeners(Collection<SessionListener> listeners)

getSessionListeners

public Collection<SessionListener> getSessionListeners()

start

public Session start(SessionContext context)
Description copied from interface: SessionManager
Starts a new session based on the specified contextual initialization data, which can be used by the underlying implementation to determine how exactly to create the internal Session instance.

This method is mainly used in framework development, as the implementation will often relay the argument to an underlying SessionFactory which could use the context to construct the internal Session instance in a specific manner. This allows pluggable Session creation logic by simply injecting a SessionFactory into the SessionManager instance.

Specified by:
start in interface SessionManager
Parameters:
context - the contextual initialization data that can be used by the implementation or underlying SessionFactory when instantiating the internal Session instance.
Returns:
the newly created session.
See Also:
SessionFactory.createSession(SessionContext)

createSession

protected abstract Session createSession(SessionContext context)
                                  throws AuthorizationException
Creates a new Session Session instance based on the specified (possibly null) initialization data. Implementing classes must manage the persistent state of the returned session such that it could later be acquired via the getSession(SessionKey) method.

Parameters:
context - the initialization data that can be used by the implementation or underlying SessionFactory when instantiating the internal Session instance.
Returns:
the new Session instance.
Throws:
HostUnauthorizedException - if the system access control policy restricts access based on client location/IP and the specified hostAddress hasn't been enabled.
AuthorizationException - if the system access control policy does not allow the currently executing caller to start sessions.

applyGlobalSessionTimeout

protected void applyGlobalSessionTimeout(Session session)

onStart

protected void onStart(Session session,
                       SessionContext context)
Template method that allows subclasses to react to a new session being created.

This method is invoked before any session listeners are notified.

Parameters:
session - the session that was just created.
context - the SessionContext that was used to start the session.

getSession

public Session getSession(SessionKey key)
                   throws SessionException
Description copied from interface: SessionManager
Retrieves the session corresponding to the specified contextual data (such as a session ID if applicable), or null if no Session could be found. If a session is found but invalid (stopped or expired), a SessionException will be thrown.

Specified by:
getSession in interface SessionManager
Parameters:
key - the Session key to use to look-up the Session
Returns:
the Session instance corresponding to the given lookup key or null if no session could be acquired.
Throws:
SessionException - if a session was found but it was invalid (stopped/expired).

doGetSession

protected abstract Session doGetSession(SessionKey key)
                                 throws InvalidSessionException
Throws:
InvalidSessionException

createExposedSession

protected Session createExposedSession(Session session,
                                       SessionContext context)

createExposedSession

protected Session createExposedSession(Session session,
                                       SessionKey key)

beforeInvalidNotification

protected Session beforeInvalidNotification(Session session)
Returns the session instance to use to pass to registered SessionListeners for notification that the session has been invalidated (stopped or expired).

The default implementation returns an ImmutableProxiedSession instance to ensure that the specified session argument is not modified by any listeners.

Parameters:
session - the Session object being invalidated.
Returns:
the Session instance to use to pass to registered SessionListeners for notification.

notifyStart

protected void notifyStart(Session session)
Notifies any interested SessionListeners that a Session has started. This method is invoked after the onStart method is called.

Parameters:
session - the session that has just started that will be delivered to any registered session listeners.
See Also:
SessionListener.onStart(org.apache.shiro.session.Session)

notifyStop

protected void notifyStop(Session session)

notifyExpiration

protected void notifyExpiration(Session session)

getStartTimestamp

public Date getStartTimestamp(SessionKey key)
Description copied from interface: NativeSessionManager
Returns the time the associated Session started (was created).

Specified by:
getStartTimestamp in interface NativeSessionManager
Parameters:
key - the session key to use to look up the target session.
Returns:
the time the specified Session started (was created).
See Also:
Session.getStartTimestamp()

getLastAccessTime

public Date getLastAccessTime(SessionKey key)
Description copied from interface: NativeSessionManager
Returns the time the associated Session last interacted with the system.

Specified by:
getLastAccessTime in interface NativeSessionManager
Parameters:
key - the session key to use to look up the target session.
Returns:
time the session last accessed the system
See Also:
Session.getLastAccessTime(), Session.touch()

getTimeout

public long getTimeout(SessionKey key)
                throws InvalidSessionException
Description copied from interface: NativeSessionManager
Returns the time in milliseconds that the associated session may remain idle before expiring.

Specified by:
getTimeout in interface NativeSessionManager
Parameters:
key - the session key to use to look up the target session.
Returns:
the time in milliseconds that the associated session may remain idle before expiring.
Throws:
InvalidSessionException - if the session has been stopped or expired prior to calling this method.

setTimeout

public void setTimeout(SessionKey key,
                       long maxIdleTimeInMillis)
                throws InvalidSessionException
Description copied from interface: NativeSessionManager
Sets the time in milliseconds that the associated session may remain idle before expiring.

Specified by:
setTimeout in interface NativeSessionManager
Parameters:
key - the session key to use to look up the target session.
maxIdleTimeInMillis - the time in milliseconds that the associated session may remain idle before expiring.
Throws:
InvalidSessionException - if the session has been stopped or expired prior to calling this method.

touch

public void touch(SessionKey key)
           throws InvalidSessionException
Description copied from interface: NativeSessionManager
Updates the last accessed time of the session identified by sessionId. This can be used to explicitly ensure that a session does not time out.

Specified by:
touch in interface NativeSessionManager
Parameters:
key - the session key to use to look up the target session.
Throws:
InvalidSessionException - if the session has been stopped or expired prior to calling this method.
See Also:
Session.touch()

getHost

public String getHost(SessionKey key)
Description copied from interface: NativeSessionManager
Returns the host name or IP string of the host where the session was started, if known. If no host name or IP was specified when starting the session, this method returns null

Specified by:
getHost in interface NativeSessionManager
Parameters:
key - the session key to use to look up the target session.
Returns:
the host name or ip address of the host where the session originated, if known. If unknown, this method returns null.

getAttributeKeys

public Collection<Object> getAttributeKeys(SessionKey key)
Description copied from interface: NativeSessionManager
Returns all attribute keys maintained by the target session or an empty collection if there are no attributes.

Specified by:
getAttributeKeys in interface NativeSessionManager
Parameters:
key - the session key to use to look up the target session.
Returns:
all attribute keys maintained by the target session or an empty collection if there are no attributes.
See Also:
Session.getAttributeKeys()

getAttribute

public Object getAttribute(SessionKey sessionKey,
                           Object attributeKey)
                    throws InvalidSessionException
Description copied from interface: NativeSessionManager
Returns the object bound to the associated session identified by the specified attribute key. If there is no object bound under the attribute key for the given session, null is returned.

Specified by:
getAttribute in interface NativeSessionManager
Parameters:
sessionKey - session key to use to look up the target session.
attributeKey - the unique name of the object bound to the associated session
Returns:
the object bound under the attributeKey or null if there is no object bound.
Throws:
InvalidSessionException - if the specified session has stopped or expired prior to calling this method.
See Also:
Session.getAttribute(Object key)

setAttribute

public void setAttribute(SessionKey sessionKey,
                         Object attributeKey,
                         Object value)
                  throws InvalidSessionException
Description copied from interface: NativeSessionManager
Binds the specified value to the associated session uniquely identified by the attributeKey. If there is already a session attribute bound under the attributeKey, that existing object will be replaced by the new value.

If the value parameter is null, it has the same effect as if the NativeSessionManager.removeAttribute(SessionKey sessionKey, Object attributeKey) method was called.

Specified by:
setAttribute in interface NativeSessionManager
Parameters:
sessionKey - the session key to use to look up the target session.
attributeKey - the key under which the value object will be bound in this session
value - the object to bind in this session.
Throws:
InvalidSessionException - if the specified session has stopped or expired prior to calling this method.
See Also:
Session.setAttribute(Object key, Object value)

removeAttribute

public Object removeAttribute(SessionKey sessionKey,
                              Object attributeKey)
                       throws InvalidSessionException
Description copied from interface: NativeSessionManager
Removes (unbinds) the object bound to associated Session under the given attributeKey.

Specified by:
removeAttribute in interface NativeSessionManager
Parameters:
sessionKey - session key to use to look up the target session.
attributeKey - the key uniquely identifying the object to remove
Returns:
the object removed or null if there was no object bound under the specified attributeKey.
Throws:
InvalidSessionException - if the specified session has stopped or expired prior to calling this method.
See Also:
Session.removeAttribute(Object key)

isValid

public boolean isValid(SessionKey key)
Description copied from interface: NativeSessionManager
Returns true if the associated session is valid (it exists and is not stopped nor expired), false otherwise.

Specified by:
isValid in interface NativeSessionManager
Parameters:
key - the session key to use to look up the target session.
Returns:
true if the session is valid (exists and is not stopped or expired), false otherwise.

stop

public void stop(SessionKey key)
          throws InvalidSessionException
Description copied from interface: NativeSessionManager
Explicitly stops the associated session, thereby releasing all of its resources.

Specified by:
stop in interface NativeSessionManager
Parameters:
key - the session key to use to look up the target session.
Throws:
InvalidSessionException - if the session has stopped or expired prior to calling this method.
See Also:
Session.stop()

onStop

protected void onStop(Session session,
                      SessionKey key)

onStop

protected void onStop(Session session)

afterStopped

protected void afterStopped(Session session)

checkValid

public void checkValid(SessionKey key)
                throws InvalidSessionException
Description copied from interface: NativeSessionManager
Returns quietly if the associated session is valid (it exists and is not stopped or expired) or throws an InvalidSessionException indicating that the session id is invalid. This might be preferred to be used instead of NativeSessionManager.isValid(org.apache.shiro.session.mgt.SessionKey) since any exception thrown will definitively explain the reason for invalidation.

Specified by:
checkValid in interface NativeSessionManager
Parameters:
key - the session key to use to look up the target session.
Throws:
InvalidSessionException - if the session id is invalid (it does not exist or it is stopped or expired).

onChange

protected void onChange(Session s)


Copyright © 2004-2012 The Apache Software Foundation. All Rights Reserved.