Class VaadinApplication
- java.lang.Object
-
- org.dellroad.stuff.vaadin7.VaadinApplication
-
- All Implemented Interfaces:
Serializable
public class VaadinApplication extends Object implements Serializable
A globally accessible "Vaadin application" singleton.At most one instance is associated with each
VaadinSession(attempting to create more than one will result in an exception). This singleton instance is then always accessible from any Vaadin thread viaget().Although this class does not require Spring, the singleton instance can be declared as a bean in the Spring XML application context created by a
SpringVaadinSessionListener, which allows other beans and widgets in the Vaadin application context to autowire it and have access to the methods provided here:
If your<bean class="org.dellroad.stuff.vaadin7.VaadinApplication"/>VaadinApplicationinstance allocates any resources on startup, be sure to configure it so that it cleans them up on application context shutdown, for example, by specifyingdestroy-method="...".If you are in a clustered environment and are subclassing this class,
scope="session"is recommended; without it, there will be one instance per server+session rather than one unique instance per session. Of course, whether that matters depends on the semantics of the subclass fields. In any case, be aware that no instance will exist in session until the application context is refreshed.Note: using
scope="session"requires adding a<listener>clause registering Spring'sRequestContextListenerin yourweb.xml.If this class is subclassed, additional application-specific fields and methods can be supplied to the entire application via the same mechanism. Subclasses can override
init()for any application-specific initialization.Note: the singleton instance of this class is stored in the Vaadin session and is
Serializable. Therefore, in a clustered environment, subclasses must be prepared to handle (de)serialization. Also, avoid declaring fields that reference this class in non-session-scope beans; instead, useVaadinApplication.get().
-
-
Constructor Summary
Constructors Constructor Description VaadinApplication()Convenience constructor.VaadinApplication(VaadinSession session)Primary Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddSessionDestroyListener(SessionDestroyListener listener)Register for a notification when theVaadinSessionis closed, without creating a memory leak.voidclose()Close theVaadinSessionassociated with this instance.static VaadinApplicationget()Get the singletonVaadinApplicationinstance associated with the currentVaadinSession.static <T extends VaadinApplication>
Tget(VaadinSession session, Class<T> clazz)Get the singleton instance of the specified class associated with the given session.static <T extends VaadinApplication>
Tget(Class<T> clazz)Get the singleton instance of the specified class associated with the currentVaadinSession.VaadinSessiongetSession()Get theVaadinSessionassociated with this instance.protected voidinit()Perform any further initialization at construction time.voidinvoke(Runnable action)Peform some action while holding the lock of theVaadinSessionassociated with this instance.Future<Void>invokeLater(Runnable action)Peform some action asynchronously while holding the lock of theVaadinSessionassociated with this instance.voidremoveSessionDestroyListener(SessionDestroyListener listener)Remove a listener added viaaddSessionDestroyListener().
-
-
-
Field Detail
-
log
protected transient Logger log
-
-
Constructor Detail
-
VaadinApplication
public VaadinApplication()
Convenience constructor. Equivalent to:VaadinApplication(VaadinUtil.getCurrentSession())- Throws:
IllegalStateException- if there is noVaadinSessionassociated with the current threadIllegalStateException- if there is already aVaadinApplicationinstance associated with the current session
-
VaadinApplication
public VaadinApplication(VaadinSession session)
Primary Constructor.- Parameters:
session- the session with which this instance should be associated- Throws:
IllegalArgumentException- ifsessionis nullIllegalStateException- if there is already aVaadinApplicationinstance associated withsession
-
-
Method Detail
-
init
protected void init()
Perform any further initialization at construction time.The implementation in
VaadinApplicationdoes nothing. Subclasses may override as desired.
-
getSession
public VaadinSession getSession()
Get theVaadinSessionassociated with this instance.- Returns:
- associated VaadinSession, never null
-
close
public void close()
Close theVaadinSessionassociated with this instance. After invoking this method, the caller would normally ensure that no further references to this instance remain so that it and the associatedVaadinSessioncan be freed.The implementation in
VaadinApplicationjust delegates toVaadinSession.close().
-
get
public static VaadinApplication get()
Get the singletonVaadinApplicationinstance associated with the currentVaadinSession.This is a convenience method, equivalent to:
VaadinApplication.get(VaadinApplication.class)- Returns:
- singleton instance for the current Vaadin application, never null
- Throws:
IllegalStateException- if there is noVaadinSessionassociated with the current threadIllegalStateException- if there is noVaadinApplicationinstance associated with the current session
-
get
public static <T extends VaadinApplication> T get(Class<T> clazz)
Get the singleton instance of the specified class associated with the currentVaadinSession.This is a convenience method, equivalent to:
VaadinApplication.get(VaadinUtil.getCurrentSession(), clazz)Useful for subclasses of
VaadinApplicationthat want to provide their own zero-parameterget()methods.- Type Parameters:
T- application type- Parameters:
clazz- singleton instance type- Returns:
- singleton instance of
clazzin the session, never null - Throws:
IllegalArgumentException- ifclazzis nullIllegalStateException- if there is noVaadinSessionassociated with the current threadIllegalStateException- if there is no singleton of typeclazzassociated with the current session
-
get
public static <T extends VaadinApplication> T get(VaadinSession session, Class<T> clazz)
Get the singleton instance of the specified class associated with the given session.- Type Parameters:
T- application type- Parameters:
session- Vaadin sessionclazz- singleton instance type- Returns:
- singleton instance of
clazzin the session, never null - Throws:
IllegalArgumentException- ifsessionis nullIllegalArgumentException- ifclazzis nullIllegalStateException- if there is no singleton of typeclazzassociated with thesession
-
invoke
public void invoke(Runnable action)
Peform some action while holding the lock of theVaadinSessionassociated with this instance.This is a convenience method that in turn invokes
VaadinUtil.invoke()using theVaadinSessionassociated with this instance.- Parameters:
action- action to perform- Throws:
IllegalArgumentException- ifactionis null- See Also:
VaadinUtil.invoke(com.vaadin.server.VaadinSession, java.lang.Runnable)
-
invokeLater
public Future<Void> invokeLater(Runnable action)
Peform some action asynchronously while holding the lock of theVaadinSessionassociated with this instance.This is a convenience method that in turn invokes
VaadinUtil.invokeLater()using theVaadinSessionassociated with this instance.- Parameters:
action- action to perform- Returns:
- future for the action
- Throws:
IllegalArgumentException- ifactionis null- See Also:
VaadinUtil.invokeLater(com.vaadin.server.VaadinSession, java.lang.Runnable)
-
addSessionDestroyListener
public void addSessionDestroyListener(SessionDestroyListener listener)
Register for a notification when theVaadinSessionis closed, without creating a memory leak.This is a convenience method that in turn invokes
VaadinUtil.addSessionDestroyListener()using theVaadinSessionassociated with this instance.- Parameters:
listener- listener for notifications- Throws:
IllegalArgumentException- iflisteneris null- See Also:
VaadinUtil.addSessionDestroyListener(com.vaadin.server.VaadinSession, com.vaadin.server.SessionDestroyListener)
-
removeSessionDestroyListener
public void removeSessionDestroyListener(SessionDestroyListener listener)
Remove a listener added viaaddSessionDestroyListener().This is a convenience method that in turn invokes
VaadinUtil.removeSessionDestroyListener()using theVaadinSessionassociated with this instance.- Parameters:
listener- listener for notifications- Throws:
IllegalArgumentException- iflisteneris null- See Also:
VaadinUtil.removeSessionDestroyListener(com.vaadin.server.VaadinSession, com.vaadin.server.SessionDestroyListener)
-
-