Package ome.services.sessions
Class SessionManagerImpl
- java.lang.Object
-
- ome.services.sessions.SessionManagerImpl
-
- All Implemented Interfaces:
java.util.EventListener
,SessionManager
,SessionCache.StaleCacheListener
,org.springframework.beans.factory.Aware
,org.springframework.context.ApplicationContextAware
,org.springframework.context.ApplicationListener<org.springframework.context.ApplicationEvent>
public class SessionManagerImpl extends java.lang.Object implements SessionManager, SessionCache.StaleCacheListener, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationListener<org.springframework.context.ApplicationEvent>
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 anApplicationListener
, which should be used to keep theSession
instances up-to-date.- Since:
- 3.0-Beta3
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface ome.services.sessions.SessionManager
SessionManager.CreationRequest
-
-
Field Summary
Fields Modifier and Type Field Description protected LightAdminPrivileges
adminPrivileges
protected ome.system.Principal
asroot
A private session for use only by this instance for running methods viaExecutor
.protected SessionCache
cache
protected ome.system.OmeroContext
context
protected long
defaultTimeToIdle
protected long
defaultTimeToLive
protected Executor
executor
protected CounterFactory
factory
static java.lang.String
GROUP_SUDO_NS
protected SessionContext
internalSession
InternalSessionContext
created duringinit()
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 ome.system.Roles
roles
protected SessionProvider
sessionProvider
-
Constructor Summary
Constructors Constructor Description SessionManagerImpl()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addCallback(java.lang.String sessionId, SessionCallback cb)
int
close(java.lang.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)
ome.model.meta.Session
createFromRequest(SessionManager.CreationRequest request)
protected SessionContext
createSessionContext(java.util.List<?> list, SessionContext previous)
Takes a snapshot as fromexecuteSessionContextLookup(ServiceFactory, Principal, Session)
and turns it into aSessionContext
instance.ome.model.meta.Share
createShare(ome.system.Principal principal, boolean enabled, long timeToLive, java.lang.String eventType, java.lang.String description, long groupId)
ome.model.meta.Session
createWithAgent(ome.system.Principal principal, java.lang.String agent, java.lang.String ip)
ome.model.meta.Session
createWithAgent(ome.system.Principal _principal, java.lang.String credentials, java.lang.String agent, java.lang.String ip)
protected void
define(ome.model.meta.Session s, java.lang.String uuid, java.lang.String message, long started, long idle, long live, java.lang.String eventType, java.lang.String agent, java.lang.String ip)
protected void
define(ome.model.meta.Session s, java.lang.String uuid, java.lang.String message, long started, SessionManager.CreationRequest req)
int
detach(java.lang.String uuid)
Allows decrementing the reference count for a session without calling the actualSessionManager.close(String)
logic.protected java.util.Map<java.lang.String,java.lang.Object>
environment(java.lang.String session, java.lang.String env)
boolean
executePasswordCheck(java.lang.String name, java.lang.String credentials)
Executes a password check using theExecutor
framework.ome.model.meta.Session
find(java.lang.String uuid)
Finds aSession
and update the last access time.ome.model.meta.Session
findQuietly(java.lang.String uuid)
Finds aSession
without updating the last access time.java.util.List<ome.model.meta.Session>
findSameUser(java.lang.String uuid, java.lang.String... agents)
Returns a non-null, possibly empty list of session instances belonging to the same user as the given session with one of the given agents.java.lang.Object
getCallbackObject(java.lang.String sessionId, java.lang.String name)
ome.system.EventContext
getEventContext(ome.system.Principal principal)
Provides a partialEventContext
for the currentSession
.java.lang.Object
getInput(java.lang.String session, java.lang.String key)
Returns the input environmentObject
stored under the given key or null if none present.java.lang.Object
getOutput(java.lang.String session, java.lang.String key)
Returns the output environmentObject
stored under the given key or null if none present.int
getReferenceCount(java.lang.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.java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>
getSessionData()
Return all sessions that are active with associated possibly varying session data information.SessionStats
getSessionStats(java.lang.String uuid)
Return theSessionStats
which are being counted for the given session or throw an exception if it has been removed.java.util.List<java.lang.String>
getUserRoles(java.lang.String uuid)
void
init()
Initialization method called by the Spring run-time to acquire an initialSession
.net.sf.ehcache.Ehcache
inMemoryCache(java.lang.String uuid)
Returns after possibly creating an in-memorycache
which can be used throughout the session.java.util.Map<java.lang.String,java.lang.Object>
inputEnvironment(java.lang.String session)
Returns a copy of the input environment.java.lang.String[]
notifications(java.lang.String sessionId)
void
onApplicationEvent(org.springframework.context.ApplicationEvent event)
net.sf.ehcache.Ehcache
onDiskCache(java.lang.String uuid)
Returns after possibly creating an on-diskcache
which can be used throughout the session.java.util.Map<java.lang.String,java.lang.Object>
outputEnvironment(java.lang.String session)
Returns a copy of the output environment.void
prepareReload()
ome.system.EventContext
reload(java.lang.String uuid)
Similar toSessionManager.getEventContext(Principal)
but uses the internal reload logic to get a fresh representation of the context.SessionContext
reload(SessionContext ctx)
Will be called in a synchronized block bySessionCache
in order to allow for an update.void
setAdminPrivileges(LightAdminPrivileges adminPrivileges)
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(java.lang.String session, java.lang.String key, java.lang.Object object)
Places theargument
in the input environment under the given key, possibly initializing theMap
Throws an exception if there is noSession
with the given identifier.void
setMaxUserTimeToIdle(long maxUserTimeToIdle)
void
setMaxUserTimeToLive(long maxUserTimeToLive)
void
setOutput(java.lang.String session, java.lang.String key, java.lang.Object object)
Places theargument
in the output environment under the given key, possibly initializing theMap
Throws an exception if there is noSession
with the given identifier.void
setPrincipalHolder(PrincipalHolder principal)
void
setReadOnly(ReadOnlyStatus readOnly)
void
setRoles(ome.system.Roles securityRoles)
ome.model.IObject
setSecurityContext(ome.system.Principal principal, ome.model.IObject obj)
Sets the context for the current session to the given value.void
setSessionCache(SessionCache sessionCache)
void
setSessionProvider(SessionProvider sessionProvider)
void
setUuid(java.lang.String uuid)
ome.model.meta.Session
update(ome.model.meta.Session session)
Certain fields from theSession
instance will be copied and then saved to the db, as well as a newSessionContext
created.ome.model.meta.Session
update(ome.model.meta.Session session, boolean trusted)
Same asSessionManager.update(Session)
but some security checks can be overridden.
-
-
-
Field Detail
-
GROUP_SUDO_NS
public static final java.lang.String GROUP_SUDO_NS
- See Also:
- Constant Field Values
-
context
protected ome.system.OmeroContext context
-
roles
protected ome.system.Roles roles
-
adminPrivileges
protected LightAdminPrivileges adminPrivileges
-
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
-
sessionProvider
protected SessionProvider sessionProvider
-
asroot
protected ome.system.Principal asroot
A private session for use only by this instance for running methods viaExecutor
. The name of thisPrincipal
will not be removed by calls tocloseAll()
.
-
internalSession
protected SessionContext internalSession
InternalSessionContext
created duringinit()
which is used for all method calls internal to the session manager (see execute* methods)
-
-
Method Detail
-
setApplicationContext
public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException
- Specified by:
setApplicationContext
in interfaceorg.springframework.context.ApplicationContextAware
- Throws:
org.springframework.beans.BeansException
-
setUuid
public void setUuid(java.lang.String uuid)
-
setSessionCache
public void setSessionCache(SessionCache sessionCache)
-
setRoles
public void setRoles(ome.system.Roles securityRoles)
-
setAdminPrivileges
public void setAdminPrivileges(LightAdminPrivileges adminPrivileges)
-
setExecutor
public void setExecutor(Executor executor)
-
setDefaultTimeToIdle
public void setDefaultTimeToIdle(long defaultTimeToIdle)
-
setMaxUserTimeToIdle
public void setMaxUserTimeToIdle(long maxUserTimeToIdle)
-
setDefaultTimeToLive
public void setDefaultTimeToLive(long defaultTimeToLive)
-
setMaxUserTimeToLive
public void setMaxUserTimeToLive(long maxUserTimeToLive)
-
setPrincipalHolder
public void setPrincipalHolder(PrincipalHolder principal)
-
setCounterFactory
public void setCounterFactory(CounterFactory factory)
-
setReadOnly
public void setReadOnly(ReadOnlyStatus readOnly)
-
setSessionProvider
public void setSessionProvider(SessionProvider sessionProvider)
-
init
public void init()
Initialization method called by the Spring run-time to acquire an initialSession
.
-
define
protected void define(ome.model.meta.Session s, java.lang.String uuid, java.lang.String message, long started, SessionManager.CreationRequest req)
-
define
protected void define(ome.model.meta.Session s, java.lang.String uuid, java.lang.String message, long started, long idle, long live, java.lang.String eventType, java.lang.String agent, java.lang.String ip)
-
createFromRequest
public ome.model.meta.Session createFromRequest(SessionManager.CreationRequest request)
- Specified by:
createFromRequest
in interfaceSessionManager
-
createWithAgent
public ome.model.meta.Session createWithAgent(ome.system.Principal _principal, java.lang.String credentials, java.lang.String agent, java.lang.String ip)
- Specified by:
createWithAgent
in interfaceSessionManager
- Returns:
- Not null. Instead an exception will be thrown.
-
createWithAgent
public ome.model.meta.Session createWithAgent(ome.system.Principal principal, java.lang.String agent, java.lang.String ip)
- Specified by:
createWithAgent
in interfaceSessionManager
- Returns:
- Not null. Instead an exception will be thrown.
-
createShare
public ome.model.meta.Share createShare(ome.system.Principal principal, boolean enabled, long timeToLive, java.lang.String eventType, java.lang.String description, long groupId)
- Specified by:
createShare
in interfaceSessionManager
- Returns:
- Not null. Instead an exception will be thrown.
-
update
public ome.model.meta.Session update(ome.model.meta.Session session)
Description copied from interface:SessionManager
Certain fields from theSession
instance will be copied and then saved to the db, as well as a newSessionContext
created. This method assumes that the user is NOT an admin.- Specified by:
update
in interfaceSessionManager
-
update
public ome.model.meta.Session update(ome.model.meta.Session session, boolean trusted)
Description copied from interface:SessionManager
Same asSessionManager.update(Session)
but some security checks can be overridden. This is usually done by checking with theSecuritySystem
but here the server is in a critical state, and instead will trust the method invoker.- Specified by:
update
in interfaceSessionManager
-
createSessionContext
protected SessionContext createSessionContext(java.util.List<?> list, SessionContext previous)
Takes a snapshot as fromexecuteSessionContextLookup(ServiceFactory, Principal, Session)
and turns it into aSessionContext
instance. List argument should never be null. Abort ifexecuteSessionContextLookup(ServiceFactory, Principal, Session)
returns null.
-
find
public ome.model.meta.Session find(java.lang.String uuid)
Description copied from interface:SessionManager
Finds aSession
and update the last access time.- Specified by:
find
in interfaceSessionManager
- Returns:
- A current session.
-
findQuietly
public ome.model.meta.Session findQuietly(java.lang.String uuid)
Description copied from interface:SessionManager
Finds aSession
without updating the last access time.- Specified by:
findQuietly
in interfaceSessionManager
- Returns:
- A current session.
-
findSameUser
public java.util.List<ome.model.meta.Session> findSameUser(java.lang.String uuid, java.lang.String... agents)
Description copied from interface:SessionManager
Returns a non-null, possibly empty list of session instances belonging to the same user as the given session with one of the given agents. Sudo is taken into account. If the agent list is empty then all are included. Anull
may be included in the list to include null agents.- Specified by:
findSameUser
in interfaceSessionManager
-
getReferenceCount
public int getReferenceCount(java.lang.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 interfaceSessionManager
-
detach
public int detach(java.lang.String uuid)
Description copied from interface:SessionManager
Allows decrementing the reference count for a session without calling the actualSessionManager.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 interfaceSessionManager
- Returns:
- See above.
-
getSessionStats
public SessionStats getSessionStats(java.lang.String uuid)
Description copied from interface:SessionManager
Return theSessionStats
which are being counted for the given session or throw an exception if it has been removed.- Specified by:
getSessionStats
in interfaceSessionManager
-
close
public int close(java.lang.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 interfaceSessionManager
-
getSessionData
public java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>> getSessionData()
Description copied from interface:SessionManager
Return all sessions that are active with associated possibly varying session data information.- Specified by:
getSessionData
in interfaceSessionManager
-
closeAll
public int closeAll()
Description copied from interface:SessionManager
Close all sessions with checking for the necessary reference counts.- Specified by:
closeAll
in interfaceSessionManager
-
getUserRoles
public java.util.List<java.lang.String> getUserRoles(java.lang.String uuid)
- Specified by:
getUserRoles
in interfaceSessionManager
-
inMemoryCache
public net.sf.ehcache.Ehcache inMemoryCache(java.lang.String uuid)
Description copied from interface:SessionManager
Returns after possibly creating an in-memorycache
which can be used throughout the session. On close, the cache will be disposed.- Specified by:
inMemoryCache
in interfaceSessionManager
-
onDiskCache
public net.sf.ehcache.Ehcache onDiskCache(java.lang.String uuid)
Description copied from interface:SessionManager
Returns after possibly creating an on-diskcache
which can be used throughout the session. On close, the cache will be disposed.- Specified by:
onDiskCache
in interfaceSessionManager
-
getInput
public java.lang.Object getInput(java.lang.String session, java.lang.String key) throws ome.conditions.RemovedSessionException
Description copied from interface:SessionManager
Returns the input environmentObject
stored under the given key or null if none present. Throws an exception if there is noSession
with the given identifier.- Specified by:
getInput
in interfaceSessionManager
- Parameters:
session
- Not null.key
- Not null.- Returns:
- Possibly null.
- Throws:
ome.conditions.RemovedSessionException
-
getOutput
public java.lang.Object getOutput(java.lang.String session, java.lang.String key) throws ome.conditions.RemovedSessionException
Description copied from interface:SessionManager
Returns the output environmentObject
stored under the given key or null if none present. Throws an exception if there is noSession
with the given identifier.- Specified by:
getOutput
in interfaceSessionManager
- Parameters:
session
- Not null.key
- Not null.- Returns:
- Possibly null.
- Throws:
ome.conditions.RemovedSessionException
-
inputEnvironment
public java.util.Map<java.lang.String,java.lang.Object> inputEnvironment(java.lang.String session)
Description copied from interface:SessionManager
Returns a copy of the input environment.- Specified by:
inputEnvironment
in interfaceSessionManager
- Parameters:
session
- The session id.- Returns:
- Not null
-
outputEnvironment
public java.util.Map<java.lang.String,java.lang.Object> outputEnvironment(java.lang.String session)
Description copied from interface:SessionManager
Returns a copy of the output environment.- Specified by:
outputEnvironment
in interfaceSessionManager
- Parameters:
session
- The session id.- Returns:
- Not null.
-
environment
protected java.util.Map<java.lang.String,java.lang.Object> environment(java.lang.String session, java.lang.String env)
-
setInput
public void setInput(java.lang.String session, java.lang.String key, java.lang.Object object) throws ome.conditions.RemovedSessionException
Description copied from interface:SessionManager
Places theargument
in the input environment under the given key, possibly initializing theMap
Throws an exception if there is noSession
with the given identifier.- Specified by:
setInput
in interfaceSessionManager
- Parameters:
session
- Not null.key
- Not null.object
- If null, key will be removed.- Throws:
ome.conditions.RemovedSessionException
-
setOutput
public void setOutput(java.lang.String session, java.lang.String key, java.lang.Object object) throws ome.conditions.RemovedSessionException
Description copied from interface:SessionManager
Places theargument
in the output environment under the given key, possibly initializing theMap
Throws an exception if there is noSession
with the given identifier.- Specified by:
setOutput
in interfaceSessionManager
- Parameters:
session
- Not null.key
- Not null.object
- If null, key will be removed.- Throws:
ome.conditions.RemovedSessionException
-
getEventContext
public ome.system.EventContext getEventContext(ome.system.Principal principal)
Description copied from interface:SessionManager
Provides a partialEventContext
for the currentSession
.- Specified by:
getEventContext
in interfaceSessionManager
- Parameters:
principal
- Non null.- Returns:
- Never null.
-
reload
public ome.system.EventContext reload(java.lang.String uuid)
Description copied from interface:SessionManager
Similar toSessionManager.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 interfaceSessionManager
- Parameters:
uuid
- non null.- Returns:
- See above.
-
notifications
public java.lang.String[] notifications(java.lang.String sessionId)
-
onApplicationEvent
public void onApplicationEvent(org.springframework.context.ApplicationEvent event)
- Specified by:
onApplicationEvent
in interfaceorg.springframework.context.ApplicationListener<org.springframework.context.ApplicationEvent>
-
addCallback
public void addCallback(java.lang.String sessionId, SessionCallback cb)
-
getCallbackObject
public java.lang.Object getCallbackObject(java.lang.String sessionId, java.lang.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 bySessionCache
in order to allow for an update.- Specified by:
reload
in interfaceSessionCache.StaleCacheListener
-
executePasswordCheck
public boolean executePasswordCheck(java.lang.String name, java.lang.String credentials)
Description copied from interface:SessionManager
Executes a password check using theExecutor
framework. Also checks the credentials against current session uuids.- Specified by:
executePasswordCheck
in interfaceSessionManager
- Returns:
- See above.
-
setSecurityContext
public ome.model.IObject setSecurityContext(ome.system.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 anExperimenterGroup
then the active group is changed, and any active shares are deactivated. If it is anShare
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 anApiUsageException
.- Specified by:
setSecurityContext
in interfaceSessionManager
- Parameters:
principal
-Principal
for which the context should be set.obj
-IObject
which represents the new context.- Returns:
- See above.
-
-