org.dellroad.stuff.vaadin
Class ContextApplication

java.lang.Object
  extended by com.vaadin.Application
      extended by org.dellroad.stuff.vaadin.ContextApplication
All Implemented Interfaces:
HttpServletRequestListener, Terminal.ErrorListener, URIHandler, Serializable
Direct Known Subclasses:
SpringContextApplication

public abstract class ContextApplication
extends Application
implements HttpServletRequestListener

Application subclass that provides some basic infrastructure for Vaadin applications:

Since:
1.0.134
See Also:
Serialized Form

Nested Class Summary
static class ContextApplication.CloseEvent
          Event delivered to listeners when a ContextApplication is closed.
static interface ContextApplication.CloseListener
          Implemented by listeners that wish to be notified when a ContextApplication is closed.
 
Nested classes/interfaces inherited from class com.vaadin.Application
Application.ApplicationError, Application.CustomizedSystemMessages, Application.SystemMessages, Application.UserChangeEvent, Application.UserChangeListener, Application.WindowAttachEvent, Application.WindowAttachListener, Application.WindowDetachEvent, Application.WindowDetachListener
 
Nested classes/interfaces inherited from interface com.vaadin.terminal.URIHandler
URIHandler.ErrorEvent
 
Field Summary
static int DEFAULT_NOTIFICATION_DELAY
          Default notification linger time for error notifications (in milliseconds): Value is 30000ms.
protected  Logger log
           
 
Constructor Summary
ContextApplication()
           
 
Method Summary
 void addListener(ContextApplication.CloseListener listener)
          Add a ContextApplication.CloseListener to be notified when this instance is closed.
 void close()
          Close this instance.
static ContextApplication currentApplication()
          Get the ContextApplication instance associated with the current thread.
static
<A extends ContextApplication>
A
currentApplication(Class<A> type)
          Get the ContextApplication associated with the current thread, cast to the desired type.
static HttpServletRequest currentRequest()
          Get the HttpServletRequest associated with the current thread.
static HttpServletResponse currentResponse()
          Get the HttpServletResponse associated with the current thread.
protected  void doOnRequestEnd(HttpServletRequest request, HttpServletResponse response)
          Sub-class hook for handling the end of a request.
protected  void doOnRequestStart(HttpServletRequest request, HttpServletResponse response)
          Sub-class hook for handling the start of a request.
static ContextApplication get()
          Get the ContextApplication instance associated with the current thread or throw an exception if there is none.
static
<A extends ContextApplication>
A
get(Class<A> type)
          Get the ContextApplication instance associated with the current thread, cast to the desired type, or throw an exception if there is none.
protected  String getErrorMessage(Throwable t)
          Convert an exception into a displayable error message.
protected  int getNotificationDelay()
          Get the notification linger time for error notifications (in milliseconds).
 void init()
          Initialize the application.
protected abstract  void initApplication()
          Initialize the application.
 void invoke(Runnable action)
          Set this instance as the "current application" (if not set already) while invoking the given callback.
 Future<?> invokeLater(Runnable action)
          Set this instance as the "current application" and invoke the given callback from within another thread.
 void onRequestEnd(HttpServletRequest request, HttpServletResponse response)
          Handle the end of a request.
 void onRequestStart(HttpServletRequest request, HttpServletResponse response)
          Handle the start of a request.
 void removeListener(ContextApplication.CloseListener listener)
          Remove a ContextApplication.CloseListener.
 void showError(String title, String description)
          Display an error message to the user.
 void showError(String title, Throwable t)
          Display an error message to the user caused by an exception.
 void terminalError(Terminal.ErrorEvent event)
          Handle an uncaugt exception thrown by a Vaadin HTTP request.
 
Methods inherited from class com.vaadin.Application
addListener, addListener, addListener, addResource, addWindow, getContext, getErrorHandler, getLocale, getLogoutURL, getMainWindow, getProperty, getPropertyNames, getRelativeLocation, getSystemMessages, getTheme, getURL, getUser, getVersion, getWindow, getWindows, handleURI, isRunning, removeListener, removeListener, removeListener, removeResource, removeWindow, setErrorHandler, setLocale, setLogoutURL, setMainWindow, setTheme, setUser, start
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_NOTIFICATION_DELAY

public static final int DEFAULT_NOTIFICATION_DELAY
Default notification linger time for error notifications (in milliseconds): Value is 30000ms.

See Also:
Constant Field Values

log

protected final Logger log
Constructor Detail

ContextApplication

public ContextApplication()
Method Detail

init

public final void init()
Initialize the application.

The implementation in ContextApplication delegates to initApplication().

Specified by:
init in class Application

initApplication

protected abstract void initApplication()
Initialize the application. Sub-classes of ContextApplication must implement this method.


terminalError

public void terminalError(Terminal.ErrorEvent event)
Handle an uncaugt exception thrown by a Vaadin HTTP request.

The implementation in ContextApplication logs the error and displays in on the user's screen via showError(String, Throwable).

Specified by:
terminalError in interface Terminal.ErrorListener
Overrides:
terminalError in class Application

showError

public void showError(String title,
                      String description)
Display an error message to the user.


showError

public void showError(String title,
                      Throwable t)
Display an error message to the user caused by an exception.


getNotificationDelay

