Top Description Inners Fields Constructors Methods
org.mortbay.jetty.servlet

public Class JDBCSessionManager

extends AbstractSessionManager
Class Inheritance
Imports
java.io.ByteArrayInputStream, .ByteArrayOutputStream, .IOException, .InputStream, .ObjectInputStream, .ObjectOutputStream, java.sql.Connection, .PreparedStatement, .ResultSet, .SQLException, java.util.Collections, .List, .ListIterator, .Map, java.util.concurrent.ConcurrentHashMap, javax.servlet.http.HttpServletRequest, .HttpSessionEvent, .HttpSessionListener, org.mortbay.jetty.SessionIdManager, org.mortbay.jetty.handler.ContextHandler, org.mortbay.log.Log, org.mortbay.util.LazyList

JDBCSessionManager SessionManager that persists sessions to a database to enable clustering. Session data is persisted to the JettySessions table: rowId (unique in cluster: webapp name/path + virtualhost + sessionId) contextPath (of the context owning the session) sessionId (unique in a context) lastNode (name of node last handled session) accessTime (time in ms session was accessed) lastAccessTime (previous time in ms session was accessed) createTime (time in ms session created) cookieTime (time in ms session cookie created) lastSavedTime (last time in ms session access times were saved) expiryTime (time in ms that the session is due to expire) map (attribute map) As an optimisation, to prevent thrashing the database, we do not persist the accessTime and lastAccessTime every time the session is accessed. Rather, we write it out every so often. The frequency is controlled by the saveIntervalSec field.

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
public class
JDBCSessionManager.Session

Session Session instance in memory of this node.

public class
JDBCSessionManager.SessionData

SessionData Persistable data about a session.

Field Summary

Modifier and TypeField and Description
protected String
protected String
protected String
protected String
protected String
protected String
protected long
private ConcurrentHashMap<K, V>
Inherited from org.mortbay.jetty.servlet.AbstractSessionManager:
__distantFuture_context_dftMaxIdleSecs_httpOnly_loader_maxCookieAge_maxSessions_minSessions_nodeIdInSessionId_refreshCookieAge_secureCookies_sessionAttributeListeners_sessionCookie_sessionDomain_sessionHandler_sessionIdManager_sessionIdPathParameterName_sessionIdPathParameterNamePrefix_sessionListeners_sessionPath

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected void
addSession(AbstractSessionManager.Session session)

Implements abstract org.mortbay.jetty.servlet.AbstractSessionManager.addSession.

Add a newly created session to our in-memory list for this node and persist it.
private String
calculateRowId(JDBCSessionManager.SessionData data)

Calculate a unique id for this session across the cluster.

private String
canonicalize(String path)

Make an acceptable file name from a context path.

protected void
deleteSession(JDBCSessionManager.SessionData data)

Delete a session from the database.

public void
public void
protected void
expire(List<E> sessionIds)

Expire any Sessions we have in memory matching the list of expired Session ids.

private Connection
getConnection()

Get a connection from the driver.

public long
public JDBCSessionManager.Session
getSession(String
The session ID in the cluster, stripped of any worker name.
idInCluster
)

Implements abstract org.mortbay.jetty.servlet.AbstractSessionManager.getSession.

A session has been requested by it's id on this node.
public Map<K, V>
getSessionMap()

Implements abstract org.mortbay.jetty.servlet.AbstractSessionManager.getSessionMap.

Get all the sessions as a map of id to Session.
public int
getSessions()

Implements abstract org.mortbay.jetty.servlet.AbstractSessionManager.getSessions.

Get the number of sessions.
private String

Returns:

0.0.0.0 if no virtual host is defined
getVirtualHost
(ContextHandler.SContext context)

Get the first virtual host for the context.

protected void
invalidateSession(String idInCluster)

Invalidate a session.

protected void
protected JDBCSessionManager.SessionData
loadSession(String id, String canonicalContextPath, String vhost)

Load a session from the database

