ome.services.sessions.state
Class SessionCache

java.lang.Object
  extended by ome.services.sessions.state.SessionCache
All Implemented Interfaces:
org.springframework.context.ApplicationContextAware

public class SessionCache
extends Object
implements org.springframework.context.ApplicationContextAware

Synchronized and lockable state for the SessionManager. Maps from Session to SessionContext in memory, with each mapping also having an additional cache which may spill over to disk.

Since:
3.0-Beta3
Author:
Josh Moore, josh at glencoesoftware.com

Nested Class Summary
static interface SessionCache.StaleCacheListener
          Observer pattern used to clear the blocked SessionCache#needsUpdate state, which prevents all further calls from happening.
 
Constructor Summary
SessionCache()
           
 
Method Summary
 boolean addSessionCallback(String session, SessionCallback cb)
           
protected  boolean checkNeedsUpdateWithoutLock()
          Whether or not doUpdate() should run.
protected  net.sf.ehcache.Ehcache createCache(String key, boolean inMemory, int maxInMemory)
           
 void doUpdate()
          Will only ever be accessed by a single thread.
 List<String> getIds()
          Since all methods which use getIds() will subsequently check for the existing session, we do not block here.
 long getLastUpdated()
           
 SessionContext getSessionContext(String uuid, boolean blocking)
          Retrieve a session possibly raising either RemovedSessionException or SessionTimeoutException.
 net.sf.ehcache.Ehcache inMemoryCache(String uuid)
           
 net.sf.ehcache.Ehcache onDiskCache(String uuid)
           
 void putSession(String uuid, SessionContext sessionContext)
          Puts a session blindly into the context.
 void removeSession(String uuid)
           
 boolean removeSessionCallback(String session, SessionCallback cb)
           
 void setAllowedBlockTime(long allowedBlockTime)
          Inject time in milliseconds to allow blocking
 void setApplicationContext(org.springframework.context.ApplicationContext ctx)
          Context injector.
 void setCacheManager(net.sf.ehcache.CacheManager manager)
          Injection method, also performs the creation of sessions
 void setStaleCacheListener(SessionCache.StaleCacheListener staleCacheListener)
           
 void setUpdateInterval(long milliseconds)
          Inject time in milliseconds between updates.
 void updateEvent(UserGroupUpdateEvent ugue)
          Marks a new update request in lastUpdateRequest.
protected  void waitForUpdate()
          If lastUpdateRun is older than lastUpdateRequest, then wait until the next background thread updates lastUpdateRequest.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SessionCache

public SessionCache()
Method Detail

setCacheManager

public void setCacheManager(net.sf.ehcache.CacheManager manager)
Injection method, also performs the creation of sessions


setApplicationContext

public void setApplicationContext(org.springframework.context.ApplicationContext ctx)
                           throws org.springframework.beans.BeansException
Context injector.

Specified by:
setApplicationContext in interface org.springframework.context.ApplicationContextAware
Throws:
org.springframework.beans.BeansException

setUpdateInterval

public void setUpdateInterval(long milliseconds)
Inject time in milliseconds between updates.


setAllowedBlockTime

public void setAllowedBlockTime(long allowedBlockTime)
Inject time in milliseconds to allow blocking


setStaleCacheListener

public void setStaleCacheListener(SessionCache.StaleCacheListener staleCacheListener)

addSessionCallback

public boolean addSessionCallback(String session,
                                  SessionCallback cb)

removeSessionCallback

public boolean removeSessionCallback(String session,
                                     SessionCallback cb)

putSession

public void putSession(String uuid,
                       SessionContext sessionContext)
Puts a session blindly into the context. This does nothing to a context which was previously present (e.g. call internalRemove, etc.) and therefore usage should be proceeded by a check.


getSessionContext

public SessionContext getSessionContext(String uuid,
                                        boolean blocking)
Retrieve a session possibly raising either RemovedSessionException or SessionTimeoutException.


removeSession

public void removeSession(String uuid)

getIds

public List<String> getIds()
Since all methods which use getIds() will subsequently check for the existing session, we do not block here. Blocking is primarily useful for post-admintype changes which can add or remove a user from a group. The existence of a session (which is what getIds specifies) is not significantly effected.


inMemoryCache

public net.sf.ehcache.Ehcache inMemoryCache(String uuid)

onDiskCache

public net.sf.ehcache.Ehcache onDiskCache(String uuid)

createCache

protected net.sf.ehcache.Ehcache createCache(String key,
                                             boolean inMemory,
                                             int maxInMemory)

getLastUpdated

public long getLastUpdated()

updateEvent

public void updateEvent(UserGroupUpdateEvent ugue)
Marks a new update request in lastUpdateRequest. If the timestamp on the event is invalid, then System.currentTimeMillis() will be used. This method updates lastUpdateRequest in the ReadWriteLock.readLock() of runUpdate, since only blocking changed during the background thread are of importance (i.e. we don't want to miss a change). However, because getting/setting a long value is not always an atomic operation, we use a AtomicLong.


waitForUpdate

protected void waitForUpdate()
If lastUpdateRun is older than lastUpdateRequest, then wait until the next background thread updates lastUpdateRequest. Note: this method does not use forceUpdateInterval since that is primarily to guarantee that old sessions are removed. If synchronization takes too long, an DatabaseBusyException is thrown.


checkNeedsUpdateWithoutLock

protected boolean checkNeedsUpdateWithoutLock()
Whether or not doUpdate() should run. This does not perform any synchronization so that methods can choose to use a read or write lock.


doUpdate

public void doUpdate()
Will only ever be accessed by a single thread. Rechecks the target update time again in case a second write thread was blocking the current one. lastUpdateRun gets set to a negative value to specify that this method is currently running.



OmeroJava Api

Version: Beta-4.2.0-r7571-b29

Copyright © 2009 The University of Dundee. All Rights Reserved.