public class VaadinSession extends Object implements javax.servlet.http.HttpSessionBindingListener, Serializable
Everything inside a VaadinSession
should be serializable to ensure
compatibility with schemes using serialization for persisting the session
data.
Modifier and Type | Class and Description |
---|---|
static class |
VaadinSession.FutureAccess
Encapsulates a
Runnable submitted using
access(Runnable) . |
static class |
VaadinSession.State
The lifecycle state of a VaadinSession.
|
Modifier and Type | Field and Description |
---|---|
protected WebBrowser |
browser |
static String |
UI_PARAMETER
The name of the parameter that is by default used in e.g.
|
Constructor and Description |
---|
VaadinSession(VaadinService service)
Creates a new VaadinSession tied to a VaadinService.
|
Modifier and Type | Method and 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.
|
Registration |
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 |
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. Use
VaadinService.generateConnectorId(VaadinSession, ClientConnector)
instead. |
static Collection<VaadinSession> |
getAllSessions(javax.servlet.http.HttpSession httpSession)
Retrieves all
VaadinSession 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, use
Page.getWebBrowser() instead. |
LegacyCommunicationManager |
getCommunicationManager()
Deprecated.
As of 7.0. Will likely change or be removed in a future
version
|
DeploymentConfiguration |
getConfiguration()
Gets the configuration for this session.
|
Object |
getConverterFactory()
Deprecated.
|
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, call
VaadinService.loadSession(WrappedSession) instead |
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.
|
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 the
Lock instance that is used for protecting the data of
this session from concurrent access. |
String |
getNextConnectorId()
Gets the next unused numerical id for connector ids.
|
int |
getNextUIid()
Creates a new unique id for a UI.
|
Queue<VaadinSession.FutureAccess> |
getPendingAccessQueue()
Gets the queue of tasks submitted using
access(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.
|
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 VaadinSession.
|
protected 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. |
void |
lock()
Locks this session to protect its data from concurrent access.
|
void |
modifyBootstrapResponse(BootstrapResponse response)
Deprecated.
As of 7.0. Will likely change or be removed in a future
version
|
void |
refreshTransients(WrappedSession wrappedSession,
VaadinService vaadinService)
Refreshes the transient fields of the session to ensure they are up to
date.
|
void |
removeBootstrapListener(BootstrapListener listener)
Deprecated.
Use a
Registration object returned by
addBootstrapListener(BootstrapListener) to remove a
listener |
void |
removeFromSession(VaadinService service)
Deprecated.
as of 7.6, call
VaadinService.removeSession(WrappedSession) instead |
void |
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(Object converterFactory)
Deprecated.
|
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 |
storeInSession(VaadinService service,
WrappedSession session)
Deprecated.
as of 7.6, call
VaadinService.storeSession(VaadinSession, WrappedSession)
instead |
void |
unlock()
Unlocks this session.
|
void |
valueBound(javax.servlet.http.HttpSessionBindingEvent arg0) |
void |
valueUnbound(javax.servlet.http.HttpSessionBindingEvent event) |
public static final String UI_PARAMETER
UI
class.protected WebBrowser browser
public VaadinSession(VaadinService service)
service
- the Vaadin service for the new sessionpublic void valueBound(javax.servlet.http.HttpSessionBindingEvent arg0)
valueBound
in interface javax.servlet.http.HttpSessionBindingListener
HttpSessionBindingListener.valueBound(HttpSessionBindingEvent)
public void valueUnbound(javax.servlet.http.HttpSessionBindingEvent event)
valueUnbound
in interface javax.servlet.http.HttpSessionBindingListener
HttpSessionBindingListener.valueUnbound(HttpSessionBindingEvent)
@Deprecated public WebBrowser getBrowser()
Page.getWebBrowser()
instead.public long getCumulativeRequestDuration()
public void setLastRequestDuration(long time)
time
- The time spent in the last request, in milliseconds.public long getLastRequestDuration()
public void setLastRequestTimestamp(long timestamp)
timestamp
- The time when the last request was handled, in milliseconds
since the epoch.public long getLastRequestTimestamp()
public WrappedSession getSession()
@Deprecated public LegacyCommunicationManager getCommunicationManager()
public DragAndDropService getDragAndDropService()
@Deprecated public static VaadinSession getForSession(VaadinService service, WrappedSession underlyingSession)
VaadinService.loadSession(WrappedSession)
insteadservice
- The service the VaadinSession is associated withunderlyingSession
- The wrapped HTTP session for the userpublic static Collection<VaadinSession> getAllSessions(javax.servlet.http.HttpSession httpSession)
VaadinSession
s which are stored in the given HTTP
session.httpSession
- the HTTP session@Deprecated public void removeFromSession(VaadinService service)
VaadinService.removeSession(WrappedSession)
insteadservice
- The service this session is associated with@Deprecated public void storeInSession(VaadinService service, WrappedSession session)
VaadinService.storeSession(VaadinSession, WrappedSession)
insteadservice
- The service this session is associated withsession
- The HTTP session this VaadinSession should be stored inpublic void setCommunicationManager(LegacyCommunicationManager communicationManager)
public void setConfiguration(DeploymentConfiguration configuration)
public DeploymentConfiguration getConfiguration()
public Locale getLocale()
public void setLocale(Locale locale)
locale
- the Locale object.public ErrorHandler getErrorHandler()
public void setErrorHandler(ErrorHandler errorHandler)
errorHandler
- @Deprecated public Object getConverterFactory()
ConverterFactory
used to locate a suitable
Converter
for fields in the session.
Note that the this and #setConverterFactory(Object))
use Object
and not ConverterFactory
in Vaadin 8 to avoid a core dependency
on the compatibility packages.
@Deprecated public void setConverterFactory(Object converterFactory)
ConverterFactory
used to locate a suitable
Converter
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).
Note that the this and #getConverterFactory()
use Object and not
ConverterFactory
in Vaadin 8 to avoid a core dependency on the
compatibility packages.
The converter factory must never be set to null.
converterFactory
- The converter factory used in the sessionpublic void addRequestHandler(RequestHandler handler)
Handlers are called in reverse order of addition, so the most recently added handler will be called first.
handler
- the request handler to addremoveRequestHandler(RequestHandler)
public void removeRequestHandler(RequestHandler handler)
handler
- the request handler to removepublic Collection<RequestHandler> getRequestHandlers()
addRequestHandler(RequestHandler)
,
removeRequestHandler(RequestHandler)
public static VaadinSession getCurrent()
ThreadLocal
) and in access(Runnable)
and
UI.access(Runnable)
. In other cases, (e.g. from background
threads, 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.
null
setCurrent(VaadinSession)
public static void setCurrent(VaadinSession session)
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.
session
- the session to set as currentgetCurrent()
,
ThreadLocal
public Collection<UI> getUIs()
@Deprecated public String createConnectorId(ClientConnector connector)
VaadinService.generateConnectorId(VaadinSession, ClientConnector)
instead.connector
- A connector that has not yet been assigned an id.public String getNextConnectorId()
null
public UI getUIById(int uiId)
This is meant for framework internal use.
uiId
- The UI idpublic boolean hasLock()
protected static boolean hasLock(VaadinService service, WrappedSession session)
public Registration addBootstrapListener(BootstrapListener listener)
listener
- the bootstrap listener to addBootstrapListener.modifyBootstrapFragment(BootstrapFragmentResponse)
,
BootstrapListener.modifyBootstrapPage(BootstrapPageResponse)
@Deprecated public void removeBootstrapListener(BootstrapListener listener)
Registration
object returned by
addBootstrapListener(BootstrapListener)
to remove a
listenerlistener
- the bootstrap listener to removeaddBootstrapListener(BootstrapListener)
@Deprecated public void modifyBootstrapResponse(BootstrapResponse response)
BootstrapResponse
:
BootstrapFragmentResponse
and BootstrapPageResponse
.response
- the bootstrap response event for which listeners should be
firedpublic void removeUI(UI ui)
ui
- the UI to removepublic GlobalResourceHandler getGlobalResourceHandler(boolean createOnDemand)
createOnDemand
- true
if a resource handler should be initialized
if there is no handler associated with this application,
false
if null
should be returned if
there is no registered handler.null
if
there is no handler and the createOnDemand parameter is
false
.public Lock getLockInstance()
Lock
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 using lock()
and unlock()
. The returned
instance is not guaranteed to support any other features of the
Lock
interface than Lock.lock()
and
Lock.unlock()
.
public void lock()
UI.access(Runnable)
(or access(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.
unlock()
,
getLockInstance()
,
hasLock()
public void unlock()
public void setAttribute(String name, Object value)
null
clears the stored value.name
- the name to associate the value with, can not be
null
value
- the value to associate with the name, or null
to
remove a previous association.getAttribute(String)
public <T> void setAttribute(Class<T> type, T value)
null
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);
type
- the type that the stored value represents, can not be nullvalue
- the value to associate with the type, or null
to
remove a previous association.getAttribute(Class)
,
setAttribute(String, Object)
public Object getAttribute(String name)
null
is returned.name
- the name of the value to get, can not be null
.null
if no value has been stored or if
it has been set to null.setAttribute(String, Object)
public <T> T getAttribute(Class<T> type)
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());
type
- the type of the value to get, can not be null
.null
if no value has been stored or if
it has been set to null.setAttribute(Class, Object)
,
getAttribute(String)
public int getNextUIid()
public void addUI(UI ui)
ui
- the initialized UI to add.public void addUIProvider(UIProvider uiProvider)
uiProvider
- the UI provider that should be addedpublic void removeUIProvider(UIProvider uiProvider)
uiProvider
- the UI provider that should be removedpublic List<UIProvider> getUIProviders()
public VaadinService getService()
public void close()
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.
@Deprecated public boolean isClosing()
getState() != State.OPEN
instead.close()
public VaadinSession.State getState()
protected void setState(VaadinSession.State state)
state
- the new statepublic void accessSynchronously(Runnable runnable)
It is generally recommended to use access(Runnable)
instead of
this method for accessing a session from a different thread as
access(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:
accessSynchronously(Runnable)
runs the task right away whereas
access(Runnable)
defers the task to a later point in time.accessSynchronously(Runnable)
blocks while waiting for the lock
to be available whereas access(Runnable)
defers the task to a
later point in time.runnable
- the runnable which accesses the sessionIllegalStateException
- if the current thread holds the lock for another sessionlock()
,
getCurrent()
,
access(Runnable)
,
UI.accessSynchronously(Runnable)
public Future<Void> access(Runnable runnable)
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 command is executed.
getCurrent()
and VaadinService.getCurrent()
are set according to this session before executing the command. Other
standard CurrentInstance values such as
VaadinService.getCurrentRequest()
and
VaadinService.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.
runnable
- the runnable which accesses the sessionlock()
,
getCurrent()
,
accessSynchronously(Runnable)
,
UI.access(Runnable)
public Queue<VaadinSession.FutureAccess> getPendingAccessQueue()
access(Runnable)
. It is
safe to call this method and access the returned queue without holding
the session lock
.public String getCsrfToken()
public String getPushId()
public UI getUIByEmbedId(String embedId)
embedId
- the embed idnull
if
no UI is foundUI.getEmbedId()
public void refreshTransients(WrappedSession wrappedSession, VaadinService vaadinService)
Called internally by the framework.
wrappedSession
- the session this VaadinSession is stored invaadinService
- the service associated with this VaadinSessionCopyright © 2018 Vaadin Ltd. All rights reserved.