protected AbstractSessionManager.Session
protected void
protected void
removeSession(String idInCluster)

Implements abstract org.mortbay.jetty.servlet.AbstractSessionManager.removeSession.

Delete an existing session, both from the in-memory map and the database.
public void
public void
setSaveInterval(long sec)

Set the time in seconds which is the interval between saving the session access time to the database.

protected void
storeSession(JDBCSessionManager.SessionData data)

Insert a session into the database.

protected void
updateSession(JDBCSessionManager.SessionData data)

Update data on an existing persisted session.

private void
updateSessionAccessTime(JDBCSessionManager.SessionData data)

Persist the time the session was last accessed.

protected void
updateSessionNode(JDBCSessionManager.SessionData data)

Update the node on which the session was last seen to be my node.

Inherited from org.mortbay.jetty.servlet.AbstractSessionManager:
accessaddEventListeneraddSessionclearEventListenerscompletegetClusterIdgetHttpOnlygetHttpSessiongetIdManagergetMaxCookieAgegetMaxInactiveIntervalgetMaxSessionsgetMetaManagergetMinSessionsgetNodeIdgetRefreshCookieAgegetSecureCookiesgetSessionCookiegetSessionCookiegetSessionDomaingetSessionHandlergetSessionIdPathParameterNamegetSessionIdPathParameterNamePrefixgetSessionPathisNodeIdInSessionIdisUsingCookiesisValidnewHttpSessionremoveEventListenerremoveSessionresetStatssetHttpOnlysetIdManagersetMaxCookieAgesetMaxInactiveIntervalsetMetaManagersetNodeIdInSessionIdsetRefreshCookieAgesetSecureCookiessetSessionCookiesetSessionDomainsetSessionHandlersetSessionIdPathParameterNamesetSessionPathsetUsingCookies

Field Detail

__deleteSessionback to summary
protected String __deleteSession
__insertSessionback to summary
protected String __insertSession
__selectSessionback to summary
protected String __selectSession
__updateSessionback to summary
protected String __updateSession
__updateSessionAccessTimeback to summary
protected String __updateSessionAccessTime
__updateSessionNodeback to summary
protected String __updateSessionNode
_saveIntervalSecback to summary
protected long _saveIntervalSec
_sessionsback to summary
private ConcurrentHashMap<K, V> _sessions

Constructor Detail

JDBCSessionManagerback to summary
public JDBCSessionManager()

Method Detail

addSessionback to summary
protected void addSession(AbstractSessionManager.Session session)

Implements abstract org.mortbay.jetty.servlet.AbstractSessionManager.addSession.

Add a newly created session to our in-memory list for this node and persist it.

See Also
org.mortbay.jetty.servlet.AbstractSessionManager#addSession(org.mortbay.jetty.servlet.AbstractSessionManager.Session)
calculateRowIdback to summary
private String calculateRowId(JDBCSessionManager.SessionData data)

Calculate a unique id for this session across the cluster. Unique id is composed of: contextpath_virtualhost0_sessionid

canonicalizeback to summary
private String canonicalize(String path)

Make an acceptable file name from a context path.

deleteSessionback to summary
protected void deleteSession(JDBCSessionManager.SessionData data) throws Exception

Delete a session from the database. Should only be called when the session has been invalidated.

doStartback to summary
public void doStart() throws Exception

Overrides org.mortbay.jetty.servlet.AbstractSessionManager.doStart.

Start the session manager.

See Also
org.mortbay.jetty.servlet.AbstractSessionManager#doStart()
doStopback to summary
public void doStop() throws Exception

Overrides org.mortbay.jetty.servlet.AbstractSessionManager.doStop.

Stop the session manager.

See Also
org.mortbay.jetty.servlet.AbstractSessionManager#doStop()
expireback to summary
protected void expire(List<E> sessionIds)

Expire any Sessions we have in memory matching the list of expired Session ids.

getConnectionback to summary
private Connection getConnection() throws SQLException

Get a connection from the driver.

