com.vaadin.server
Class VaadinSession

java.lang.Object
  extended by com.vaadin.server.VaadinSession
All Implemented Interfaces:
java.io.Serializable, java.util.EventListener, javax.servlet.http.HttpSessionBindingListener
Direct Known Subclasses:
VaadinPortletSession, VaadinServletSession

public class VaadinSession
extends java.lang.Object
implements javax.servlet.http.HttpSessionBindingListener, java.io.Serializable

Contains everything that Vaadin needs to store for a specific user. This is typically stored in a HttpSession or PortletSession, 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
 class VaadinSession.ApplicationError
          Application error is an error message defined on the application level.
static class VaadinSession.SessionStartEvent
          Deprecated. might be refactored or removed before 7.0.0
static class VaadinSession.WindowAttachEvent
          Deprecated. might be refactored or removed before 7.0.0
static interface VaadinSession.WindowAttachListener
          Window attach listener interface.
static class VaadinSession.WindowDetachEvent
          Deprecated. might be refactored or removed before 7.0.0
static interface VaadinSession.WindowDetachListener
          Window detach listener interface.
 
Field Summary
protected  WebBrowser browser
           
static java.lang.String UI_PARAMETER
          The name of the parameter that is by default used in e.g.
 
Constructor Summary
VaadinSession()
           
 
Method Summary
 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 addUIProvider(UIProvider uIProvider)
           
 void close()
          Deprecated. might be refactored or removed before 7.0.0
 void closeInactiveUIs()
          Deprecated. might be refactored or removed before 7.0.0
 java.lang.String createConnectorId(ClientConnector connector)
          Deprecated. might be refactored or removed before 7.0.0
 UI createUI(WrappedRequest request)
          Deprecated. might be refactored or removed before 7.0.0
protected
<T extends UI>
T
createUIInstance(WrappedRequest request, java.lang.Class<T> uiClass)
          Deprecated. might be refactored or removed before 7.0.0
 AbstractCommunicationManager getApplicationManager()
          Deprecated. might be refactored or removed before 7.0.0
<T> T
getAttribute(java.lang.Class<T> type)
          Gets a stored attribute value.
 java.lang.Object getAttribute(java.lang.String name)
          Gets a stored attribute value.
 WebBrowser getBrowser()
          Deprecated. might be refactored or removed before 7.0.0
 DeploymentConfiguration getConfiguration()
          Gets the configuration for this session
 ConverterFactory getConverterFactory()
          Gets the ConverterFactory used to locate a suitable Converter for fields in the session.
static VaadinSession getCurrent()
          Gets the currently used session.
 Terminal.ErrorListener getErrorHandler()
          Gets the session's error handler.
static VaadinSession getForSession(WrappedSession underlyingSession)
          Deprecated. might be refactored or removed before 7.0.0
 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.
protected  int getHeartbeatTimeout()
          Returns the number of seconds that must pass without a valid heartbeat or UIDL request being received from a UI before that UI is removed from the application.
 long getLastRequestTime()
           
 java.util.Locale getLocale()
          Gets the default locale for this session.
 java.util.concurrent.locks.Lock getLock()
          Gets the lock that should be used to synchronize usage of data inside this session.
 java.lang.String getLogoutURL()
          Deprecated. might be refactored or removed before 7.0.0
 java.util.Collection<RequestHandler> getRequestHandlers()
          Gets the request handlers that are registered to the session.
 WrappedSession getSession()
          Gets the underlying session to which this vaadin session is currently associated.
 long getTotalSessionTime()
           
 UI getUIById(int uiId)
          Returns a UI with the given id.
 java.lang.Class<? extends UI> getUIClass(WrappedRequest request)
          Deprecated. might be refactored or removed before 7.0.0
protected  int getUidlRequestTimeout()
          Returns the number of seconds that must pass without a valid UIDL request being received from a UI before the UI is removed from the session, even though heartbeat requests are received.
 UI getUIForRequest(WrappedRequest request)
          Deprecated. might be refactored or removed before 7.0.0
 UIProvider getUiProvider(WrappedRequest request, java.lang.Class<?> uiClass)
          Deprecated. might be refactored or removed before 7.0.0
 java.util.Collection<UIProvider> getUIProviders()
           
 java.util.Collection<UI> getUIs()
          Gets all the UIs of this session.
 java.net.URL getURL()
          Deprecated. might be refactored or removed before 7.0.0
 boolean isRunning()
          Deprecated. might be refactored or removed before 7.0.0
