ome.services.sessions
Class SessionManagerImpl

java.lang.Object
  extended by ome.services.sessions.SessionManagerImpl
All Implemented Interfaces:
EventListener, SessionManager, SessionCache.StaleCacheListener, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationListener

public class SessionManagerImpl
extends Object
implements SessionManager, SessionCache.StaleCacheListener, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationListener

Is for ISession a cache and will be kept there in sync? OR Factors out the logic from ISession and SessionManagerI Therefore either called directly, or via synchronous messages. Uses the name of a Principal as the key to the session. We may need to limit user names to prevent this. (Strictly alphanumeric) Receives notifications as an ApplicationListener, which should be used to keep the Session instances up-to-date.

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

Field Summary
protected  Principal asroot
          A private session for use only by this instance for running methods via Executor.
protected  SessionCache cache
           
protected  OmeroContext context
           
protected  long defaultTimeToIdle
           
protected  long defaultTimeToLive
           
protected  Executor executor
           
protected  CounterFactory factory
           
protected  SessionContext internalSession
          Internal SessionContext created during init() which is used for all method calls internal to the session manager (see execute* methods)
protected  long maxUserTimeToIdle
           
protected  long maxUserTimeToLive
           
protected  PrincipalHolder principalHolder
           
protected  boolean readOnly
           
protected  Roles roles
           
 
Constructor Summary
SessionManagerImpl()
           
 
Method Summary
 void addCallback(String sessionId, SessionCallback cb)
           
 int close(String uuid)
          If reference count for the session is less than 1, close the session.
 int closeAll()
          Close all sessions with checking for the necessary reference counts.
 ome.model.meta.Session copy(ome.model.meta.Session source)
           
protected  SessionContext createSessionContext(List<?> list, SessionContext previous)
          Takes a snapshot as from #executeSessionContextLookup(ServiceFactory, Principal) and turns it into a SessionContext instance.
 ome.model.meta.Share createShare(Principal principal, boolean enabled, long timeToLive, String eventType, String description, long groupId)
           
 ome.model.meta.Session createWithAgent(Principal principal, String agent)
           
 ome.model.meta.Session createWithAgent(Principal _principal, String credentials, String agent)
           
protected  void define(ome.model.meta.Session s, String uuid, String message, long started, long idle, long live, String eventType, String agent)
           
 int detach(String uuid)
          Allows decrementing the reference count for a session without calling the actual SessionManager.close(String) logic.