getSaveIntervalback to summary
public long getSaveInterval()
getSessionback to summary
public JDBCSessionManager.Session getSession(String idInCluster)

Implements abstract org.mortbay.jetty.servlet.AbstractSessionManager.getSession.

A session has been requested by it's id on this node. Load the session by id AND context path from the database. Multiple contexts may share the same session id (due to dispatching) but they CANNOT share the same contents. Check if last node id is my node id, if so, then the session we have in memory cannot be stale. If another node used the session last, then we need to refresh from the db.

Note

this method will go to the database, so if you only want to check for the existence of a Session in memory, use _sessions.get(id) instead.

Parameters
idInCluster:String

Doc from org.mortbay.jetty.servlet.AbstractSessionManager.getSession.

The session ID in the cluster, stripped of any worker name.

Returns:JDBCSessionManager.Session

Doc from org.mortbay.jetty.servlet.AbstractSessionManager.getSession.

A Session or null if none exists.

See Also
org.mortbay.jetty.servlet.AbstractSessionManager#getSession(java.lang.String)
getSessionMapback to summary
public Map<K, V> getSessionMap()

Implements abstract org.mortbay.jetty.servlet.AbstractSessionManager.getSessionMap.

Get all the sessions as a map of id to Session.

See Also
org.mortbay.jetty.servlet.AbstractSessionManager#getSessionMap()
getSessionsback to summary
public int getSessions()

Implements abstract org.mortbay.jetty.servlet.AbstractSessionManager.getSessions.

Get the number of sessions.

See Also
org.mortbay.jetty.servlet.AbstractSessionManager#getSessions()
getVirtualHostback to summary
private String getVirtualHost(ContextHandler.SContext context)

Get the first virtual host for the context. Used to help identify the exact session/contextPath.

Returns:String

0.0.0.0 if no virtual host is defined

invalidateSessionback to summary
protected void invalidateSession(String idInCluster)

Invalidate a session.

invalidateSessionsback to summary
protected void invalidateSessions()

Implements abstract org.mortbay.jetty.servlet.AbstractSessionManager.invalidateSessions.

loadSessionback to summary
protected JDBCSessionManager.SessionData loadSession(String id, String canonicalContextPath, String vhost) throws Exception

Load a session from the database

newSessionback to summary
protected AbstractSessionManager.Session newSession(HttpServletRequest request)

Implements abstract org.mortbay.jetty.servlet.AbstractSessionManager.newSession.

Make a new Session.

See Also
org.mortbay.jetty.servlet.AbstractSessionManager#newSession(javax.servlet.http.HttpServletRequest)
prepareTablesback to summary
protected void prepareTables()
removeSessionback to summary
protected void removeSession(String idInCluster)

Implements abstract org.mortbay.jetty.servlet.AbstractSessionManager.removeSession.

Delete an existing session, both from the in-memory map and the database.

See Also
org.mortbay.jetty.servlet.AbstractSessionManager#removeSession(java.lang.String)
removeSessionback to summary
public void removeSession(AbstractSessionManager.Session session, boolean invalidate)

Overrides org.mortbay.jetty.servlet.AbstractSessionManager.removeSession.

Remove session from manager

Parameters
session:AbstractSessionManager.Session

The session to remove

invalidate:boolean

True if HttpSessionListener#sessionDestroyed(HttpSessionEvent) and SessionIdManager#invalidateAll(String) should be called.

setSaveIntervalback to summary
public void setSaveInterval(long sec)

Set the time in seconds which is the interval between saving the session access time to the database. This is an optimization that prevents the database from being overloaded when a session is accessed very frequently. On session exit, if the session attributes have NOT changed, the time at which we last saved the accessed time is compared to the current accessed time. If the interval is at least saveIntervalSecs, then the access time will be persisted to the database. If any session attribute does change, then the attributes and the accessed time are persisted.

storeSessionback to summary
protected void storeSession(JDBCSessionManager.SessionData data) throws Exception

Insert a session into the database.

