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 theNativeSessionManager
interface, supportingSessionListener
s 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 void
afterStopped(Session session)
protected void
applyGlobalSessionTimeout(Session session)
protected Session
beforeInvalidNotification(Session session)
Returns the session instance to use to pass to registeredSessionListener
s 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 anInvalidSessionException
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 newSession Session
instance based on the specified (possiblynull
) 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.protected org.apache.shiro.event.EventBus
getEventBus()
Returns the EventBus used to publish SessionEvents.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 associatedSession
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), ornull
if no Session could be found.Collection<SessionListener>
getSessionListeners()
Date
getStartTimestamp(SessionKey key)
Returns the time the associatedSession
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)
Returnstrue
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 interestedSessionListener
s 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)
protected void
publishEvent(Object event)
Publishes events on the event bus if the event bus is non-null, otherwise does nothing.Object
removeAttribute(SessionKey sessionKey, Object attributeKey)
Removes (unbinds) the object bound to associatedSession
under the givenattributeKey
.void
setAttribute(SessionKey sessionKey, Object attributeKey, Object value)
Binds the specifiedvalue
to the associated session uniquely identified by theattributeKey
.void
setEventBus(org.apache.shiro.event.EventBus eventBus)
Sets the EventBus to use to publish SessionEvents.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 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:
setEventBus
in 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: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 underlyingSessionFactory
which could use the context to construct the internal Session instance in a specific manner. This allows pluggableSession
creation logic by simply injecting aSessionFactory
into theSessionManager
instance.- Specified by:
start
in interfaceSessionManager
- Parameters:
context
- the contextual initialization data that can be used by the implementation or underlyingSessionFactory
when instantiating the internalSession
instance.- Returns:
- the newly created session.
- See Also:
SessionFactory.createSession(SessionContext)
-
createSession
protected abstract Session createSession(SessionContext context) throws AuthorizationException
Creates a newSession Session
instance 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 underlyingSessionFactory
when instantiating the internalSession
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 justcreated
.context
- theSessionContext
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), ornull
if no Session could be found. If a session is found but invalid (stopped or expired), aSessionException
will be thrown.- Specified by:
getSession
in interfaceSessionManager
- Parameters:
key
- the Session key to use to look-up the Session- Returns:
- the
Session
instance corresponding to the given lookup key ornull
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 registeredSessionListener
s for notification that the session has been invalidated (stopped or expired). The default implementation returns anImmutableProxiedSession
instance to ensure that the specifiedsession
argument is not modified by any listeners.- Parameters:
session
- theSession
object being invalidated.- Returns:
- the
Session
instance to use to pass to registeredSessionListener
s for notification.
-
notifyStart
protected void notifyStart(Session session)
Notifies any interestedSessionListener
s that a Session has started. This method is invoked after theonStart
method is called.- Parameters:
session
- the session that has just started that will be delivered to anyregistered
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 associatedSession
started (was created).- Specified by:
getStartTimestamp
in interfaceNativeSessionManager
- 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 associatedSession
last interacted with the system.- Specified by:
getLastAccessTime
in 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:NativeSessionManager
Returns 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:
getTimeout
in 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:NativeSessionManager
Sets 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:
setTimeout
in 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:NativeSessionManager
Updates 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:
touch
in 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: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 returnsnull
- Specified by:
getHost
in 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:NativeSessionManager
Returns all attribute keys maintained by the target session or an empty collection if there are no attributes.- Specified by:
getAttributeKeys
in 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: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 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
attributeKey
ornull
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 specifiedvalue
to 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 thevalue
parameter is null, it has the same effect as if theNativeSessionManager.removeAttribute(SessionKey sessionKey, Object attributeKey)
method was called.- Specified by:
setAttribute
in interfaceNativeSessionManager
- Parameters:
sessionKey
- the session key to use to look up the target session.attributeKey
- the key under which thevalue
object 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:NativeSessionManager
Removes (unbinds) the object bound to associatedSession
under the givenattributeKey
.- Specified by:
removeAttribute
in 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
null
if 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:NativeSessionManager
Returnstrue
if the associated session is valid (it exists and is not stopped nor expired),false
otherwise.- Specified by:
isValid
in interfaceNativeSessionManager
- 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 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:NativeSessionManager
Returns quietly if the associated session is valid (it exists and is not stopped or expired) or throws anInvalidSessionException
indicating 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:
checkValid
in 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)
-
-