protected  Map<String,Object> environment(String session, String env)
           
 boolean executePasswordCheck(String name, String credentials)
          Executes a password check using the Executor framework.
 ome.model.meta.Session find(String uuid)
           
 List<ome.model.meta.Session> findByUser(String user)
           
 List<ome.model.meta.Session> findByUserAndAgent(String user, String... agents)
          Returns a non-null, possibly empty list of session instances belonging to the given user and with one of the given agents.
 Object getCallbackObject(String sessionId, String name)
           
 EventContext getEventContext(Principal principal)
          Provides a partial EventContext for the current Session.
 Object getInput(String session, String key)
          Returns the input environment Object stored under the given key or null if none present.
 Object getOutput(String session, String key)
          Returns the output environment Object stored under the given key or null if none present.
 int getReferenceCount(String uuid)
          Return the number of client which are presumed to be attached to this session or throw an exception if there's no such session.
 SessionStats getSessionStats(String uuid)
          Return the SessionStats which are being counted for the given session or throw an exception if it has been removed.
 List<String> getUserRoles(String uuid)
           
 void init()
          Initialization method called by the Spring run-time to acquire an initial Session.
 net.sf.ehcache.Ehcache inMemoryCache(String uuid)
          Returns after possibly creating an in-memory cache which can be used throughout the session.
 Map<String,Object> inputEnvironment(String session)
          Returns a copy of the input environment.
 String[] notifications(String sessionId)
           
 void onApplicationEvent(org.springframework.context.ApplicationEvent event)
           
 net.sf.ehcache.Ehcache onDiskCache(String uuid)
          Returns after possibly creating an on-disk cache which can be used throughout the session.
 Map<String,Object> outputEnvironment(String session)
          Returns a copy of the output environment.
 void prepareReload()
           
 SessionContext reload(SessionContext ctx)
          Will be called in a synchronized block by SessionCache in order to allow for an update.
 EventContext reload(String uuid)
          Similar to SessionManager.getEventContext(Principal) but uses the internal reload logic to get a fresh representation of the context.
 void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
           
 void setCounterFactory(CounterFactory factory)
           
 void setDefaultTimeToIdle(long defaultTimeToIdle)
           
 void setDefaultTimeToLive(long defaultTimeToLive)
           
 void setExecutor(Executor executor)
           
 void setInput(String session, String key, Object object)
          Places the argument in the input environment under the given key, possibly initializing the Map Throws an exception if there is no Session with the given identifier.
 void setOutput(String session, String key, Object object)
          Places the argument in the output environment under the given key, possibly initializing the Map Throws an exception if there is no Session with the given identifier.
 void setPrincipalHolder(PrincipalHolder principal)
           
 void setReadOnly(boolean readOnly)
           
 void setRoles(Roles securityRoles)
           
 ome.model.IObject setSecurityContext(Principal principal, ome.model.IObject obj)
          Sets the context for the current session to the given value.
 void setSessionCache(SessionCache sessionCache)
           
 void setUuid(String uuid)
           
 ome.model.meta.Session update(ome.model.meta.Session session)
          See ISession#updateSession(Session) for the logic that's implemented here.
 ome.model.meta.Session update(ome.model.meta.Session session, boolean trusted)
          Same as SessionManager.update(Session) but some security checks can be overriden.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

context

protected OmeroContext context

roles

protected Roles roles

cache

protected SessionCache cache

executor

protected Executor executor

defaultTimeToIdle

protected long defaultTimeToIdle

maxUserTimeToIdle

protected long maxUserTimeToIdle

defaultTimeToLive

protected long defaultTimeToLive

maxUserTimeToLive

protected long maxUserTimeToLive

principalHolder

protected PrincipalHolder principalHolder

factory

protected CounterFactory factory

readOnly

protected boolean readOnly

asroot

protected Principal asroot
A private session for use only by this instance for running methods via Executor. The name of this Principal will not be removed by calls to closeAll().


internalSession

protected SessionContext internalSession
Internal SessionContext created during init() which is used for all method calls internal to the session manager (see execute* methods)

Constructor Detail

SessionManagerImpl

public SessionManagerImpl()
Method Detail

setApplicationContext

public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
                           throws org.springframework.beans.BeansException
Specified by:
setApplicationContext in interface org.springframework.context.ApplicationContextAware
Throws:
org.springframework.beans.BeansException

setUuid

public void setUuid(String uuid)

setSessionCache

public void setSessionCache(SessionCache sessionCache)

setRoles

public void setRoles(Roles securityRoles)

setExecutor

public void setExecutor(Executor executor)

setDefaultTimeToIdle

public void setDefaultTimeToIdle(long defaultTimeToIdle)

setDefaultTimeToLive

public void setDefaultTimeToLive(long defaultTimeToLive)

setPrincipalHolder

public void setPrincipalHolder(PrincipalHolder principal)

setCounterFactory

public void setCounterFactory(CounterFactory factory)

setReadOnly

public void setReadOnly(boolean readOnly)

init

public void init()
Initialization method called by the Spring run-time to acquire an initial Session.


define

protected void define(ome.model.meta.Session s,
                      String uuid,
                      String message,
                      long started,
                      long idle,
                      long live,
                      String eventType,
                      String agent)

createWithAgent

public ome.model.meta.Session createWithAgent(Principal _principal,
                                              String credentials,
                                              String agent)
Specified by:
createWithAgent in interface SessionManager
Returns:
Not null. Instead an exception will be thrown.

createWithAgent

public ome.model.meta.Session createWithAgent(Principal principal,
                                              String agent)
Specified by:
createWithAgent in interface SessionManager
Returns:
Not null. Instead an exception will be thrown.

createShare