updateSessionback to summary
protected void updateSession(JDBCSessionManager.SessionData data) throws Exception

Update data on an existing persisted session.

updateSessionAccessTimeback to summary
private void updateSessionAccessTime(JDBCSessionManager.SessionData data) throws Exception

Persist the time the session was last accessed.

updateSessionNodeback to summary
protected void updateSessionNode(JDBCSessionManager.SessionData data) throws Exception

Update the node on which the session was last seen to be my node.

org.mortbay.jetty.servlet back to summary

protected Class JDBCSessionManager.ClassLoadingObjectInputStream

extends ObjectInputStream
Class Inheritance

ClassLoadingObjectInputStream

Constructor Summary

AccessConstructor and Description
public
public

Method Summary

Modifier and TypeMethod and Description
public Class<T>
resolveClass(ObjectStreamClass
an instance of class ObjectStreamClass
cl
)

Overrides java.io.ObjectInputStream.resolveClass.

Load the local class equivalent of the specified stream class description.
Inherited from java.io.ObjectInputStream:
availableclosedefaultReadObjectenableResolveObjectgetObjectInputFilterreadreadreadBooleanreadBytereadCharreadClassDescriptorreadDoublereadFieldsreadFloatreadFullyreadFullyreadIntreadLinereadLongreadObjectreadObjectOverridereadShortreadStreamHeaderreadUnsharedreadUnsignedBytereadUnsignedShortreadUTFregisterValidationresolveObjectresolveProxyClasssetObjectInputFilterskipBytes

Constructor Detail

ClassLoadingObjectInputStreamback to summary
public ClassLoadingObjectInputStream(InputStream in) throws IOException
ClassLoadingObjectInputStreamback to summary
public ClassLoadingObjectInputStream() throws IOException

Method Detail

resolveClassback to summary
public Class<T> resolveClass(ObjectStreamClass cl) throws IOException, ClassNotFoundException

Overrides java.io.ObjectInputStream.resolveClass.

Doc from java.io.ObjectInputStream.resolveClass.

Load the local class equivalent of the specified stream class description. Subclasses may implement this method to allow classes to be fetched from an alternate source.

The corresponding method in ObjectOutputStream is annotateClass. This method will be invoked only once for each unique class in the stream. This method can be implemented by subclasses to use an alternate loading mechanism but must return a Class object. Once returned, if the class is not an array class, its serialVersionUID is compared to the serialVersionUID of the serialized class, and if there is a mismatch, the deserialization fails and an InvalidClassException is thrown.

The default implementation of this method in ObjectInputStream returns the result of calling

Class.forName(desc.getName(), false, loader)
Class.forName(desc.getName(), false, loader)
where loader is the first class loader on the current thread's stack (starting from the currently executing method) that is neither the platform class loader nor its ancestor; otherwise, loader is the platform class loader. If this call results in a ClassNotFoundException and the name of the passed ObjectStreamClass instance is the Java language keyword for a primitive type or void, then the Class object representing that primitive type or void will be returned (e.g., an ObjectStreamClass with the name "int" will be resolved to Integer.TYPE). Otherwise, the ClassNotFoundException will be thrown to the caller of this method.
Parameters
cl:ObjectStreamClass

an instance of class ObjectStreamClass

Returns:Class<T>

a Class object corresponding to desc

Exceptions
IOException:
any of the usual Input/Output exceptions.
ClassNotFoundException:
if class of a serialized object cannot be found.
org.mortbay.jetty.servlet back to summary

public Class JDBCSessionManager.Session

extends Session
Class Inheritance

Session Session instance in memory of this node.

Field Summary

Modifier and TypeField and Description
private JDBCSessionManager.SessionData
private boolean
Inherited from org.mortbay.jetty.servlet.AbstractSessionManager.Session:
_accessed_clusterId_cookieSet_created_doInvalidate_idChanged_invalid_lastAccessed_maxIdleMs_newSession_nodeId_requests_values

Constructor Summary

