Package org.apache.shiro.session.mgt
Class AbstractNativeSessionManager
- java.lang.Object
-
- org.apache.shiro.session.mgt.AbstractSessionManager
-
- org.apache.shiro.session.mgt.AbstractNativeSessionManager
-
- All Implemented Interfaces:
org.apache.shiro.event.EventBusAware,NativeSessionManager,SessionManager
- Direct Known Subclasses:
AbstractValidatingSessionManager
public abstract class AbstractNativeSessionManager extends AbstractSessionManager implements NativeSessionManager, org.apache.shiro.event.EventBusAware
Abstract implementation supporting theNativeSessionManagerinterface, supportingSessionListeners and application of theglobalSessionTimeout.- 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
Constructors Constructor Description AbstractNativeSessionManager()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidafterStopped(Session session)protected voidapplyGlobalSessionTimeout(Session session)protected SessionbeforeInvalidNotification(Session session)Returns the session instance to use to pass to registeredSessionListeners for notification that the session has been invalidated (stopped or expired).voidcheckValid(SessionKey key)Returns quietly if the associated session is valid (it exists and is not stopped or expired) or throws anInvalidSessionExceptionindicating that the session id is invalid.protected SessioncreateExposedSession(Session session, SessionContext context)protected SessioncreateExposedSession(Session session, SessionKey key)protected abstract SessioncreateSession(SessionContext context)Creates a newSession Sessioninstance based on the specified (possiblynull) initialization data.protected abstract SessiondoGetSession(SessionKey key)ObjectgetAttribute(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.protected org.apache.shiro.event.EventBusgetEventBus()Returns the EventBus used to publish SessionEvents.StringgetHost(SessionKey key)Returns the host name or IP string of the host where the session was started, if known.DategetLastAccessTime(SessionKey key)Returns the time the associatedSessionlast interacted with the system.SessiongetSession(SessionKey key)Retrieves the session corresponding to the specified contextual data (such as a session ID if applicable), ornullif no Session could be found.Collection<SessionListener>getSessionListeners()DategetStartTimestamp(SessionKey key)Returns the time the associatedSessionstarted (was created).longgetTimeout(SessionKey key)Returns the time in milliseconds that the associated session may remain idle before expiring.booleanisValid(SessionKey key)Returnstrueif the associated session is valid (it exists and is not stopped nor expired),falseotherwise.protected voidnotifyExpiration(Session session)protected voidnotifyStart(Session session)Notifies any interestedSessionListeners that a Session has started.protected voidnotifyStop(Session session)protected voidonChange(Session s)protected voidonStart(Session session, SessionContext context)Template method that allows subclasses to react to a new session being created.protected voidonStop(Session session)protected voidonStop(Session session, SessionKey key)protected voidpublishEvent(Object event)Publishes events on the event bus if the event bus is non-null, otherwise does nothing.ObjectremoveAttribute(SessionKey sessionKey, Object attributeKey)Removes (unbinds) the object bound to associatedSessionunder the givenattributeKey.voidsetAttribute(SessionKey sessionKey, Object attributeKey, Object value)Binds the specifiedvalueto the associated session uniquely identified by theattributeKey.voidsetEventBus(org.apache.shiro.event.EventBus eventBus)Sets the EventBus to use to publish SessionEvents.voidsetSessionListeners(Collection<SessionListener> listeners)voidsetTimeout(SessionKey key, long maxIdleTimeInMillis)Sets the time in milliseconds that the associated session may remain idle before expiring.Sessionstart(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.voidstop(SessionKey key)Explicitly stops the associated session, thereby releasing all of its resources.voidtouch(SessionKey key)Updates the last accessed time of the session identified bysessionId.-
Methods inherited from class org.apache.shiro.session.mgt.AbstractSessionManager
getGlobalSessionTimeout, setGlobalSessionTimeout
-
-
-
-
Method Detail
-
setSessionListeners
public void setSessionListeners(Collection<SessionListener> listeners)
-
getSessionListeners
public Collection<SessionListener> getSessionListeners()
-
getEventBus
protected org.apache.shiro.event.EventBus getEventBus()
Returns the EventBus used to publish SessionEvents.- Returns:
- the EventBus used to publish SessionEvents.
- Since:
- 1.3
-
setEventBus
public void setEventBus(org.apache.shiro.event.EventBus eventBus)
Sets the EventBus to use to publish SessionEvents.- Specified by:
setEventBusin interfaceorg.apache.shiro.event.EventBusAware- Parameters:
eventBus- the EventBus to use to publish SessionEvents.- Since:
- 1.3
-
publishEvent
protected void publishEvent(Object event)
Publishes events on the event bus if the event bus is non-null, otherwise does nothing.- Parameters:
event- the event to publish on the event bus if the event bus exists.- Since:
- 1.3
-
start
public Session start(SessionContext context)
Description copied from interface:SessionManagerStarts 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 underlyingSessionFactorywhich could use the context to construct the internal Session instance in a specific manner. This allows pluggableSessioncreation logic by simply injecting aSessionFactoryinto theSessionManagerinstance.- Specified by:
startin interfaceSessionManager- Parameters:
context- the contextual initialization data that can be used by the implementation or underlyingSessionFactorywhen instantiating the internalSessioninstance.- Returns:
- the newly created session.
- See Also:
SessionFactory.createSession(SessionContext)
-
createSession
protected abstract Session createSession(SessionContext context) throws AuthorizationException
Creates a newSession Sessioninstance based on the specified (possiblynull) initialization data. Implementing classes must manage the persistent state of the returned session such that it could later be acquired via thegetSession(SessionKey)method.- Parameters:
context- the initialization data that can be used by the implementation or underlyingSessionFactorywhen instantiating the internalSessioninstance.- Returns:
- the new
Sessioninstance. - 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 justcreated.context- theSessionContextthat was used to start the session.
-
getSession
public Session getSession(SessionKey key) throws SessionException
Description copied from interface:SessionManagerRetrieves the session corresponding to the specified contextual data (such as a session ID if applicable), ornullif no Session could be found. If a session is found but invalid (stopped or expired), aSessionExceptionwill be thrown.- Specified by:
getSessionin interfaceSessionManager- Parameters:
key- the Session key to use to look-up the Session- Returns:
- the
Sessioninstance corresponding to the given lookup key ornullif 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 registeredSessionListeners for notification that the session has been invalidated (stopped or expired). The default implementation returns anImmutableProxiedSessioninstance to ensure that the specifiedsessionargument is not modified by any listeners.- Parameters:
session- theSessionobject being invalidated.- Returns:
- the
Sessioninstance to use to pass to registeredSessionListeners for notification.
-
notifyStart
protected void notifyStart(Session session)
Notifies any interestedSessionListeners that a Session has started. This method is invoked after theonStartmethod is called.- Parameters:
session- the session that has just started that will be delivered to anyregisteredsession 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:NativeSessionManagerReturns the time the associatedSessionstarted (was created).- Specified by:
getStartTimestampin interfaceNativeSessionManager- Parameters:
key- the session key to use to look up the target session.- Returns:
- the time the specified
Sessionstarted (was created). - See Also:
Session.getStartTimestamp()
-
getLastAccessTime
public Date getLastAccessTime(SessionKey key)
Description copied from interface:NativeSessionManagerReturns the time the associatedSessionlast interacted with the system.- Specified by:
getLastAccessTimein interfaceNativeSessionManager- 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:NativeSessionManagerReturns the time in milliseconds that the associated session may remain idle before expiring.- A negative return value means the session will never expire.
- A non-negative return value (0 or greater) means the session expiration will occur if idle for that length of time.
- Specified by:
getTimeoutin interfaceNativeSessionManager- 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:NativeSessionManagerSets the time in milliseconds that the associated session may remain idle before expiring.- A negative return value means the session will never expire.
- A non-negative return value (0 or greater) means the session expiration will occur if idle for that length of time.
- Specified by:
setTimeoutin interfaceNativeSessionManager- 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:NativeSessionManagerUpdates the last accessed time of the session identified bysessionId. This can be used to explicitly ensure that a session does not time out.- Specified by:
touchin interfaceNativeSessionManager- 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:NativeSessionManagerReturns 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 returnsnull- Specified by:
getHostin interfaceNativeSessionManager- 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:NativeSessionManagerReturns all attribute keys maintained by the target session or an empty collection if there are no attributes.- Specified by:
getAttributeKeysin interfaceNativeSessionManager- 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:NativeSessionManagerReturns 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,nullis returned.- Specified by:
getAttributein interfaceNativeSessionManager- 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
attributeKeyornullif 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:NativeSessionManagerBinds the specifiedvalueto the associated session uniquely identified by theattributeKey. If there is already a session attribute bound under theattributeKey, that existing object will be replaced by the newvalue. If thevalueparameter is null, it has the same effect as if theNativeSessionManager.removeAttribute(SessionKey sessionKey, Object attributeKey)method was called.- Specified by:
setAttributein interfaceNativeSessionManager- Parameters:
sessionKey- the session key to use to look up the target session.attributeKey- the key under which thevalueobject will be bound in this sessionvalue- 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:NativeSessionManagerRemoves (unbinds) the object bound to associatedSessionunder the givenattributeKey.- Specified by:
removeAttributein interfaceNativeSessionManager- 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
nullif there was no object bound under the specifiedattributeKey. - 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:NativeSessionManagerReturnstrueif the associated session is valid (it exists and is not stopped nor expired),falseotherwise.- Specified by:
isValidin interfaceNativeSessionManager- Parameters:
key- the session key to use to look up the target session.- Returns:
trueif the session is valid (exists and is not stopped or expired),falseotherwise.
-
stop
public void stop(SessionKey key) throws InvalidSessionException
Description copied from interface:NativeSessionManagerExplicitly stops the associated session, thereby releasing all of its resources.- Specified by:
stopin interfaceNativeSessionManager- 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:NativeSessionManagerReturns quietly if the associated session is valid (it exists and is not stopped or expired) or throws anInvalidSessionExceptionindicating that the session id is invalid. This might be preferred to be used instead ofNativeSessionManager.isValid(org.apache.shiro.session.mgt.SessionKey)since any exception thrown will definitively explain the reason for invalidation.- Specified by:
checkValidin interfaceNativeSessionManager- 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)
-
-