protected int getNotificationDelay()
Get the notification linger time for error notifications (in milliseconds).

The implementation in ContextApplication returns DEFAULT_NOTIFICATION_DELAY.


getErrorMessage

protected String getErrorMessage(Throwable t)
Convert an exception into a displayable error message.


invoke

public void invoke(Runnable action)
Set this instance as the "current application" (if not set already) while invoking the given callback.

This method is useful for situations in which non-Vaadin threads need to call into Vaadin code that expects to successfully retrieve the current application via currentApplication().

This method also synchronizes on this ContextApplication instance as required by Vaadin for thread safety.

Parameters:
action - action to perform
Throws:
IllegalStateException - if a different ContextApplication is already set as the current application associated with the current thread
See Also:
invokeLater()

invokeLater

public Future<?> invokeLater(Runnable action)
Set this instance as the "current application" and invoke the given callback from within another thread.

This method functions like invoke() except that action will be invoked from within a separate thread dedicated to this application instance. This is useful to reduce Vaadin application lock contention, by performing Vaadin-related actions in a separate, dedicated thread. Actions are executed in the order they are given to this method.

The returned Future's get() method will return null upon successful completion. This method itself always returns immediately.

Parameters:
action - action to perform
Returns:
a Future representing the pending results of action
Throws:
IllegalStateException - if this instance is not initialized or has been closed
See Also:
invoke()

onRequestStart

public final void onRequestStart(HttpServletRequest request,
                                 HttpServletResponse response)
Handle the start of a request.

The implementation in ContextApplication delegates to doOnRequestStart(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse).

Specified by:
onRequestStart in interface HttpServletRequestListener

onRequestEnd

public final void onRequestEnd(HttpServletRequest request,
                               HttpServletResponse response)
Handle the end of a request.

The implementation in ContextApplication delegates to doOnRequestEnd(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse).

Specified by:
onRequestEnd in interface HttpServletRequestListener

doOnRequestStart

protected void doOnRequestStart(HttpServletRequest request,
                                HttpServletResponse response)
Sub-class hook for handling the start of a request. This method is invoked by onRequestStart(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse).

The implementation in ContextApplication does nothing. Subclasses should override as necessary.


doOnRequestEnd

protected void doOnRequestEnd(HttpServletRequest request,
                              HttpServletResponse response)
Sub-class hook for handling the end of a request. This method is invoked by onRequestEnd(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse).

The implementation in ContextApplication does nothing. Subclasses should override as necessary.


currentApplication

public static ContextApplication currentApplication()
Get the ContextApplication instance associated with the current thread.

If the current thread is handling a Vaadin HTTP request that is executing within an ContextApplication instance, or is executing within invoke(java.lang.Runnable), then this method will return the associated ContextApplication.

Returns:
the ContextApplication associated with the current thread, or null if the current thread is not servicing a Vaadin web request or the current Vaadin Application is not an ContextApplication
See Also:
invoke(java.lang.Runnable)

currentApplication

public static <A extends ContextApplication> A currentApplication(Class<A> type)
Get the ContextApplication associated with the current thread, cast to the desired type.

Parameters:
type - expected application type
Returns:
the ContextApplication associated with the current thread
Throws:
ClassCastException - if the current application is not assignable to type
See Also:
currentApplication()

get

public static ContextApplication get()
Get the ContextApplication instance associated with the current thread or throw an exception if there is none.

If the current thread is handling a Vaadin web request that is executing within an ContextApplication instance, or is executing within invoke(java.lang.Runnable), then this method will return the associated ContextApplication. Otherwise, an exception is thrown.

Returns:
the ContextApplication associated with the current thread
Throws:
IllegalStateException - if the current thread is not servicing a Vaadin web request or the current Vaadin Application is not an ContextApplication

get

public static <A extends ContextApplication> A get(Class<A> type)
Get the ContextApplication instance associated with the current thread, cast to the desired type, or throw an exception if there is none.

Parameters:
type - expected application type
Returns:
the ContextApplication associated with the current thread
Throws:
IllegalStateException - if the current ContextApplication is not found
ClassCastException - if the current application is not assignable to type
See Also:
get()

currentRequest

public static HttpServletRequest currentRequest()
Get the HttpServletRequest associated with the current thread.

If the current thread is handling a Vaadin web request for an instance of this class, this method will return the associated HttpServletRequest.

Returns:
the HttpServletRequest associated with the current thread, or null if the current thread is not servicing a Vaadin web request or the current Vaadin Application is not an instance of this class

currentResponse

public static HttpServletResponse currentResponse()
Get the HttpServletResponse associated with the current thread.

If the current thread is handling a Vaadin web request for an instance of this class, this method will return the associated HttpServletResponse.

Returns:
the HttpServletResponse associated with the current thread, or null if the current thread is not servicing a Vaadin web request or the current Vaadin Application is not an instance of this class

close

public void close()
Close this instance.

The implementation in ContextApplication first delegates to the superclass and then notifies any registered ContextApplication.CloseListeners.

Overrides:
close in class Application

addListener

public void addListener(ContextApplication.CloseListener listener)
Add a ContextApplication.CloseListener to be notified when this instance is closed.


removeListener

public void removeListener(ContextApplication.CloseListener listener)
Remove a ContextApplication.CloseListener.