AccessConstructor and Description
protected
Session(HttpServletRequest request)

Session from a request.

protected
Session(JDBCSessionManager.SessionData data)

Session restored in database.

Method Summary

Modifier and TypeMethod and Description
protected void
protected void
protected void
protected Map<K, V>
public void
removeAttribute(String
the name of the object to remove from this session
name
)

Overrides org.mortbay.jetty.servlet.AbstractSessionManager.Session.removeAttribute.

Implements javax.servlet.http.HttpSession.removeAttribute.

Removes the object bound with the specified name from this session.
public void
setAttribute(String
the name to which the object is bound; cannot be null
name
,
Object
the object to be bound
value
)

Overrides org.mortbay.jetty.servlet.AbstractSessionManager.Session.setAttribute.

Implements javax.servlet.http.HttpSession.setAttribute.

Binds an object to this session, using the name specified.
protected void
Inherited from org.mortbay.jetty.servlet.AbstractSessionManager.Session:
bindValuedidActivatedoInvalidategetAttributegetAttributeNamesgetClusterIdgetCookieSetTimegetCreationTimegetIdgetLastAccessedTimegetMaxInactiveIntervalgetNodeIdgetServletContextgetSessiongetSessionContextgetValuegetValueNamesinitValuesinvalidateisIdChangedisNewisValidputValueremoveValuesetIdChangedsetMaxInactiveIntervaltoStringunbindValuewillPassivate

Field Detail

_databack to summary
private JDBCSessionManager.SessionData _data
_dirtyback to summary
private boolean _dirty

Constructor Detail

Sessionback to summary
protected Session(HttpServletRequest request)

Session from a request.

Sessionback to summary
protected Session(JDBCSessionManager.SessionData data)

Session restored in database.

Method Detail

accessback to summary
protected void access(long time)

Overrides org.mortbay.jetty.servlet.AbstractSessionManager.Session.access.

Entry to session. Called by SessionHandler on inbound request and the session already exists in this node's memory.

See Also
org.mortbay.jetty.servlet.AbstractSessionManager.Session#access(long)
completeback to summary
protected void complete()

Overrides org.mortbay.jetty.servlet.AbstractSessionManager.Session.complete.

Exit from session

See Also
org.mortbay.jetty.servlet.AbstractSessionManager.Session#complete()
cookieSetback to summary
protected void cookieSet()

Overrides org.mortbay.jetty.servlet.AbstractSessionManager.Session.cookieSet.

newAttributeMapback to summary
protected Map<K, V> newAttributeMap()

Implements abstract org.mortbay.jetty.servlet.AbstractSessionManager.Session.newAttributeMap.

removeAttributeback to summary
public void removeAttribute(String name)

Overrides org.mortbay.jetty.servlet.AbstractSessionManager.Session.removeAttribute.

Implements javax.servlet.http.HttpSession.removeAttribute.

Doc from javax.servlet.http.HttpSession.removeAttribute.

Removes the object bound with the specified name from this session. If the session does not have an object bound with the specified name, this method does nothing.

After this method executes, and if the object implements HttpSessionBindingListener, the container calls HttpSessionBindingListener.valueUnbound. The container then notifies any HttpSessionAttributeListeners in the web application.

Parameters
name:String

the name of the object to remove from this session

setAttributeback to summary
public void setAttribute(String name, Object value)

Overrides org.mortbay.jetty.servlet.AbstractSessionManager.Session.setAttribute.

Implements javax.servlet.http.HttpSession.setAttribute.

Doc from javax.servlet.http.HttpSession.setAttribute.

Binds an object to this session, using the name specified. If an object of the same name is already bound to the session, the object is replaced.

After this method executes, and if the new object implements HttpSessionBindingListener, the container calls HttpSessionBindingListener.valueBound. The container then notifies any HttpSessionAttributeListeners in the web application.

If an object was already bound to this session of this name that implements HttpSessionBindingListener, its HttpSessionBindingListener.valueUnbound method is called.

