Package org.eclipse.jetty.session
Class AbstractSessionCache
java.lang.Object
org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.component.ContainerLifeCycle
org.eclipse.jetty.session.AbstractSessionCache
- All Implemented Interfaces:
SessionCache
,Container
,Destroyable
,Dumpable
,Dumpable.DumpableContainer
,LifeCycle
- Direct Known Subclasses:
DefaultSessionCache
,NullSessionCache
@ManagedObject
public abstract class AbstractSessionCache
extends ContainerLifeCycle
implements SessionCache
AbstractSessionCache
A base implementation of the
SessionCache
interface for managing a set of
Session objects pertaining to a context in memory.
This implementation ensures that multiple requests for the same session id
always return the same Session object.
It will delay writing out a session to the SessionDataStore until the
last request exits the session. If the SessionDataStore supports passivation
then the session passivation and activation listeners are called appropriately as
the session is written.
This implementation also supports evicting idle Session objects. An idle Session
is one that is still valid, has not expired, but has not been accessed by a
request for a configurable amount of time. An idle session will be first
passivated before it is evicted from the cache.-
Nested Class Summary
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener, AbstractLifeCycle.StopException
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Container
Container.InheritedListener, Container.Listener
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Dumpable
Dumpable.DumpableContainer
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle
LifeCycle.Listener
-
Field Summary
Modifier and TypeFieldDescriptionprotected SessionContext
Information about the context to which this SessionCache pertainsprotected int
When, if ever, to evict sessions: never; only when the last request for them finishes; after inactivity time (expressed as secs)protected boolean
If true, when a response is about to be committed back to the client, a dirty session will be flushed to the session store.protected boolean
If true, when the server shuts down, all sessions in the cache will be invalidated before being removed.protected final SessionManager
The SessionManager related to this SessionCacheprotected boolean
If true, a Session whose data cannot be read will be deleted from the SessionDataStore.protected boolean
If true, as soon as a new session is created, it will be persisted to the SessionDataStoreprotected boolean
If true, a session that will be evicted from the cache because it has been inactive too long will be saved before being evicted.protected SessionDataStore
The authoritative source of session dataFields inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
FAILED, STARTED, STARTING, STOPPED, STOPPING
Fields inherited from interface org.eclipse.jetty.session.SessionCache
EVICT_ON_INACTIVITY, EVICT_ON_SESSION_EXIT, NEVER_EVICT
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(String id, ManagedSession session) Add an entirely new session (created by the application calling Request.getSession(true)) to the cache.checkExpiration
(Set<String> candidates) Check a list of session ids that belong to potentially expired sessions.void
checkInactiveSession
(ManagedSession session) Check a session for being inactive and thus being able to be evicted, if eviction is enabled.void
commit
(ManagedSession session) A response that has accessed this session is about to be returned to the client.boolean
Check to see if this cache contains an entry for the session corresponding to the session id.Remove a session object from this store and from any backing store.protected abstract ManagedSession
doComputeIfAbsent
(String id, Function<String, ManagedSession> mappingFunction) Compute the mappingFunction to create a Session object iff the session with the given id isn't already in the map, otherwise return the existing Session.abstract ManagedSession
Remove the session with this identity from the storeprotected abstract ManagedSession
Get the session matching the key from the cache.protected abstract Session
doPutIfAbsent
(String id, ManagedSession session) Put the session into the map if it wasn't already thereprotected abstract boolean
doReplace
(String id, ManagedSession oldValue, ManagedSession newValue) Replace the mapping from id to oldValue with newValueprotected void
doStart()
Starts the managed lifecycle beans in the order they were added.protected void
doStop()
Stops the managed lifecycle beans in the reverse order they were added.boolean
Check to see if a session corresponding to the id exists.Get a session object.protected ManagedSession
getAndEnter
(String id, boolean enter) Get a session object.int
void
initialize
(SessionContext context) boolean
boolean
boolean
boolean
boolean
Whether we should save a session that has been inactive before we boot it from the cache.newSession
(String id, long time, long maxInactiveMs) Create an entirely new Session.abstract ManagedSession
newSession
(SessionData data) Create a new Session object from pre-existing session datavoid
release
(ManagedSession session) Finish using the Session object.renewSessionId
(String oldId, String newId, String oldExtendedId, String newExtendedId) Change the id of a Session.protected void
renewSessionId
(ManagedSession session, String newId, String newExtendedId) Swap the id on a session.void
setEvictionPolicy
(int evictionTimeout) -1 means we never evict inactive sessions.void
setFlushOnResponseCommit
(boolean flushOnResponseCommit) If true, a dirty session will be written to the SessionDataStore just before a response is returned to the client.void
setInvalidateOnShutdown
(boolean invalidateOnShutdown) If true, all existing sessions in the cache will be invalidated when the server shuts down.void
setRemoveUnloadableSessions
(boolean removeUnloadableSessions) If a session's data cannot be loaded from the store without error, remove it from the persistent store.void
setSaveOnCreate
(boolean saveOnCreate) Whether or not a session that is newly created should be immediately saved.void
setSaveOnInactiveEviction
(boolean saveOnEvict) Whether or not a a session that is about to be evicted should be saved before being evicted.void
setSessionDataStore
(SessionDataStore sessionStore) A SessionDataStore that is the authoritative source of session information.toString()
Methods inherited from class org.eclipse.jetty.util.component.ContainerLifeCycle
addBean, addBean, addEventListener, addManaged, contains, destroy, dump, dump, dump, dumpObjects, dumpStdErr, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, installBean, installBean, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, start, stop, unmanage, updateBean, updateBean, updateBeans, updateBeans
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
getEventListeners, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, setEventListeners, start, stop
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.eclipse.jetty.util.component.Container
getCachedBeans, getEventListeners
Methods inherited from interface org.eclipse.jetty.util.component.Dumpable.DumpableContainer
isDumpable
Methods inherited from interface org.eclipse.jetty.util.component.LifeCycle
addEventListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeEventListener, start, stop
Methods inherited from interface org.eclipse.jetty.session.SessionCache
shutdown
-
Field Details
-
_sessionDataStore
The authoritative source of session data -
_manager
The SessionManager related to this SessionCache -
_context
Information about the context to which this SessionCache pertains -
_evictionPolicy
protected int _evictionPolicyWhen, if ever, to evict sessions: never; only when the last request for them finishes; after inactivity time (expressed as secs) -
_saveOnCreate
protected boolean _saveOnCreateIf true, as soon as a new session is created, it will be persisted to the SessionDataStore -
_saveOnInactiveEviction
protected boolean _saveOnInactiveEvictionIf true, a session that will be evicted from the cache because it has been inactive too long will be saved before being evicted. -
_removeUnloadableSessions
protected boolean _removeUnloadableSessionsIf true, a Session whose data cannot be read will be deleted from the SessionDataStore. -
_flushOnResponseCommit
protected boolean _flushOnResponseCommitIf true, when a response is about to be committed back to the client, a dirty session will be flushed to the session store. -
_invalidateOnShutdown
protected boolean _invalidateOnShutdownIf true, when the server shuts down, all sessions in the cache will be invalidated before being removed.
-
-
Constructor Details
-
AbstractSessionCache
- Parameters:
handler
- theSessionManager
to use
-
-
Method Details
-
newSession
Create a new Session object from pre-existing session data- Specified by:
newSession
in interfaceSessionCache
- Parameters:
data
- the session data- Returns:
- a new Session object
-
doGet
Get the session matching the key from the cache. Does not load the session.- Parameters:
id
- session id- Returns:
- the Session object matching the id
-
doPutIfAbsent
Put the session into the map if it wasn't already there- Parameters:
id
- the identity of the sessionsession
- the session object- Returns:
- null if the session wasn't already in the map, or the existing entry otherwise
-
doComputeIfAbsent
protected abstract ManagedSession doComputeIfAbsent(String id, Function<String, ManagedSession> mappingFunction) Compute the mappingFunction to create a Session object iff the session with the given id isn't already in the map, otherwise return the existing Session. This method is expected to have precisely the same behaviour asConcurrentHashMap.computeIfAbsent(K, java.util.function.Function<? super K, ? extends V>)
- Parameters:
id
- the session idmappingFunction
- the function to load the data for the session- Returns:
- an existing Session from the cache
-
doReplace
Replace the mapping from id to oldValue with newValue- Parameters:
id
- the idoldValue
- the old valuenewValue
- the new value- Returns:
- true if replacement was done
-
doDelete
Remove the session with this identity from the store- Parameters:
id
- the id- Returns:
- Session that was removed or null
-
getSessionManager
- Specified by:
getSessionManager
in interfaceSessionCache
- Returns:
- the SessionManger
-
initialize
- Specified by:
initialize
in interfaceSessionCache
- Parameters:
context
- theSessionContext
to use for this cache
-
doStart
Description copied from class:ContainerLifeCycle
Starts the managed lifecycle beans in the order they were added.- Overrides:
doStart
in classContainerLifeCycle
- Throws:
AbstractLifeCycle.StopException
- If thrown, the lifecycle will immediately be stopped.Exception
- If there was a problem starting. Will cause a transition to FAILED state
-
doStop
Description copied from class:ContainerLifeCycle
Stops the managed lifecycle beans in the reverse order they were added.- Overrides:
doStop
in classContainerLifeCycle
- Throws:
Exception
- If there was a problem stopping. Will cause a transition to FAILED state
-
getSessionDataStore
- Specified by:
getSessionDataStore
in interfaceSessionCache
- Returns:
- the SessionDataStore or null if there isn't one
-
setSessionDataStore
Description copied from interface:SessionCache
A SessionDataStore that is the authoritative source of session information.- Specified by:
setSessionDataStore
in interfaceSessionCache
- Parameters:
sessionStore
- theSessionDataStore
to use
-
getEvictionPolicy
- Specified by:
getEvictionPolicy
in interfaceSessionCache
- Returns:
- the eviction policy
-
setEvictionPolicy
public void setEvictionPolicy(int evictionTimeout) -1 means we never evict inactive sessions. 0 means we evict a session after the last request for it exits >0 is the number of seconds after which we evict inactive sessions from the cache- Specified by:
setEvictionPolicy
in interfaceSessionCache
- Parameters:
evictionTimeout
- -1 is never evict; 0 is evict-on-exit; and any other positive value is the time in seconds that a session can be idle before it can be evicted.
-
isSaveOnCreate
@ManagedAttribute(value="immediately save new sessions", readonly=true) public boolean isSaveOnCreate()- Specified by:
isSaveOnCreate
in interfaceSessionCache
- Returns:
- if
true
the newly created session will be saved immediately
-
setSaveOnCreate
public void setSaveOnCreate(boolean saveOnCreate) Description copied from interface:SessionCache
Whether or not a session that is newly created should be immediately saved. If false, a session that is created and invalidated within a single request is never persisted.- Specified by:
setSaveOnCreate
in interfaceSessionCache
- Parameters:
saveOnCreate
-true
to immediately save the newly created session
-
isRemoveUnloadableSessions
@ManagedAttribute(value="delete unreadable stored sessions", readonly=true) public boolean isRemoveUnloadableSessions()- Specified by:
isRemoveUnloadableSessions
in interfaceSessionCache
- Returns:
- true if sessions that can't be loaded are deleted from the store
-
setRemoveUnloadableSessions
public void setRemoveUnloadableSessions(boolean removeUnloadableSessions) If a session's data cannot be loaded from the store without error, remove it from the persistent store.- Specified by:
setRemoveUnloadableSessions
in interfaceSessionCache
- Parameters:
removeUnloadableSessions
- iftrue
unloadable sessions will be removed from session store
-
setFlushOnResponseCommit
public void setFlushOnResponseCommit(boolean flushOnResponseCommit) Description copied from interface:SessionCache
If true, a dirty session will be written to the SessionDataStore just before a response is returned to the client. This ensures that subsequent requests to either the same node or a different node see the changed session data.- Specified by:
setFlushOnResponseCommit
in interfaceSessionCache
-
isFlushOnResponseCommit
public boolean isFlushOnResponseCommit()- Specified by:
isFlushOnResponseCommit
in interfaceSessionCache
- Returns:
true
if dirty sessions should be written before the response is committed.
-
get
Get a session object. If the session object is not in this session store, try getting the data for it from a SessionDataStore associated with the session manager. The usage count of the session is incremented.- Specified by:
get
in interfaceSessionCache
- Parameters:
id
- the session id- Returns:
- the Session if one exists, null otherwise
- Throws:
Exception
- if any error occurred
-
getAndEnter
Get a session object. If the session object is not in this session store, try getting the data for it from a SessionDataStore associated with the session manager.- Parameters:
id
- The session to retrieveenter
- if true, the usage count of the session will be incremented- Returns:
- the session if it exists, null otherwise
- Throws:
Exception
- if the session cannot be loaded
-
add
Add an entirely new session (created by the application calling Request.getSession(true)) to the cache. The usage count of the fresh session is incremented.- Specified by:
add
in interfaceSessionCache
- Parameters:
id
- the idsession
- the session- Throws:
Exception
-
commit
A response that has accessed this session is about to be returned to the client. Pass the session to the store to persist, so that any changes will be visible to subsequent requests on the same node (if using NullSessionCache), or on other nodes.- Specified by:
commit
in interfaceSessionCache
- Throws:
Exception
-
release
Finish using the Session object. This should be called when a request exists the session. Only when the last simultaneous request exists the session will any action be taken. If there is a SessionDataStore write the session data through to it. If the SessionDataStore supports passivation, call the passivate/active listeners. If the evictionPolicy == SessionCache.EVICT_ON_SESSION_EXIT then after we have saved the session, we evict it from the cache.- Specified by:
release
in interfaceSessionCache
- Parameters:
session
- the current session object- Throws:
Exception
- if any error occurred
-
exists
Check to see if a session corresponding to the id exists. This method will first check with the object store. If it doesn't exist in the object store (might be passivated etc), it will check with the data store.- Specified by:
exists
in interfaceSessionCache
- Parameters:
id
- the session id- Returns:
- true if the session exists, false otherwise
- Throws:
Exception
- the Exception
-
contains
Check to see if this cache contains an entry for the session corresponding to the session id.- Specified by:
contains
in interfaceSessionCache
- Parameters:
id
- the session id- Returns:
- true if a Session object matching the id is present in the cache, false otherwise
- Throws:
Exception
- if any error occurred
-
delete
Remove a session object from this store and from any backing store.- Specified by:
delete
in interfaceSessionCache
- Parameters:
id
- the session id- Returns:
- the Session that was removed, null otherwise
- Throws:
Exception
- if any error occurred
-
checkExpiration
Description copied from interface:SessionCache
Check a list of session ids that belong to potentially expired sessions. The Session in the cache should be checked, but also the SessionDataStore, as that is the authoritative source of all session information.- Specified by:
checkExpiration
in interfaceSessionCache
- Parameters:
candidates
- the session ids to check- Returns:
- the set of session ids that have actually expired: this can be a superset of the original candidate list.
-
checkInactiveSession
Check a session for being inactive and thus being able to be evicted, if eviction is enabled.- Specified by:
checkInactiveSession
in interfaceSessionCache
- Parameters:
session
- session to check
-
renewSessionId
public ManagedSession renewSessionId(String oldId, String newId, String oldExtendedId, String newExtendedId) throws Exception Description copied from interface:SessionCache
Change the id of a Session.- Specified by:
renewSessionId
in interfaceSessionCache
- Parameters:
oldId
- the current session idnewId
- the new session idoldExtendedId
- the current extended session idnewExtendedId
- the new extended session id- Returns:
- the Session after changing its id
- Throws:
Exception
- if any error occurred
-
renewSessionId
protected void renewSessionId(ManagedSession session, String newId, String newExtendedId) throws Exception Swap the id on a session.- Parameters:
session
- the session for which to do the swapnewId
- the new idnewExtendedId
- the full id plus node id- Throws:
Exception
- if there was a failure saving the change
-
setSaveOnInactiveEviction
public void setSaveOnInactiveEviction(boolean saveOnEvict) Description copied from interface:SessionCache
Whether or not a a session that is about to be evicted should be saved before being evicted.- Specified by:
setSaveOnInactiveEviction
in interfaceSessionCache
- Parameters:
saveOnEvict
-true
if the session should be saved before being evicted
-
setInvalidateOnShutdown
public void setInvalidateOnShutdown(boolean invalidateOnShutdown) Description copied from interface:SessionCache
If true, all existing sessions in the cache will be invalidated when the server shuts down. Default is false.- Specified by:
setInvalidateOnShutdown
in interfaceSessionCache
- Parameters:
invalidateOnShutdown
- true invalidate all sessions in cache on server shutdown
-
isInvalidateOnShutdown
public boolean isInvalidateOnShutdown()- Specified by:
isInvalidateOnShutdown
in interfaceSessionCache
-
isSaveOnInactiveEviction
@ManagedAttribute(value="save sessions before evicting from cache", readonly=true) public boolean isSaveOnInactiveEviction()Whether we should save a session that has been inactive before we boot it from the cache.- Specified by:
isSaveOnInactiveEviction
in interfaceSessionCache
- Returns:
- true if an inactive session will be saved before being evicted
-
newSession
Description copied from interface:SessionCache
Create an entirely new Session.- Specified by:
newSession
in interfaceSessionCache
- Parameters:
id
- the unique id associated to the sessiontime
- the timestamp of the session creationmaxInactiveMs
- the max inactive time in milliseconds- Returns:
- a new Session
-
toString
- Overrides:
toString
in classAbstractLifeCycle
-