protected  boolean isUIAlive(UI ui)
          Deprecated. Might be refactored or removed before 7.0.0
 void modifyBootstrapResponse(BootstrapResponse response)
          Deprecated. might be refactored or removed before 7.0.0
 void removeBootstrapListener(BootstrapListener listener)
          Remove a bootstrap listener that was previously added.
 void removeFromSession()
          Deprecated. might be refactored or removed before 7.0.0
 void removeRequestHandler(RequestHandler handler)
          Removes a request handler from the session.
 void removeUIProvider(UIProvider uIProvider)
           
<T> void
setAttribute(java.lang.Class<T> type, T value)
          Stores a value in this vaadin session.
 void setAttribute(java.lang.String name, java.lang.Object value)
          Stores a value in this vaadin session.
 void setConverterFactory(ConverterFactory converterFactory)
          Sets the ConverterFactory used to locate a suitable Converter for fields in the session.
static void setCurrent(VaadinSession session)
          Sets the thread local for the current session.
 void setErrorHandler(Terminal.ErrorListener errorHandler)
          Sets the session error handler.
 void setLastRequestTime(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 setLocale(java.util.Locale locale)
          Sets the default locale for this session.
 void setLogoutURL(java.lang.String logoutURL)
          Deprecated. might be refactored or removed before 7.0.0
 void start(VaadinSession.SessionStartEvent event)
          Deprecated. might be refactored or removed before 7.0.0
 void storeInSession(WrappedSession session)
          Deprecated. might be refactored or removed before 7.0.0
 void valueBound(javax.servlet.http.HttpSessionBindingEvent arg0)
           
 void valueUnbound(javax.servlet.http.HttpSessionBindingEvent event)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UI_PARAMETER

public static final java.lang.String UI_PARAMETER
The name of the parameter that is by default used in e.g. web.xml to define the name of the default UI class.

See Also:
Constant Field Values

browser

protected WebBrowser browser
Constructor Detail

VaadinSession

public VaadinSession()
Method Detail

valueBound

public void valueBound(javax.servlet.http.HttpSessionBindingEvent arg0)
Specified by:
valueBound in interface javax.servlet.http.HttpSessionBindingListener
See Also:
HttpSessionBindingListener.valueBound(HttpSessionBindingEvent)

valueUnbound

public void valueUnbound(javax.servlet.http.HttpSessionBindingEvent event)
Specified by:
valueUnbound in interface javax.servlet.http.HttpSessionBindingListener
See Also:
HttpSessionBindingListener.valueUnbound(HttpSessionBindingEvent)

getBrowser

@Deprecated
public WebBrowser getBrowser()
Deprecated. might be refactored or removed before 7.0.0

Get the web browser associated with this session.

Returns:

getTotalSessionTime

public long getTotalSessionTime()
Returns:
The total time spent servicing requests in this session.

setLastRequestTime

public void setLastRequestTime(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.

getLastRequestTime

public long getLastRequestTime()
Returns:
the time spent servicing the last request in this session.

getSession

public WrappedSession getSession()
Gets the underlying session to which this vaadin session is currently associated.

Returns:
the wrapped session for this context

getApplicationManager

@Deprecated
public AbstractCommunicationManager getApplicationManager()
Deprecated. might be refactored or removed before 7.0.0

Returns:

getURL

@Deprecated
public java.net.URL getURL()
Deprecated. might be refactored or removed before 7.0.0

Gets the URL of the application.

This is the URL what can be entered to a browser window to start the application. Navigating to the application URL shows the main window ( #getMainWindow()) of the application. Note that the main window can also be shown by navigating to the window url ( com.vaadin.ui.Window#getURL()).

Returns:
the application's URL.

close

@Deprecated
public void close()
Deprecated. might be refactored or removed before 7.0.0

Ends the session.

When the session is closed, close events are fired for its UIs, its state is removed from the underlying session, and the browser window is redirected to the application logout url set with setLogoutURL(String). If the logout url has not been set, the browser window is reloaded and the application is restarted.


getForSession

@Deprecated
public static VaadinSession getForSession(WrappedSession underlyingSession)
Deprecated. might be refactored or removed before 7.0.0

Parameters:
underlyingSession -
Returns:

removeFromSession

@Deprecated
public void removeFromSession()
Deprecated. might be refactored or removed before 7.0.0


storeInSession

@Deprecated
public void storeInSession(WrappedSession session)
Deprecated. might be refactored or removed before 7.0.0

Parameters:
session -

start

@Deprecated
public void start(VaadinSession.SessionStartEvent event)
Deprecated. might be refactored or removed before 7.0.0

Starts the application on the given URL.

This method is called by Vaadin framework when a user navigates to the application. After this call the application corresponds to the given URL and it will return windows when asked for them. There is no need to call this method directly.

Application properties are defined by servlet configuration object ServletConfig and they are overridden by context-wide initialization parameters ServletContext.

Parameters:
event - the application start event containing details required for starting the application.

isRunning

@Deprecated
public boolean isRunning()
Deprecated. might be refactored or removed before 7.0.0

Tests if the application is running or if it has been finished.

Application starts running when its start(SessionStartEvent) method has been called and stops when the close() is called.

Returns:
true if the application is running, false if not.

getConfiguration

public DeploymentConfiguration getConfiguration()
Gets the configuration for this session

Returns:
the deployment configuration

getLocale

public java.util.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(java.util.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.

getLogoutURL

@Deprecated
public java.lang.String getLogoutURL()
Deprecated. might be refactored or removed before 7.0.0

Returns the URL user is redirected to on application close. If the URL is null, the application is closed normally as defined by the application running environment.

Desktop application just closes the application window and web-application redirects the browser to application main URL.

Returns:
the URL.

setLogoutURL

@Deprecated
public void setLogoutURL(java.lang.String logoutURL)
Deprecated. might be refactored or removed before 7.0.0

Sets the URL user is redirected to on application close. If the URL is null, the application is closed normally as defined by the application running environment: Desktop application just closes the application window and web-application redirects the browser to application main URL.

Parameters:
logoutURL - the logoutURL to set.

getErrorHandler

public Terminal.ErrorListener getErrorHandler()
Gets the session's error handler.

Returns:
the current error handler

setErrorHandler

public void setErrorHandler(Terminal.ErrorListener errorHandler)
Sets the session error handler.

Parameters:
errorHandler -

getConverterFactory

public ConverterFactory getConverterFactory()
Gets the ConverterFactory used to locate a suitable Converter for fields in the session. See setConverterFactory(ConverterFactory) for more details

Returns:
The converter factory used in the session

setConverterFactory

public void setConverterFactory(ConverterFactory converterFactory)
Sets the 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).

The Converter for an individual field can be overridden using AbstractField.setConverter(Converter) and for individual property ids in a Table using Table.setConverter(Object, Converter).

The converter factory must never be set to null.

Parameters:
converterFactory - The converter factory used in the session

getUIClass

@Deprecated
public java.lang.Class<? extends UI> getUIClass(WrappedRequest request)
Deprecated. might be refactored or removed before 7.0.0

Gets the UI class for a request for which no UI is already known. This method is called when the framework processes a request that does not originate from an existing UI instance. This typically happens when a host page is requested.

Parameters:
request - the wrapped request for which a UI is needed
Returns:
a UI instance to use for the request
Since:
7.0
See Also:
UI, WrappedRequest.getBrowserDetails()

createUIInstance

@Deprecated
protected <T extends UI> T createUIInstance(WrappedRequest request,
                                                       java.lang.Class<T> uiClass)
Deprecated. might be refactored or removed before 7.0.0

Creates an UI instance for a request for which no UI is already known. This method is called when the framework processes a request that does not originate from an existing UI instance. This typically happens when a host page is requested.

Parameters:
request -
uiClass -
Returns:

getUiProvider

@Deprecated
public UIProvider getUiProvider(WrappedRequest request,
                                           java.lang.Class<?> uiClass)
Deprecated. might be refactored or removed before 7.0.0

Gets the UIProvider that should be used for a request. The selection can further be restricted by also requiring the UI provider to support a specific UI class.

Parameters:
request - the request for which to get an UI provider
uiClass - the UI class for which a provider is required, or null to use the first UI provider supporting the request.
Returns:
an UI provider supporting the request (and the UI class if provided).
Since:
7.0.0
See Also:
UIProvider, addUIProvider(UIProvider)

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 java.util.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 (see InheritableThreadLocal). In other cases, (e.g. from background threads started in some other way), the current session is not automatically defined.

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.

Parameters:
session -
Since:
7.0
See Also:
getCurrent(), ThreadLocal

addUIProvider

public void addUIProvider(UIProvider uIProvider)

removeUIProvider

public void removeUIProvider(UIProvider uIProvider)

getUIForRequest

@Deprecated
public UI getUIForRequest(WrappedRequest request)
Deprecated. might be refactored or removed before 7.0.0

Finds the UI to which a particular request belongs. If the request originates from an existing UI, that UI is returned. In other cases, the method attempts to create and initialize a new UI and might throw a UIRequiresMoreInformationException if all required information is not available.

Please note that this method can also return a newly created UI which has not yet been initialized. You can use #isUIInitPending(int) with the UI's id ( UI.getUIId() to check whether the initialization is still pending.

Parameters:
request - the request for which a UI is desired
Returns:
a UI belonging to the request
Since:
7.0
See Also:
createUI(WrappedRequest)

createUI

@Deprecated
public UI createUI(WrappedRequest request)
Deprecated. might be refactored or removed before 7.0.0

Parameters:
request -
Returns:

getUIs

public java.util.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 java.lang.String createConnectorId(ClientConnector connector)
Deprecated. might be refactored or removed before 7.0.0

Generate 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

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. might be refactored or removed before 7.0.0

Fires a bootstrap event to all registered listeners. There are currently two supported events, both inheriting from BootstrapResponse: BootstrapFragmentResponse and BootstrapPageResponse.

Parameters:
response - the bootstrap response event for which listeners should be fired

closeInactiveUIs

@Deprecated
public void closeInactiveUIs()
Deprecated. might be refactored or removed before 7.0.0

Removes all those UIs from the session for which isUIAlive(com.vaadin.ui.UI) returns false. Close events are fired for the removed UIs.

Called by the framework at the end of every request.

Since:
7.0.0
See Also:
UI.CloseEvent, UI.CloseListener, isUIAlive(UI)

getHeartbeatTimeout

protected int getHeartbeatTimeout()
Returns the number of seconds that must pass without a valid heartbeat or UIDL request being received from a UI before that UI is removed from the application. This is a lower bound; it might take longer to close an inactive UI. Returns a negative number if heartbeat is disabled and timeout never occurs.

Returns:
The heartbeat timeout in seconds or a negative number if timeout never occurs.
Since:
7.0.0
See Also:
getUidlRequestTimeout(), closeInactiveUIs(), DeploymentConfiguration.getHeartbeatInterval()

getUidlRequestTimeout

protected int getUidlRequestTimeout()
Returns the number of seconds that must pass without a valid UIDL request being received from a UI before the UI is removed from the session, even though heartbeat requests are received. This is a lower bound; it might take longer to close an inactive UI. Returns a negative number if

This timeout only has effect if cleanup of inactive UIs is enabled; otherwise heartbeat requests are enough to extend UI lifetime indefinitely.

Returns:
The UIDL request timeout in seconds, or a negative number if timeout never occurs.
Since:
7.0.0
See Also:
DeploymentConfiguration.isIdleUICleanupEnabled(), getHeartbeatTimeout(), closeInactiveUIs()

isUIAlive

@Deprecated
protected boolean isUIAlive(UI ui)
Deprecated. Might be refactored or removed before 7.0.0

Returns whether the given UI is alive (the client-side actively communicates with the server) or whether it can be removed from the session and eventually collected.

Parameters:
ui - The UI whose status to check
Returns:
true if the UI is alive, false if it could be removed.
Since:
7.0.0

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 if null 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 is false.
Since:
7.0.0

getUIProviders

public java.util.Collection<UIProvider> getUIProviders()

getLock

public java.util.concurrent.locks.Lock getLock()
Gets the lock that should be used to synchronize usage of data inside this session.

Returns:
the lock that should be used for synchronization

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.Object value)
Stores a value in this vaadin 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 to null clears the stored value.

Parameters:
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.
See Also:
getAttribute(String)

setAttribute

public <T> void setAttribute(java.lang.Class<T> type,
                             T value)
Stores a value in this vaadin 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 to 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);

Parameters:
type - the type that the stored value represents, can not be null
value - the value to associate with the type, or null to remove a previous association.
See Also:
getAttribute(Class), setAttribute(String, Object)

getAttribute

public java.lang.Object getAttribute(java.lang.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 be null.
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(java.lang.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 be null.
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)


Copyright © 2000-2011 Vaadin Ltd. All Rights Reserved.