If the value passed in is null, this has the same effect as calling removeAttribute().

Parameters
name:String

the name to which the object is bound; cannot be null

value:Object

the object to be bound

timeoutback to summary
protected void timeout() throws IllegalStateException

Overrides org.mortbay.jetty.servlet.AbstractSessionManager.Session.timeout.

org.mortbay.jetty.servlet back to summary

public Class JDBCSessionManager.SessionData

extends Object
Class Inheritance

SessionData Persistable data about a session.

Field Summary

Modifier and TypeField and Description
private long
private Map<K, V>
private String
private long
private long
private long
private String
private long
private String
private long
private long
private String
private String

Constructor Summary

AccessConstructor and Description
public
SessionData(String sessionId)

Method Summary

Modifier and TypeMethod and Description
public synchronized long
protected synchronized Map<K, V>
public synchronized String
public synchronized long
public synchronized long
public synchronized long
public synchronized String
public synchronized long
public synchronized String
public synchronized long
public synchronized long
protected synchronized String
public synchronized String
protected synchronized void
setAccessed(long ms)

protected synchronized void
public synchronized void
public void
setCookieSet(long ms)

protected synchronized void
setCreated(long ms)

public synchronized void
setExpiryTime(long time)

public synchronized void
setLastAccessed(long ms)

public synchronized void
public synchronized void
setLastSaved(long time)

public synchronized void
setMaxIdleMs(long ms)

public synchronized void
public synchronized void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

_accessedback to summary
private long _accessed
_attributesback to summary
private Map<K, V> _attributes
_canonicalContextback to summary
private String _canonicalContext
_cookieSetback to summary
private long _cookieSet
_createdback to summary
private long _created
_expiryTimeback to summary
private long _expiryTime
_idback to summary
private String _id
_lastAccessedback to summary
private long _lastAccessed
_lastNodeback to summary
private String _lastNode
_lastSavedback to summary
private long _lastSaved
_maxIdleMsback to summary
private long _maxIdleMs
_rowIdback to summary
private String _rowId
_virtualHostback to summary
private String _virtualHost

Constructor Detail

SessionDataback to summary
public SessionData(String sessionId)

Method Detail

getAccessedback to summary
public synchronized long getAccessed()
getAttributeMapback to summary
protected synchronized Map<K, V> getAttributeMap()
getCanonicalContextback to summary
public synchronized String getCanonicalContext()
getCookieSetback to summary
public synchronized long getCookieSet()
getCreatedback to summary
public synchronized long getCreated()
getExpiryTimeback to summary
public synchronized long getExpiryTime()
getIdback to summary
public synchronized String getId()
getLastAccessedback to summary
public synchronized long getLastAccessed()
getLastNodeback to summary
public synchronized String getLastNode()
getLastSavedback to summary
public synchronized long getLastSaved()
getMaxIdleMsback to summary
public synchronized long getMaxIdleMs()
getRowIdback to summary
protected synchronized String getRowId()
getVirtualHostback to summary
public synchronized String getVirtualHost()
setAccessedback to summary
protected synchronized void setAccessed(long ms)
setAttributeMapback to summary
protected synchronized void setAttributeMap(ConcurrentHashMap<K, V> map)
setCanonicalContextback to summary
public synchronized void setCanonicalContext(String str)
setCookieSetback to summary
public void setCookieSet(long ms)
setCreatedback to summary
protected synchronized void setCreated(long ms)
setExpiryTimeback to summary
public synchronized void setExpiryTime(long time)
setLastAccessedback to summary
public synchronized void setLastAccessed(long ms)
setLastNodeback to summary
public synchronized void setLastNode(String node)
setLastSavedback to summary
public synchronized void setLastSaved(long time)
setMaxIdleMsback to summary
public synchronized void setMaxIdleMs(long ms)
setRowIdback to summary
public synchronized void setRowId(String rowId)
setVirtualHostback to summary
public synchronized void setVirtualHost(String vhost)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.