Package org.eclipse.jetty.server.session
Class AbstractSessionCache
- java.lang.Object
-
- org.eclipse.jetty.util.component.AbstractLifeCycle
-
- org.eclipse.jetty.util.component.ContainerLifeCycle
-
- org.eclipse.jetty.server.session.AbstractSessionCache
-
- All Implemented Interfaces:
SessionCache,org.eclipse.jetty.util.component.Container,org.eclipse.jetty.util.component.Destroyable,org.eclipse.jetty.util.component.Dumpable,org.eclipse.jetty.util.component.LifeCycle
- Direct Known Subclasses:
DefaultSessionCache,NullSessionCache
@ManagedObject public abstract class AbstractSessionCache extends org.eclipse.jetty.util.component.ContainerLifeCycle implements SessionCache
AbstractSessionCache A base implementation of theSessionCacheinterface 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 Modifier and Type Class Description protected classAbstractSessionCache.PlaceHolderSessionPlaceHolder-
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.component.AbstractLifeCycle.AbstractLifeCycleListener
-
-
Field Summary
Fields Modifier and Type Field Description protected SessionContext_contextInformation about the context to which this SessionCache pertainsprotected int_evictionPolicyWhen, if ever, to evict sessions: never; only when the last request for them finishes; after inactivity time (expressed as secs)protected SessionHandler_handlerThe SessionHandler related to this SessionCacheprotected boolean_removeUnloadableSessionsIf true, a Session whose data cannot be read will be deleted from the SessionDataStore.protected boolean_saveOnCreateIf true, as soon as a new session is created, it will be persisted to the SessionDataStoreprotected 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.protected SessionDataStore_sessionDataStoreThe authoritative source of session data-
Fields inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
FAILED, RUNNING, STARTED, STARTING, STOPPED, STOPPING
-
Fields inherited from interface org.eclipse.jetty.server.session.SessionCache
EVICT_ON_INACTIVITY, EVICT_ON_SESSION_EXIT, NEVER_EVICT
-
-
Constructor Summary
Constructors Constructor Description AbstractSessionCache(SessionHandler handler)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Set<String>checkExpiration(Set<String> candidates)Check a list of session ids that belong to potentially expired sessions.voidcheckInactiveSession(Session session)Check a session for being inactive and thus being able to be evicted, if eviction is enabled.booleancontains(String id)Check to see if this cache contains an entry for the session corresponding to the session id.Sessiondelete(String id)Remove a session object from this store and from any backing store.abstract SessiondoDelete(String id)Remove the session with this identity from the storeabstract SessiondoGet(String id)Get the session matching the keyabstract SessiondoPutIfAbsent(String id, Session session)Put the session into the map if it wasn't already thereabstract booleandoReplace(String id, Session oldValue, Session newValue)Replace the mapping from id to oldValue with newValueprotected voiddoStart()protected voiddoStop()booleanexists(String id)Check to see if a session corresponding to the id exists.Sessionget(String id)Get a session object.intgetEvictionPolicy()SessionDataStoregetSessionDataStore()SessionHandlergetSessionHandler()voidinitialize(SessionContext context)booleanisRemoveUnloadableSessions()booleanisSaveOnCreate()booleanisSaveOnInactiveEviction()Whether we should save a session that has been inactive before we boot it from the cache.SessionnewSession(HttpServletRequest request, String id, long time, long maxInactiveMs)Create an entirely new Session.abstract SessionnewSession(HttpServletRequest request, SessionData data)Create a new Session for a request.abstract SessionnewSession(SessionData data)Create a new Session object from pre-existing session datavoidput(String id, Session session)Put the Session object back into the session store.SessionrenewSessionId(String oldId, String newId)Change the id of a Session.voidsetEvictionPolicy(int evictionTimeout)-1 means we never evict inactive sessions.voidsetRemoveUnloadableSessions(boolean removeUnloadableSessions)If a session's data cannot be loaded from the store without error, remove it from the persistent store.voidsetSaveOnCreate(boolean saveOnCreate)Whether or not a session that is newly created should be immediately saved.voidsetSaveOnInactiveEviction(boolean saveOnEvict)Whether or not a a session that is about to be evicted should be saved before being evicted.voidsetSessionDataStore(SessionDataStore sessionStore)A SessionDataStore that is the authoritative source of session information.StringtoString()-
Methods inherited from class org.eclipse.jetty.util.component.ContainerLifeCycle
addBean, addBean, addBean, addEventListener, addManaged, contains, destroy, dump, dump, dump, dump, dump, dumpBeans, dumpObject, dumpObjects, dumpStdErr, dumpThis, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, setStopTimeout, start, stop, unmanage, updateBean, updateBean, updateBeans
-
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, 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.LifeCycle
addLifeCycleListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
-
Methods inherited from interface org.eclipse.jetty.server.session.SessionCache
shutdown
-
-
-
-
Field Detail
-
_sessionDataStore
protected SessionDataStore _sessionDataStore
The authoritative source of session data
-
_handler
protected final SessionHandler _handler
The SessionHandler related to this SessionCache
-
_context
protected SessionContext _context
Information about the context to which this SessionCache pertains
-
_evictionPolicy
protected int _evictionPolicy
When, if ever, to evict sessions: never; only when the last request for them finishes; after inactivity time (expressed as secs)
-
_saveOnCreate
protected boolean _saveOnCreate
If true, as soon as a new session is created, it will be persisted to the SessionDataStore
-
_saveOnInactiveEviction
protected boolean _saveOnInactiveEviction
If 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 _removeUnloadableSessions
If true, a Session whose data cannot be read will be deleted from the SessionDataStore.
-
-
Constructor Detail
-
AbstractSessionCache
public AbstractSessionCache(SessionHandler handler)
- Parameters:
handler- theSessionHandlerto use
-
-
Method Detail
-
newSession
public abstract Session newSession(SessionData data)
Create a new Session object from pre-existing session data- Specified by:
newSessionin interfaceSessionCache- Parameters:
data- the session data- Returns:
- a new Session object
-
newSession
public abstract Session newSession(HttpServletRequest request, SessionData data)
Create a new Session for a request.- Parameters:
request- the requestdata- the session data- Returns:
- the new session
-
doGet
public abstract Session doGet(String id)
Get the session matching the key- Parameters:
id- session id- Returns:
- the Session object matching the id
-
doPutIfAbsent
public abstract Session doPutIfAbsent(String id, Session session)
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
-
doReplace
public abstract boolean doReplace(String id, Session oldValue, Session newValue)
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
public abstract Session doDelete(String id)
Remove the session with this identity from the store- Parameters:
id- the id- Returns:
- true if removed false otherwise
-
getSessionHandler
public SessionHandler getSessionHandler()
- Specified by:
getSessionHandlerin interfaceSessionCache- Returns:
- the SessionManger
-
initialize
public void initialize(SessionContext context)
- Specified by:
initializein interfaceSessionCache- Parameters:
context- theSessionContextto use for this cache- See Also:
SessionCache.initialize(org.eclipse.jetty.server.session.SessionContext)
-
doStart
protected void doStart() throws Exception- Overrides:
doStartin classorg.eclipse.jetty.util.component.ContainerLifeCycle- Throws:
Exception- See Also:
AbstractLifeCycle.doStart()
-
doStop
protected void doStop() throws Exception- Overrides:
doStopin classorg.eclipse.jetty.util.component.ContainerLifeCycle- Throws:
Exception- See Also:
AbstractLifeCycle.doStop()
-
getSessionDataStore
public SessionDataStore getSessionDataStore()
- Specified by:
getSessionDataStorein interfaceSessionCache- Returns:
- the SessionDataStore or null if there isn't one
-
setSessionDataStore
public void setSessionDataStore(SessionDataStore sessionStore)
Description copied from interface:SessionCacheA SessionDataStore that is the authoritative source of session information.- Specified by:
setSessionDataStorein interfaceSessionCache- Parameters:
sessionStore- theSessionDataStoreto use- See Also:
SessionCache.setSessionDataStore(org.eclipse.jetty.server.session.SessionDataStore)
-
getEvictionPolicy
@ManagedAttribute(value="session eviction policy", readonly=true) public int getEvictionPolicy()- Specified by:
getEvictionPolicyin interfaceSessionCache- Returns:
- the eviction policy
- See Also:
SessionCache.getEvictionPolicy()
-
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:
setEvictionPolicyin 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.- See Also:
SessionCache.setEvictionPolicy(int)
-
isSaveOnCreate
@ManagedAttribute(value="immediately save new sessions", readonly=true) public boolean isSaveOnCreate()- Specified by:
isSaveOnCreatein interfaceSessionCache- Returns:
- if
truethe newly created session will be saved immediately
-
setSaveOnCreate
public void setSaveOnCreate(boolean saveOnCreate)
Description copied from interface:SessionCacheWhether 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:
setSaveOnCreatein interfaceSessionCache- Parameters:
saveOnCreate-trueto immediately save the newly created session
-
isRemoveUnloadableSessions
@ManagedAttribute(value="delete unreadable stored sessions", readonly=true) public boolean isRemoveUnloadableSessions()- Specified by:
isRemoveUnloadableSessionsin 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:
setRemoveUnloadableSessionsin interfaceSessionCache- Parameters:
removeUnloadableSessions- iftrueunloadable sessions will be removed from session store
-
get
public Session get(String id) throws Exception
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.- Specified by:
getin interfaceSessionCache- Parameters:
id- the session id- Returns:
- the Session if one exists, null otherwise
- Throws:
Exception- if any error occurred- See Also:
SessionCache.get(java.lang.String)
-
put
public void put(String id, Session session) throws Exception
Put the Session object back into the session store. 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:
putin interfaceSessionCache- Parameters:
id- the session idsession- the current session object- Throws:
Exception- if any error occurred- See Also:
SessionCache.put(java.lang.String, org.eclipse.jetty.server.session.Session)
-
exists
public boolean exists(String id) throws Exception
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:
existsin interfaceSessionCache- Parameters:
id- the session id- Returns:
- true if the session exists, false otherwise
- Throws:
Exception- the Exception- See Also:
SessionCache.exists(java.lang.String)
-
contains
public boolean contains(String id) throws Exception
Check to see if this cache contains an entry for the session corresponding to the session id.- Specified by:
containsin 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- See Also:
SessionCache.contains(java.lang.String)
-
delete
public Session delete(String id) throws Exception
Remove a session object from this store and from any backing store.- Specified by:
deletein interfaceSessionCache- Parameters:
id- the session id- Returns:
- the Session that was removed, null otherwise
- Throws:
Exception- if any error occurred- See Also:
SessionCache.delete(java.lang.String)
-
checkExpiration
public Set<String> checkExpiration(Set<String> candidates)
Description copied from interface:SessionCacheCheck 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:
checkExpirationin 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.
- See Also:
SessionCache.checkExpiration(Set)
-
checkInactiveSession
public void checkInactiveSession(Session session)
Check a session for being inactive and thus being able to be evicted, if eviction is enabled.- Specified by:
checkInactiveSessionin interfaceSessionCache- Parameters:
session- session to check
-
renewSessionId
public Session renewSessionId(String oldId, String newId) throws Exception
Description copied from interface:SessionCacheChange the id of a Session.- Specified by:
renewSessionIdin interfaceSessionCache- Parameters:
oldId- the current session idnewId- the new session id- Returns:
- the Session after changing its id
- Throws:
Exception- if any error occurred- See Also:
SessionCache.renewSessionId(java.lang.String, java.lang.String)
-
setSaveOnInactiveEviction
public void setSaveOnInactiveEviction(boolean saveOnEvict)
Description copied from interface:SessionCacheWhether or not a a session that is about to be evicted should be saved before being evicted.- Specified by:
setSaveOnInactiveEvictionin interfaceSessionCache- Parameters:
saveOnEvict-trueif the session should be saved before being evicted- See Also:
SessionCache.setSaveOnInactiveEviction(boolean)
-
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:
isSaveOnInactiveEvictionin interfaceSessionCache- Returns:
- true if an inactive session will be saved before being evicted
-
newSession
public Session newSession(HttpServletRequest request, String id, long time, long maxInactiveMs)
Description copied from interface:SessionCacheCreate an entirely new Session.- Specified by:
newSessionin interfaceSessionCache- Parameters:
request- the requestid- the unique id associated to the sessiontime- the timestamp of the session creationmaxInactiveMs- the max inactive time in milliseconds- Returns:
- a new Session
- See Also:
SessionCache.newSession(javax.servlet.http.HttpServletRequest, java.lang.String, long, long)
-
toString
public String toString()
- Overrides:
toStringin classorg.eclipse.jetty.util.component.AbstractLifeCycle
-
-