public ome.model.meta.Share createShare(Principal principal,
                                        boolean enabled,
                                        long timeToLive,
                                        String eventType,
                                        String description,
                                        long groupId)
Specified by:
createShare in interface SessionManager
Returns:

update

public ome.model.meta.Session update(ome.model.meta.Session session)
Description copied from interface: SessionManager
See ISession#updateSession(Session) for the logic that's implemented here. Certain fields from the Session instance will be copied and then saved to the db, as well as a new SessionContext created. This method assumes that the user is NOT an admin.

Specified by:
update in interface SessionManager

update

public ome.model.meta.Session update(ome.model.meta.Session session,
                                     boolean trusted)
Description copied from interface: SessionManager
Same as SessionManager.update(Session) but some security checks can be overriden. This is usually done by checking with the SecuritySystem but here the server is in a critical state, and instead will trust the method invoker.

Specified by:
update in interface SessionManager

createSessionContext

protected SessionContext createSessionContext(List<?> list,
                                              SessionContext previous)
Takes a snapshot as from #executeSessionContextLookup(ServiceFactory, Principal) and turns it into a SessionContext instance. List argument should never be null. Abort if #executeSessionContextLookup(ServiceFactory, Principal) returns null.


find

public ome.model.meta.Session find(String uuid)
Specified by:
find in interface SessionManager
Returns:
A current session.

findByUser

public List<ome.model.meta.Session> findByUser(String user)
Specified by:
findByUser in interface SessionManager
Returns:

findByUserAndAgent

public List<ome.model.meta.Session> findByUserAndAgent(String user,
                                                       String... agents)
Description copied from interface: SessionManager
Returns a non-null, possibly empty list of session instances belonging to the given user and with one of the given agents. If the agent list is empty, then only sessions without agent values will be returned.

Specified by:
findByUserAndAgent in interface SessionManager

getReferenceCount

public int getReferenceCount(String uuid)
Description copied from interface: SessionManager
Return the number of client which are presumed to be attached to this session or throw an exception if there's no such session.

Specified by:
getReferenceCount in interface SessionManager

detach

public int detach(String uuid)
Description copied from interface: SessionManager
Allows decrementing the reference count for a session without calling the actual SessionManager.close(String) logic. This is useful when it is assumed that another user will re-attach to the same session. A timeout can still cause the session to be removed.

Specified by:
detach in interface SessionManager
Returns:

getSessionStats

public SessionStats getSessionStats(String uuid)
Description copied from interface: SessionManager
Return the SessionStats which are being counted for the given session or throw an exception if it has been removed.

Specified by:
getSessionStats in interface SessionManager

close

public int close(String uuid)
Description copied from interface: SessionManager
If reference count for the session is less than 1, close the session. Otherwise decrement the reference count. The current reference count is returned. If -1, then no such session existed. If -2, then the session was removed.

Specified by:
close in interface SessionManager

closeAll

public int closeAll()
Description copied from interface: SessionManager
Close all sessions with checking for the necessary reference counts.

Specified by:
closeAll in interface SessionManager

getUserRoles

public List<String> getUserRoles(String uuid)
Specified by:
getUserRoles in interface SessionManager

inMemoryCache

public net.sf.ehcache.Ehcache inMemoryCache(String uuid)
Description copied from interface: SessionManager
Returns after possibly creating an in-memory cache which can be used throughout the session. On close, the cache will be disposed.

Specified by:
inMemoryCache in interface SessionManager

onDiskCache

public net.sf.ehcache.Ehcache onDiskCache(String uuid)
Description copied from interface: SessionManager
Returns after possibly creating an on-disk cache which can be used throughout the session. On close, the cache will be disposed.

Specified by:
onDiskCache in interface SessionManager

getInput

public Object getInput(String session,
                       String key)
                throws ome.conditions.RemovedSessionException
Description copied from interface: SessionManager
Returns the input environment Object stored under the given key or null if none present. Throws an exception if there is no Session with the given identifier.

Specified by:
getInput in interface SessionManager
Parameters:
session - Not null.
key - Not null.
Returns:
Possibly null.
Throws:
ome.conditions.RemovedSessionException

