Class VaadinSession
- java.lang.Object
-
- com.vaadin.server.VaadinSession
-
- All Implemented Interfaces:
Serializable
,EventListener
,javax.servlet.http.HttpSessionBindingListener
public class VaadinSession extends Object implements javax.servlet.http.HttpSessionBindingListener, Serializable
Contains everything that Vaadin needs to store for a specific user. This is typically stored in aHttpSession
, but others storage mechanisms might also be used.Everything inside a
VaadinSession
should be serializable to ensure compatibility with schemes using serialization for persisting the session data.- Since:
- 7.0.0
- Author:
- Vaadin Ltd
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
VaadinSession.FutureAccess
Encapsulates aRunnable
submitted usingaccess(Runnable)
.static class
VaadinSession.State
The lifecycle state of a VaadinSession.
-
Field Summary
Fields Modifier and Type Field Description protected WebBrowser
browser
static String
UI_PARAMETER
The name of the parameter that is by default used in e.g.static String
UI_PRIORITY_PARAMETER
The priority of the specified default UI class (defaults to 0)
-
Constructor Summary
Constructors Constructor Description VaadinSession(VaadinService service)
Creates a new VaadinSession tied to a VaadinService.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Future<Void>
access(Runnable runnable)
Provides exclusive access to this session from outside a request handling thread.void
accessSynchronously(Runnable runnable)
Locks this session and runs the provided Runnable right away.void
addBootstrapListener(BootstrapListener listener)
Adds a listener that will be invoked when the bootstrap HTML is about to be generated.void
addRequestHandler(RequestHandler handler)
Adds a request handler to this session.void
addUI(UI ui)
Adds an initialized UI to this session.void
addUIProvider(UIProvider uiProvider)
Adds a UI provider to this session.void
addUIProvider(UIProvider uiProvider, int priority)
Adds a UI provider to this session with a persistent priority value.void
close()
Sets this session to be closed and all UI state to be discarded at the end of the current request, or at the end of the next request if there is no ongoing one.String
createConnectorId(ClientConnector connector)
Deprecated.As of 7.0.static Collection<VaadinSession>
getAllSessions(javax.servlet.http.HttpSession httpSession)
Retrieves allVaadinSession
s which are stored in the given HTTP session<T> T
getAttribute(Class<T> type)
Gets a stored attribute value.Object
getAttribute(String name)
Gets a stored attribute value.WebBrowser
getBrowser()
Deprecated.As of 7.0, usePage.getWebBrowser()
instead.LegacyCommunicationManager
getCommunicationManager()
Deprecated.As of 7.0.DeploymentConfiguration
getConfiguration()
Gets the configuration for this sessionConverterFactory
getConverterFactory()
Gets theConverterFactory
used to locate a suitableConverter
for fields in the session.String
getCsrfToken()
Gets the CSRF token (aka double submit cookie) that is used to protect against Cross Site Request Forgery attacks.long
getCumulativeRequestDuration()
static VaadinSession
getCurrent()
Gets the currently used session.DragAndDropService
getDragAndDropService()
ErrorHandler
getErrorHandler()
Gets the session's error handler.static VaadinSession
getForSession(VaadinService service, WrappedSession underlyingSession)
Deprecated.as of 7.6, callVaadinService.loadSession(WrappedSession)
insteadGlobalResourceHandler
getGlobalResourceHandler(boolean createOnDemand)
Gets this session's global resource handler that takes care of serving connector resources that are not served by any single connector because e.g.long
getLastRequestDuration()
long
getLastRequestTimestamp()
Returns the time when the last request was serviced in this session.Locale
getLocale()
Gets the default locale for this session.Lock
getLockInstance()
Gets theLock
instance that is used for protecting the data of this session from concurrent access.int
getNextUIid()
Creates a new unique id for a UI.Queue<VaadinSession.FutureAccess>
getPendingAccessQueue()
Gets the queue of tasks submitted usingaccess(Runnable)
.String
getPushId()
Gets the push connection identifier for this session.Collection<RequestHandler>
getRequestHandlers()
Gets the request handlers that are registered to the session.VaadinService
getService()
WrappedSession
getSession()
Gets the underlying session to which this service session is currently associated.VaadinSession.State
getState()
Returns the lifecycle state of this session.UI
getUIByEmbedId(String embedId)
Finds the UI with the corresponding embed id.UI
getUIById(int uiId)
Returns a UI with the given id.int
getUIProviderPriority(UIProvider provider)
Get the priority of a specific, already added UI provider.List<UIProvider>
getUIProviders()
Gets the UI providers configured for this session.Collection<UI>
getUIs()
Gets all the UIs of this session.boolean
hasLock()
Checks if the current thread has exclusive access to this VaadinSessionprotected static boolean
hasLock(VaadinService service, WrappedSession session)
Checks if the current thread has exclusive access to the given WrappedSession.boolean
isClosing()
Deprecated.As of 7.2, use
instead.getState() != State.OPEN
void
lock()
Locks this session to protect its data from concurrent access.void
modifyBootstrapResponse(BootstrapResponse response)
Deprecated.As of 7.0.void
refreshTransients(WrappedSession wrappedSession, VaadinService vaadinService)
Refreshes the transient fields of the session to ensure they are up to date.void
removeBootstrapListener(BootstrapListener listener)
Remove a bootstrap listener that was previously added.void
removeFromSession(VaadinService service)
Deprecated.as of 7.6, callVaadinService.removeSession(WrappedSession)
insteadvoid
removeRequestHandler(RequestHandler handler)
Removes a request handler from the session.void
removeUI(UI ui)
Called by the framework to remove an UI instance from the session because it has been closed.void
removeUIProvider(UIProvider uiProvider)
Removes a UI provider association from this session.<T> void
setAttribute(Class<T> type, T value)
Stores a value in this service session.void
setAttribute(String name, Object value)
Stores a value in this service session.void
setCommunicationManager(LegacyCommunicationManager communicationManager)
void
setConfiguration(DeploymentConfiguration configuration)
void
setConverterFactory(ConverterFactory converterFactory)
Sets theConverterFactory
used to locate a suitableConverter
for fields in the session.static void
setCurrent(VaadinSession session)
Sets the thread local for the current session.void
setErrorHandler(ErrorHandler errorHandler)
Sets the session error handler.void
setLastRequestDuration(long time)
Sets the time spent servicing the last request in the session and updates the total time spent servicing requests in this session.void
setLastRequestTimestamp(long timestamp)
Sets the time when the last UIDL request was serviced in this session.void
setLocale(Locale locale)
Sets the default locale for this session.protected void
setState(VaadinSession.State state)
Sets the lifecycle state of this session.void
setUIProviderPriority(UIProvider provider, int priority)
Set the priority of a specific, already added UI provider.void
storeInSession(VaadinService service, WrappedSession session)
Deprecated.as of 7.6, callVaadinService.storeSession(VaadinSession, WrappedSession)
insteadvoid
unlock()
Unlocks this session.void
valueBound(javax.servlet.http.HttpSessionBindingEvent arg0)
void
valueUnbound(javax.servlet.http.HttpSessionBindingEvent event)
-
-
-
Field Detail
-
UI_PARAMETER
public static final String UI_PARAMETER
The name of the parameter that is by default used in e.g. web.xml to define the name of the defaultUI
class.- See Also:
- Constant Field Values
-
UI_PRIORITY_PARAMETER
public static final String UI_PRIORITY_PARAMETER
The priority of the specified default UI class (defaults to 0)- See Also:
- Constant Field Values
-
browser
protected WebBrowser browser
-
-
Constructor Detail
-
VaadinSession
public VaadinSession(VaadinService service)
Creates a new VaadinSession tied to a VaadinService.- Parameters:
service
- the Vaadin service for the new session
-
-
Method Detail
-
valueBound
public void valueBound(javax.servlet.http.HttpSessionBindingEvent arg0)
- Specified by:
valueBound
in interfacejavax.servlet.http.HttpSessionBindingListener
- See Also:
HttpSessionBindingListener.valueBound(HttpSessionBindingEvent)
-
valueUnbound
public void valueUnbound(javax.servlet.http.HttpSessionBindingEvent event)
- Specified by:
valueUnbound
in interfacejavax.servlet.http.HttpSessionBindingListener
- See Also:
HttpSessionBindingListener.valueUnbound(HttpSessionBindingEvent)
-
getBrowser
@Deprecated public WebBrowser getBrowser()
Deprecated.As of 7.0, usePage.getWebBrowser()
instead.Get the web browser associated with this session.- Returns:
- the web browser object
-
getCumulativeRequestDuration
public long getCumulativeRequestDuration()
- Returns:
- The total time spent servicing requests in this session, in milliseconds.
-
setLastRequestDuration
public void setLastRequestDuration(long time)
Sets the time spent servicing the last request in the session and updates the total time spent servicing requests in this session.- Parameters:
time
- The time spent in the last request, in milliseconds.
-
getLastRequestDuration
public long getLastRequestDuration()
- Returns:
- The time spent servicing the last request in this session, in milliseconds.
-
setLastRequestTimestamp
public void setLastRequestTimestamp(long timestamp)
Sets the time when the last UIDL request was serviced in this session.- Parameters:
timestamp
- The time when the last request was handled, in milliseconds since the epoch.
-
getLastRequestTimestamp
public long getLastRequestTimestamp()
Returns the time when the last request was serviced in this session.- Returns:
- The time when the last request was handled, in milliseconds since the epoch.
-
getSession
public WrappedSession getSession()
Gets the underlying session to which this service session is currently associated.- Returns:
- the wrapped session for this context
-
getCommunicationManager
@Deprecated public LegacyCommunicationManager getCommunicationManager()
Deprecated.As of 7.0. Will likely change or be removed in a future version- Returns:
-
getDragAndDropService
public DragAndDropService getDragAndDropService()
-
getForSession
@Deprecated public static VaadinSession getForSession(VaadinService service, WrappedSession underlyingSession)
Deprecated.as of 7.6, callVaadinService.loadSession(WrappedSession)
insteadLoads the VaadinSession for the given service and WrappedSession from the HTTP session.- Parameters:
service
- The service the VaadinSession is associated withunderlyingSession
- The wrapped HTTP session for the user- Returns:
- A VaadinSession instance for the service, session combination or null if none was found.
-
getAllSessions
public static Collection<VaadinSession> getAllSessions(javax.servlet.http.HttpSession httpSession)
Retrieves allVaadinSession
s which are stored in the given HTTP session- Parameters:
httpSession
- the HTTP session- Returns:
- the found VaadinSessions
- Since:
- 7.2
-
removeFromSession
@Deprecated public void removeFromSession(VaadinService service)
Deprecated.as of 7.6, callVaadinService.removeSession(WrappedSession)
insteadRemoves this VaadinSession from the HTTP session.- Parameters:
service
- The service this session is associated with
-
storeInSession
@Deprecated public void storeInSession(VaadinService service, WrappedSession session)
Deprecated.as of 7.6, callVaadinService.storeSession(VaadinSession, WrappedSession)
insteadStores this VaadinSession in the HTTP session.- Parameters:
service
- The service this session is associated withsession
- The HTTP session this VaadinSession should be stored in
-
setCommunicationManager
public void setCommunicationManager(LegacyCommunicationManager communicationManager)
-
setConfiguration
public void setConfiguration(DeploymentConfiguration configuration)
-
getConfiguration
public DeploymentConfiguration getConfiguration()
Gets the configuration for this session- Returns:
- the deployment configuration
-
getLocale
public Locale getLocale()
Gets the default locale for this session. By default this is the preferred locale of the user using the session. In most cases it is read from the browser defaults.- Returns:
- the locale of this session.
-
setLocale
public void setLocale(Locale locale)
Sets the default locale for this session. By default this is the preferred locale of the user using the application. In most cases it is read from the browser defaults.- Parameters:
locale
- the Locale object.
-
getErrorHandler
public ErrorHandler getErrorHandler()
Gets the session's error handler.- Returns:
- the current error handler
-
setErrorHandler
public void setErrorHandler(ErrorHandler errorHandler)
Sets the session error handler.- Parameters:
errorHandler
-
-
getConverterFactory
public ConverterFactory getConverterFactory()
Gets theConverterFactory
used to locate a suitableConverter
for fields in the session. SeesetConverterFactory(ConverterFactory)
for more details- Returns:
- The converter factory used in the session
-
setConverterFactory
public void setConverterFactory(ConverterFactory converterFactory)
Sets theConverterFactory
used to locate a suitableConverter
for fields in the session.The
ConverterFactory
is used to find a suitable converter when binding data to a UI component and the data type does not match the UI component type, e.g. binding a Double to a TextField (which is based on a String).The
Converter
for an individual field can be overridden usingAbstractField.setConverter(Converter)
and for individual property ids in aTable
usingTable.setConverter(Object, Converter)
.The converter factory must never be set to null.
- Parameters:
converterFactory
- The converter factory used in the session
-
addRequestHandler
public void addRequestHandler(RequestHandler handler)
Adds a request handler to this session. Request handlers can be added to provide responses to requests that are not handled by the default functionality of the framework.Handlers are called in reverse order of addition, so the most recently added handler will be called first.
- Parameters:
handler
- the request handler to add- Since:
- 7.0
- See Also:
removeRequestHandler(RequestHandler)
-
removeRequestHandler
public void removeRequestHandler(RequestHandler handler)
Removes a request handler from the session.- Parameters:
handler
- the request handler to remove- Since:
- 7.0
-
getRequestHandlers
public Collection<RequestHandler> getRequestHandlers()
Gets the request handlers that are registered to the session. The iteration order of the returned collection is the same as the order in which the request handlers will be invoked when a request is handled.- Returns:
- a collection of request handlers, with the iteration order according to the order they would be invoked
- Since:
- 7.0
- See Also:
addRequestHandler(RequestHandler)
,removeRequestHandler(RequestHandler)
-
getCurrent
public static VaadinSession getCurrent()
Gets the currently used session. The current session is automatically defined when processing requests to the server and in threads started at a point when the current session is defined (seeInheritableThreadLocal
). In other cases, (e.g. from background threads started in some other way), the current session is not automatically defined.The session is stored using a weak reference to avoid leaking memory in case it is not explicitly cleared.
- Returns:
- the current session instance if available, otherwise
null
- Since:
- 7.0
- See Also:
setCurrent(VaadinSession)
-
setCurrent
public static void setCurrent(VaadinSession session)
Sets the thread local for the current session. This method is used by the framework to set the current session whenever a new request is processed and it is cleared when the request has been processed.The application developer can also use this method to define the current session outside the normal request handling and treads started from request handling threads, e.g. when initiating custom background threads.
The session is stored using a weak reference to avoid leaking memory in case it is not explicitly cleared.
- Parameters:
session
- the session to set as current- Since:
- 7.0
- See Also:
getCurrent()
,ThreadLocal
-
getUIs
public Collection<UI> getUIs()
Gets all the UIs of this session. This includes UIs that have been requested but not yet initialized. UIs that receive no heartbeat requests from the client are eventually removed from the session.- Returns:
- a collection of UIs belonging to this application
- Since:
- 7.0
-
createConnectorId
@Deprecated public String createConnectorId(ClientConnector connector)
Deprecated.As of 7.0. Will likely change or be removed in a future versionGenerate an id for the given Connector. Connectors must not call this method more than once, the first time they need an id.- Parameters:
connector
- A connector that has not yet been assigned an id.- Returns:
- A new id for the connector
-
getUIById
public UI getUIById(int uiId)
Returns a UI with the given id.This is meant for framework internal use.
- Parameters:
uiId
- The UI id- Returns:
- The UI with the given id or null if not found
-
hasLock
public boolean hasLock()
Checks if the current thread has exclusive access to this VaadinSession- Returns:
- true if the thread has exclusive access, false otherwise
- Since:
- 7.1
-
hasLock
protected static boolean hasLock(VaadinService service, WrappedSession session)
Checks if the current thread has exclusive access to the given WrappedSession.- Returns:
- true if this thread has exclusive access, false otherwise
- Since:
- 7.6
-
addBootstrapListener
public void addBootstrapListener(BootstrapListener listener)
Adds a listener that will be invoked when the bootstrap HTML is about to be generated. This can be used to modify the contents of the HTML that loads the Vaadin application in the browser and the HTTP headers that are included in the response serving the HTML.- Parameters:
listener
- the bootstrap listener to add- See Also:
BootstrapListener.modifyBootstrapFragment(BootstrapFragmentResponse)
,BootstrapListener.modifyBootstrapPage(BootstrapPageResponse)
-
removeBootstrapListener
public void removeBootstrapListener(BootstrapListener listener)
Remove a bootstrap listener that was previously added.- Parameters:
listener
- the bootstrap listener to remove- See Also:
addBootstrapListener(BootstrapListener)
-
modifyBootstrapResponse
@Deprecated public void modifyBootstrapResponse(BootstrapResponse response)
Deprecated.As of 7.0. Will likely change or be removed in a future versionFires a bootstrap event to all registered listeners. There are currently two supported events, both inheriting fromBootstrapResponse
:BootstrapFragmentResponse
andBootstrapPageResponse
.- Parameters:
response
- the bootstrap response event for which listeners should be fired
-
removeUI
public void removeUI(UI ui)
Called by the framework to remove an UI instance from the session because it has been closed.- Parameters:
ui
- the UI to remove
-
getGlobalResourceHandler
public GlobalResourceHandler getGlobalResourceHandler(boolean createOnDemand)
Gets this session's global resource handler that takes care of serving connector resources that are not served by any single connector because e.g. because they are served with strong caching or because of legacy reasons.- Parameters:
createOnDemand
-true
if a resource handler should be initialized if there is no handler associated with this application,false
ifnull
should be returned if there is no registered handler.- Returns:
- this session's global resource handler, or
null
if there is no handler and the createOnDemand parameter isfalse
. - Since:
- 7.0.0
-
getLockInstance
public Lock getLockInstance()
Gets theLock
instance that is used for protecting the data of this session from concurrent access.The
Lock
can be used to gain more control than what is available only usinglock()
andunlock()
. The returned instance is not guaranteed to support any other features of theLock
interface thanLock.lock()
andLock.unlock()
.
-
lock
public void lock()
Locks this session to protect its data from concurrent access. Accessing the UI state from outside the normal request handling should always lock the session and unlock it when done. The preferred way to ensure locking is done correctly is to wrap your code usingUI.access(Runnable)
(oraccess(Runnable)
if you are only touching the session and not any UI), e.g.:myUI.access(new Runnable() { @Override public void run() { // Here it is safe to update the UI. // UI.getCurrent can also be used myUI.getContent().setCaption("Changed safely"); } });
If you for whatever reason want to do locking manually, you should do it like:session.lock(); try { doSomething(); } finally { session.unlock(); }
This method will block until the lock can be retrieved.getLockInstance()
can be used if more control over the locking is required.- See Also:
unlock()
,getLockInstance()
,hasLock()
-
unlock
public void unlock()
-
setAttribute
public void setAttribute(String name, Object value)
Stores a value in this service session. This can be used to associate data with the current user so that it can be retrieved at a later point from some other part of the application. Setting the value tonull
clears the stored value.- Parameters:
name
- the name to associate the value with, can not benull
value
- the value to associate with the name, ornull
to remove a previous association.- See Also:
getAttribute(String)
-
setAttribute
public <T> void setAttribute(Class<T> type, T value)
Stores a value in this service session. This can be used to associate data with the current user so that it can be retrieved at a later point from some other part of the application. Setting the value tonull
clears the stored value.The fully qualified name of the type is used as the name when storing the value. The outcome of calling this method is thus the same as if calling
setAttribute(type.getName(), value);
- Parameters:
type
- the type that the stored value represents, can not be nullvalue
- the value to associate with the type, ornull
to remove a previous association.- See Also:
getAttribute(Class)
,setAttribute(String, Object)
-
getAttribute
public Object getAttribute(String name)
Gets a stored attribute value. If a value has been stored for the session, that value is returned. If no value is stored for the name,null
is returned.- Parameters:
name
- the name of the value to get, can not benull
.- Returns:
- the value, or
null
if no value has been stored or if it has been set to null. - See Also:
setAttribute(String, Object)
-
getAttribute
public <T> T getAttribute(Class<T> type)
Gets a stored attribute value. If a value has been stored for the session, that value is returned. If no value is stored for the name,null
is returned.The fully qualified name of the type is used as the name when getting the value. The outcome of calling this method is thus the same as if calling
getAttribute(type.getName());
- Parameters:
type
- the type of the value to get, can not benull
.- Returns:
- the value, or
null
if no value has been stored or if it has been set to null. - See Also:
setAttribute(Class, Object)
,getAttribute(String)
-
getNextUIid
public int getNextUIid()
Creates a new unique id for a UI.- Returns:
- a unique UI id
-
addUI
public void addUI(UI ui)
Adds an initialized UI to this session.- Parameters:
ui
- the initialized UI to add.
-
addUIProvider
public void addUIProvider(UIProvider uiProvider)
Adds a UI provider to this session. The UI provider is inserted as the first UI provider with priority 0. UI providers with higher priority will take precedence.- Parameters:
uiProvider
- the UI provider that should be added
-
addUIProvider
public void addUIProvider(UIProvider uiProvider, int priority)
Adds a UI provider to this session with a persistent priority value. The order of insertion is FILO; the new UI provider is inserted into the list before other UI providers with the same priority level.- Parameters:
uiProvider
- the UI provider that should be addedpriority
- the priority value. Higher priority value means that the UI provider gets put earlier in the list of providers. Default priority is 0.- Since:
- 7.7.41
-
removeUIProvider
public void removeUIProvider(UIProvider uiProvider)
Removes a UI provider association from this session.- Parameters:
uiProvider
- the UI provider that should be removed
-
getUIProviders
public List<UIProvider> getUIProviders()
Gets the UI providers configured for this session.- Returns:
- an unmodifiable list of UI providers
-
getUIProviderPriority
public int getUIProviderPriority(UIProvider provider)
Get the priority of a specific, already added UI provider. If the UI provider has not been added to this Session, returns Integer.MIN_VALUE.- Parameters:
provider
- a UIProvider instance- Returns:
- priority of the UI provider, 0 if the provider was added with no set priority or Integer.MIN_INT if the UI provider has not been added to this Session.
- Since:
- 7.7.41
-
setUIProviderPriority
public void setUIProviderPriority(UIProvider provider, int priority) throws NoSuchElementException
Set the priority of a specific, already added UI provider. If the UI provider has not been added to this Session, throws aNoSuchElementException
.Please note, that this will cause an immediate re-sorting of all added UIProvider instances. The sorting is guaranteed to be stable.
- Parameters:
provider
- a UIProvider instancepriority
- desired new priority- Throws:
NoSuchElementException
- if the UI provider is not a part of this Session.- Since:
- 7.7.41
-
getService
public VaadinService getService()
-
close
public void close()
Sets this session to be closed and all UI state to be discarded at the end of the current request, or at the end of the next request if there is no ongoing one.After the session has been discarded, any UIs that have been left open will give a Session Expired error and a new session will be created for serving new UIs.
To avoid causing out of sync errors, you should typically redirect to some other page using
Page.setLocation(String)
to make the browser unload the invalidated UI.
-
isClosing
@Deprecated public boolean isClosing()
Deprecated.As of 7.2, use
instead.getState() != State.OPEN
Returns whether this session is marked to be closed. Note that this method also returns true if the session is actually already closed.- Returns:
- true if this session is marked to be closed, false otherwise
- See Also:
close()
-
getState
public VaadinSession.State getState()
Returns the lifecycle state of this session.- Returns:
- the current state
- Since:
- 7.2
-
setState
protected void setState(VaadinSession.State state)
Sets the lifecycle state of this session. The allowed transitions are OPEN to CLOSING and CLOSING to CLOSED.- Parameters:
state
- the new state- Since:
- 7.2
-
accessSynchronously
public void accessSynchronously(Runnable runnable)
Locks this session and runs the provided Runnable right away.It is generally recommended to use
access(Runnable)
instead of this method for accessing a session from a different thread asaccess(Runnable)
can be used while holding the lock of another session. To avoid causing deadlocks, this methods throws an exception if it is detected than another session is also locked by the current thread.This method behaves differently than
access(Runnable)
in some situations:- If the current thread is currently holding the lock of this session,
accessSynchronously(Runnable)
runs the task right away whereasaccess(Runnable)
defers the task to a later point in time. - If some other thread is currently holding the lock for this session,
accessSynchronously(Runnable)
blocks while waiting for the lock to be available whereasaccess(Runnable)
defers the task to a later point in time.
- Parameters:
runnable
- the runnable which accesses the session- Throws:
IllegalStateException
- if the current thread holds the lock for another session- Since:
- 7.1
- See Also:
lock()
,getCurrent()
,access(Runnable)
,UI.accessSynchronously(Runnable)
- If the current thread is currently holding the lock of this session,
-
access
public Future<Void> access(Runnable runnable)
Provides exclusive access to this session from outside a request handling thread.The given runnable is executed while holding the session lock to ensure exclusive access to this session. If this session is not locked, the lock will be acquired and the runnable is run right away. If this session is currently locked, the runnable will be run before that lock is released.
RPC handlers for components inside this session do not need to use this method as the session is automatically locked by the framework during RPC handling.
Please note that the runnable might be invoked on a different thread or later on the current thread, which means that custom thread locals might not have the expected values when the runnable is executed. Inheritable values in
CurrentInstance
will have the same values as when this method was invoked.getCurrent()
andVaadinService.getCurrent()
are set according to this session before executing the runnable. Non-inheritable CurrentInstance values includingVaadinService.getCurrentRequest()
andVaadinService.getCurrentResponse()
will not be defined.The returned future can be used to check for task completion and to cancel the task. To help avoiding deadlocks,
Future.get()
throws an exception if it is detected that the current thread holds the lock for some other session.- Parameters:
runnable
- the runnable which accesses the session- Returns:
- a future that can be used to check for task completion and to cancel the task
- Since:
- 7.1
- See Also:
lock()
,getCurrent()
,accessSynchronously(Runnable)
,UI.access(Runnable)
-
getPendingAccessQueue
public Queue<VaadinSession.FutureAccess> getPendingAccessQueue()
Gets the queue of tasks submitted usingaccess(Runnable)
. It is safe to call this method and access the returned queue without holding thesession lock
.- Returns:
- the queue of pending access tasks
- Since:
- 7.1
-
getCsrfToken
public String getCsrfToken()
Gets the CSRF token (aka double submit cookie) that is used to protect against Cross Site Request Forgery attacks.- Returns:
- the csrf token string
- Since:
- 7.1
-
getPushId
public String getPushId()
Gets the push connection identifier for this session. Used when establishing a push connection with the client.- Returns:
- the push connection identifier string
- Since:
- 7.7.11
-
getUIByEmbedId
public UI getUIByEmbedId(String embedId)
Finds the UI with the corresponding embed id.- Parameters:
embedId
- the embed id- Returns:
- the UI with the corresponding embed id, or
null
if no UI is found - Since:
- 7.2
- See Also:
UI.getEmbedId()
-
refreshTransients
public void refreshTransients(WrappedSession wrappedSession, VaadinService vaadinService)
Refreshes the transient fields of the session to ensure they are up to date.Called internally by the framework.
- Parameters:
wrappedSession
- the session this VaadinSession is stored invaadinService
- the service associated with this VaadinSession- Since:
- 7.6
-
-