getOutput

public Object getOutput(String session,
                        String key)
                 throws ome.conditions.RemovedSessionException
Description copied from interface: SessionManager
Returns the output environment Object stored under the given key or null if none present. Throws an exception if there is no Session with the given identifier.

Specified by:
getOutput in interface SessionManager
Parameters:
session - Not null.
key - Not null.
Returns:
Possibly null.
Throws:
ome.conditions.RemovedSessionException

inputEnvironment

public Map<String,Object> inputEnvironment(String session)
Description copied from interface: SessionManager
Returns a copy of the input environment.

Specified by:
inputEnvironment in interface SessionManager
Returns:
Not null

outputEnvironment

public Map<String,Object> outputEnvironment(String session)
Description copied from interface: SessionManager
Returns a copy of the output environment.

Specified by:
outputEnvironment in interface SessionManager
Returns:
Not null.

environment

protected Map<String,Object> environment(String session,
                                         String env)

setInput

public void setInput(String session,
                     String key,
                     Object object)
              throws ome.conditions.RemovedSessionException
Description copied from interface: SessionManager
Places the argument in the input environment under the given key, possibly initializing the Map Throws an exception if there is no Session with the given identifier.

Specified by:
setInput in interface SessionManager
Parameters:
session - Not null.
key - Not null.
object - If null, key will be removed.
Throws:
ome.conditions.RemovedSessionException

setOutput

public void setOutput(String session,
                      String key,
                      Object object)
               throws ome.conditions.RemovedSessionException
Description copied from interface: SessionManager
Places the argument in the output environment under the given key, possibly initializing the Map Throws an exception if there is no Session with the given identifier.

Specified by:
setOutput in interface SessionManager
Parameters:
session - Not null.
key - Not null.
object - If null, key will be removed.
Throws:
ome.conditions.RemovedSessionException

getEventContext

public EventContext getEventContext(Principal principal)
Description copied from interface: SessionManager
Provides a partial EventContext for the current Session.

Specified by:
getEventContext in interface SessionManager
Returns:
Never null.

reload

public EventContext reload(String uuid)
Description copied from interface: SessionManager
Similar to SessionManager.getEventContext(Principal) but uses the internal reload logic to get a fresh representation of the context. This queries all of the user management tables (experimenter, experimentergroup, etc) and so should not be used anywhere in a critical path.

Specified by:
reload in interface SessionManager
Parameters:
uuid - non null.
Returns:

notifications

public String[] notifications(String sessionId)

onApplicationEvent

public void onApplicationEvent(org.springframework.context.ApplicationEvent event)
Specified by:
onApplicationEvent in interface org.springframework.context.ApplicationListener

addCallback

public void addCallback(String sessionId,
                        SessionCallback cb)

getCallbackObject

public Object getCallbackObject(String sessionId,
                                String name)

copy

public ome.model.meta.Session copy(ome.model.meta.Session source)

prepareReload

public void prepareReload()

reload

public SessionContext reload(SessionContext ctx)
Will be called in a synchronized block by SessionCache in order to allow for an update.

Specified by:
reload in interface SessionCache.StaleCacheListener

executePasswordCheck

public boolean executePasswordCheck(String name,
                                    String credentials)
Description copied from interface: SessionManager
Executes a password check using the Executor framework. Also checks the credentials against current session uuids.

Specified by:
executePasswordCheck in interface SessionManager

setSecurityContext

public ome.model.IObject setSecurityContext(Principal principal,
                                            ome.model.IObject obj)
Description copied from interface: SessionManager
Sets the context for the current session to the given value. If it is an ExperimenterGroup then the active group is changed, and any active shares are deactivated. If it is an Share then the share is activate (the group is left alone). Unless otherwise specified, the user's default group is used as the initial context. Passing any other object will result in an ApiUsageException.

Specified by:
setSecurityContext in interface SessionManager
Parameters:
principal - Principal for which the context should be set.
obj - IObject which represents the new context.


OmeroJava Api

Version: 4.3.3-00d1137e-b2894

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