/* * Copyright 2011 Vaadin Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.vaadin; import java.io.IOException; import java.io.Serializable; import java.lang.reflect.Method; import java.net.SocketException; import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Enumeration; import java.util.EventObject; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.vaadin.data.util.converter.Converter; import com.vaadin.data.util.converter.ConverterFactory; import com.vaadin.data.util.converter.DefaultConverterFactory; import com.vaadin.event.EventRouter; import com.vaadin.server.AbstractErrorMessage; import com.vaadin.server.AbstractUIProvider; import com.vaadin.server.ApplicationContext; import com.vaadin.server.BootstrapFragmentResponse; import com.vaadin.server.BootstrapListener; import com.vaadin.server.BootstrapPageResponse; import com.vaadin.server.BootstrapResponse; import com.vaadin.server.ChangeVariablesErrorEvent; import com.vaadin.server.ClientConnector; import com.vaadin.server.CombinedRequest; import com.vaadin.server.DeploymentConfiguration; import com.vaadin.server.GlobalResourceHandler; import com.vaadin.server.RequestHandler; import com.vaadin.server.ServletApplicationContext; import com.vaadin.server.Terminal; import com.vaadin.server.UIProvider; import com.vaadin.server.VaadinServlet; import com.vaadin.server.VariableOwner; import com.vaadin.server.WrappedRequest; import com.vaadin.server.WrappedRequest.BrowserDetails; import com.vaadin.server.WrappedResponse; import com.vaadin.shared.ui.ui.UIConstants; import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.AbstractField; import com.vaadin.ui.Table; import com.vaadin.ui.UI; import com.vaadin.ui.Window; import com.vaadin.util.ReflectTools; /** *
* Base class required for all Vaadin applications. This class provides all the * basic services required by Vaadin. These services allow external discovery * and manipulation of the user, {@link com.vaadin.ui.Window windows} and * themes, and starting and stopping the application. *
* *
* As mentioned, all Vaadin applications must inherit this class. However, this
* is almost all of what one needs to do to create a fully functional
* application. The only thing a class inheriting the Application
* needs to do is implement the init
method where it creates the
* windows it needs to perform its function. Note that all applications must
* have at least one window: the main window. The first unnamed window
* constructed by an application automatically becomes the main window which
* behaves just like other windows with one exception: when accessing windows
* using URLs the main window corresponds to the application URL whereas other
* windows correspond to a URL gotten by catenating the window's name to the
* application URL.
*
* See the class com.vaadin.demo.HelloWorld
for a simple example of
* a fully working application.
*
* Window access. Application
provides methods to
* list, add and remove the windows it contains.
*
* Execution control. This class includes method to start and * finish the execution of the application. Being finished means basically that * no windows will be available from the application anymore. *
* ** Theme selection. The theme selection process allows a theme * to be specified at three different levels. When a window's theme needs to be * found out, the window itself is queried for a preferred theme. If the window * does not prefer a specific theme, the application containing the window is * queried. If neither the application prefers a theme, the default theme for * the {@link com.vaadin.server.Terminal terminal} is used. The terminal always * defines a default theme. *
* * @author Vaadin Ltd. * @since 3.0 */ @SuppressWarnings("serial") public class Application implements Terminal.ErrorListener, Serializable { /** * The name of the parameter that is by default used in e.g. web.xml to * define the name of the default {@link UI} class. */ public static final String UI_PARAMETER = "UI"; private static final Method BOOTSTRAP_FRAGMENT_METHOD = ReflectTools .findMethod(BootstrapListener.class, "modifyBootstrapFragment", BootstrapFragmentResponse.class); private static final Method BOOTSTRAP_PAGE_METHOD = ReflectTools .findMethod(BootstrapListener.class, "modifyBootstrapPage", BootstrapPageResponse.class); /** * A special application designed to help migrating applications from Vaadin * 6 to Vaadin 7. The legacy application supports setting a main window, * adding additional browser level windows and defining the theme for the * entire application. * * @deprecated This class is only intended to ease migration and should not * be used for new projects. * * @since 7.0 */ @Deprecated public static class LegacyApplication extends Application { /** * Ignore initial / and then get everything up to the next / */ private static final Pattern WINDOW_NAME_PATTERN = Pattern .compile("^/?([^/]+).*"); private UI.LegacyWindow mainWindow; private String theme; private Map* The main window is the window attached to the application URL ( * {@link #getURL()}) and thus which is show by default to the user. *
** Note that each application must have at least one main window. *
* * @return the UI used as the default window */ public UI.LegacyWindow getMainWindow() { return mainWindow; } private UI getUIInstance(WrappedRequest request) { String pathInfo = request.getRequestPathInfo(); String name = null; if (pathInfo != null && pathInfo.length() > 0) { Matcher matcher = WINDOW_NAME_PATTERN.matcher(pathInfo); if (matcher.matches()) { // Skip the initial slash name = matcher.group(1); } } UI.LegacyWindow window = getWindow(name); if (window != null) { return window; } return mainWindow; } /** * This implementation simulates the way of finding a window for a * request by extracting a window name from the requested path and * passes that name to {@link #getWindow(String)}. ** {@inheritDoc} */ @Override public UI getUIForRequest(WrappedRequest request) { UI uiInstance = getUIInstance(request); if (uiInstance.getUIId() == -1) { // Not initialized -> Let go through createUIInstance to make it // initialized return null; } else { UI.setCurrent(uiInstance); return uiInstance; } } /** * Sets the application's theme. *
* Note that this theme can be overridden for a specific UI with
* {@link Application#getThemeForUI(UI)}. Setting theme to be
* null
selects the default theme. For the available theme
* names, see the contents of the VAADIN/themes directory.
*
null
is returned.
*
* @return the name of the application's theme.
*/
public String getTheme() {
return theme;
}
/**
*
* Gets a UI by name. Returns null
if the application is
* not running or it does not contain a window corresponding to the
* name.
*
null
to use
* the default window
*/
public UI.LegacyWindow getWindow(String name) {
return legacyUINames.get(name);
}
/**
* Counter to get unique names for windows with no explicit name
*/
private int namelessUIIndex = 0;
/**
* Adds a new browser level window to this application. Please note that
* UI doesn't have a name that is used in the URL - to add a named
* window you should instead use {@link #addWindow(UI, String)}
*
* @param uI
* the UI window to add to the application
* @return returns the name that has been assigned to the window
*
* @see #addWindow(UI, String)
*/
public void addWindow(UI.LegacyWindow uI) {
if (uI.getName() == null) {
String name = Integer.toString(namelessUIIndex++);
uI.setName(name);
}
legacyUINames.put(uI.getName(), uI);
uI.setApplication(this);
}
/**
* Removes the specified window from the application. This also removes
* all name mappings for the window (see {@link #addWindow(UI, String)
* and #getWindowName(UI)}.
*
* * Note that removing window from the application does not close the * browser window - the window is only removed from the server-side. *
* * @param uI * the UI to remove */ public void removeWindow(UI.LegacyWindow uI) { for (Entry* Note that the returned set of windows can not be modified. *
* * @return the unmodifiable collection of windows. */ public Collectionnull
if the URL is not defined.
*
* @see Application#getURL()
*/
public URL getApplicationUrl() {
return applicationUrl;
}
/**
* Returns the deployment configuration used by this application.
*
* @return the deployment configuration.
*/
public DeploymentConfiguration getConfiguration() {
return configuration;
}
/**
* Gets the context application will be running in.
*
* @return the context application will be running in.
*
* @see Application#getContext()
*/
public ApplicationContext getContext() {
return context;
}
}
private final static Logger logger = Logger.getLogger(Application.class
.getName());
/**
* Application context the application is running in.
*/
private ApplicationContext context;
/**
* Deployment configuration for the application.
*/
private DeploymentConfiguration configuration;
/**
* The application's URL.
*/
private URL applicationUrl;
/**
* Application status.
*/
private volatile boolean applicationIsRunning = false;
/**
* Default locale of the application.
*/
private Locale locale;
/**
* URL where the user is redirected to on application close, or null if
* application is just closed without redirection.
*/
private String logoutURL = null;
/**
* Application wide error handler which is used by default if an error is
* left unhandled.
*/
private Terminal.ErrorListener errorHandler = this;
/**
* The converter factory that is used to provide default converters for the
* application.
*/
private ConverterFactory converterFactory = new DefaultConverterFactory();
private LinkedList* 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 ( * {@link #getMainWindow()}) of the application. Note that the main window * can also be shown by navigating to the window url ( * {@link com.vaadin.ui.Window#getURL()}). *
* * @return the application's URL. */ public URL getURL() { return applicationUrl; } /** * Ends the Application. ** In effect this will cause the application stop returning any windows when * asked. When the application is closed, close events are fired for its * UIs, its state is removed from the session, and the browser window is * redirected to the application logout url set with * {@link #setLogoutURL(String)}. If the logout url has not been set, the * browser window is reloaded and the application is restarted. */ public void close() { applicationIsRunning = false; for (UI ui : getUIs()) { ui.fireCloseEvent(); } } /** * 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 * {@link javax.servlet.ServletConfig} and they are overridden by * context-wide initialization parameters * {@link javax.servlet.ServletContext}. *
* * @param event * the application start event containing details required for * starting the application. * */ public void start(ApplicationStartEvent event) { applicationUrl = event.getApplicationUrl(); configuration = event.getConfiguration(); context = event.getContext(); init(); applicationIsRunning = true; } /** * Tests if the application is running or if it has been finished. * ** Application starts running when its * {@link #start(URL, Properties, ApplicationContext)} method has been * called and stops when the {@link #close()} is called. *
* * @returntrue
if the application is running,
* false
if not.
*/
public boolean isRunning() {
return applicationIsRunning;
}
/**
*
* Main initializer of the application. The init
method is
* called by the framework when the application is started, and it should
* perform whatever initialization operations the application needs.
*
* See {@link #start(URL, Properties, ApplicationContext)} how properties * are defined. *
* * @return an enumeration of all the keys in this property list, including * the keys in the default property list. * */ public Enumeration> getPropertyNames() { return getProperties().propertyNames(); } /** * Searches for the property with the specified name in this application. * This method returnsnull
if the property is not found.
*
* See {@link #start(URL, Properties, ApplicationContext)} how properties
* are defined.
*
* @param name
* the name of the property.
* @return the value in this property list with the specified key value.
*/
public String getProperty(String name) {
return getProperties().getProperty(name);
}
/**
* Gets the default locale for this application.
*
* By default this is the preferred locale of the user using the
* application. In most cases it is read from the browser defaults.
*
* @return the locale of this application.
*/
public Locale getLocale() {
if (locale != null) {
return locale;
}
return Locale.getDefault();
}
/**
* Sets the default locale for this application.
*
* By default this is the preferred locale of the user using the
* application. In most cases it is read from the browser defaults.
*
* @param locale
* the Locale object.
*
*/
public void setLocale(Locale locale) {
this.locale = locale;
}
/**
* Window detach event.
*
* This event is sent each time a window is removed from the application
* with {@link com.vaadin.Application#removeWindow(Window)}.
*/
public static class WindowDetachEvent extends EventObject {
private final Window window;
/**
* Creates a event.
*
* @param application
* the application to which the detached window belonged.
* @param window
* the Detached window.
*/
public WindowDetachEvent(Application application, Window window) {
super(application);
this.window = window;
}
/**
* Gets the detached window.
*
* @return the detached window.
*/
public Window getWindow() {
return window;
}
/**
* Gets the application from which the window was detached.
*
* @return the Application.
*/
public Application getApplication() {
return (Application) getSource();
}
}
/**
* Window attach event.
*
* This event is sent each time a window is attached tothe application with
* {@link com.vaadin.Application#addWindow(Window)}.
*/
public static class WindowAttachEvent extends EventObject {
private final Window window;
/**
* Creates a event.
*
* @param application
* the application to which the detached window belonged.
* @param window
* the Attached window.
*/
public WindowAttachEvent(Application application, Window window) {
super(application);
this.window = window;
}
/**
* Gets the attached window.
*
* @return the attached window.
*/
public Window getWindow() {
return window;
}
/**
* Gets the application to which the window was attached.
*
* @return the Application.
*/
public Application getApplication() {
return (Application) getSource();
}
}
/**
* Window attach listener interface.
*/
public interface WindowAttachListener extends Serializable {
/**
* Window attached
*
* @param event
* the window attach event.
*/
public void windowAttached(WindowAttachEvent event);
}
/**
* Window detach listener interface.
*/
public interface WindowDetachListener extends Serializable {
/**
* Window detached.
*
* @param event
* the window detach event.
*/
public void windowDetached(WindowDetachEvent event);
}
/**
* 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. *
* * @return the URL. */ public String getLogoutURL() { return logoutURL; } /** * 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.
*
* @param logoutURL
* the logoutURL to set.
*/
public void setLogoutURL(String logoutURL) {
this.logoutURL = logoutURL;
}
/**
*
* Invoked by the terminal on any exception that occurs in application and
* is thrown by the setVariable
to the terminal. The default
* implementation sets the exceptions as ComponentErrors
to the
* component that initiated the exception and prints stack trace to standard
* error stream.
*
* You can safely override this method in your application in order to * direct the errors to some other destination (for example log). *
* * @param event * the change event. * @see com.vaadin.server.Terminal.ErrorListener#terminalError(com.vaadin.server.Terminal.ErrorEvent) */ @Override public void terminalError(Terminal.ErrorEvent event) { final Throwable t = event.getThrowable(); if (t instanceof SocketException) { // Most likely client browser closed socket getLogger().info( "SocketException in CommunicationManager." + " Most likely client (browser) closed socket."); return; } // Finds the original source of the error/exception Object owner = null; if (event instanceof VariableOwner.ErrorEvent) { owner = ((VariableOwner.ErrorEvent) event).getVariableOwner(); } else if (event instanceof ChangeVariablesErrorEvent) { owner = ((ChangeVariablesErrorEvent) event).getComponent(); } // Shows the error in AbstractComponent if (owner instanceof AbstractComponent) { ((AbstractComponent) owner).setComponentError(AbstractErrorMessage .getErrorMessageForException(t)); } // also print the error on console getLogger().log(Level.SEVERE, "Terminal error:", t); } /** * Gets the application context. ** The application context is the environment where the application is * running in. The actual implementation class of may contains quite a lot * more functionality than defined in the {@link ApplicationContext} * interface. *
** By default, when you are deploying your application to a servlet * container, the implementation class is {@link ServletApplicationContext} * - you can safely cast to this class and use the methods from there. When * you are deploying your application as a portlet, context implementation * is {@link PortletApplicationContext}. *
* * @return the application context. */ public ApplicationContext getContext() { return context; } /** * Gets the application error handler. * * The default error handler is the application itself. * * @return Application error handler */ public Terminal.ErrorListener getErrorHandler() { return errorHandler; } /** * Sets the application error handler. * * The default error handler is the application itself. By overriding this, * you can redirect the error messages to your selected target (log for * example). * * @param errorHandler */ public void setErrorHandler(Terminal.ErrorListener errorHandler) { this.errorHandler = errorHandler; } /** * Gets the {@link ConverterFactory} used to locate a suitable * {@link Converter} for fields in the application. * * See {@link #setConverterFactory(ConverterFactory)} for more details * * @return The converter factory used in the application */ public ConverterFactory getConverterFactory() { return converterFactory; } /** * Sets the {@link ConverterFactory} used to locate a suitable * {@link Converter} for fields in the application. ** The {@link 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 {@link Converter} for an individual field can be overridden using * {@link AbstractField#setConverter(Converter)} and for individual property * ids in a {@link Table} using * {@link Table#setConverter(Object, Converter)}. *
** The converter factory must never be set to null. * * @param converterFactory * The converter factory used in the application */ public void setConverterFactory(ConverterFactory converterFactory) { this.converterFactory = converterFactory; } /** * Contains the system messages used to notify the user about various * critical situations that can occur. *
* Customize by overriding the static * {@link Application#getSystemMessages()} and returning * {@link CustomizedSystemMessages}. *
** The defaults defined in this class are: *
* Vaadin gets the SystemMessages from your application by calling a static * getSystemMessages() method. By default the * Application.getSystemMessages() is used. You can customize this by * defining a static MyApplication.getSystemMessages() and returning * CustomizedSystemMessages. Note that getSystemMessages() is static - * changing the system messages will by default change the message for all * users of the application. *
*
* The default behavior is to show a notification, and restart the
* application the the user clicks the message.
* Instead of restarting the application, you can set a specific URL that
* the user is taken to.
* Setting both caption and message to null will restart the application (or
* go to the specified URL) without displaying a notification.
* set*NotificationEnabled(false) will achieve the same thing.
*
* The situations are: *
* Subclasses of Application may override this method to provide custom * logic for choosing what kind of UI to use. *
* The default implementation in {@link Application} uses the
* {@value #UI_PARAMETER} parameter from web.xml for finding the name of the
* UI class. If {@link DeploymentConfiguration#getClassLoader()} does not
* return null
, the returned {@link ClassLoader} is used for
* loading the UI class. Otherwise the {@link ClassLoader} used to load this
* class is used.
*
*
* Subclasses of Application may override this method to provide custom * logic for choosing how to create a suitable UI or for picking an already * created UI. If an existing UI is picked, care should be taken to avoid * keeping the same UI open in multiple browser windows, as that will cause * the states to go out of sync. *
* * @param request * @param uiClass * @return */ protectednull
to use the first UI provider supporting the
* request.
* @return an UI provider supporting the request (and the UI class if
* provided).
*
* @since 7.0.0
*/
public UIProvider getUiProvider(WrappedRequest request, Class> uiClass) {
UIProvider provider = (UIProvider) request
.getAttribute(UIProvider.class.getName());
if (provider != null) {
// Cached provider found, verify that it's a sensible selection
Class extends UI> providerClass = provider.getUIClass(this,
request);
if (uiClass == null && providerClass != null) {
// Use it if it gives any answer if no specific class is
// required
return provider;
} else if (uiClass == providerClass) {
// Use it if it gives the expected UI class
return provider;
} else {
// Don't keep it cached if it doesn't match the expectations
request.setAttribute(UIProvider.class.getName(), null);
}
}
// Iterate all current providers if no matching cached provider found
provider = doGetUiProvider(request, uiClass);
// Cache the found provider
request.setAttribute(UIProvider.class.getName(), provider);
return provider;
}
private UIProvider doGetUiProvider(WrappedRequest request, Class> uiClass) {
int providersSize = uiProviders.size();
if (providersSize == 0) {
throw new IllegalStateException("There are no UI providers");
}
for (int i = providersSize - 1; i >= 0; i--) {
UIProvider provider = uiProviders.get(i);
Class extends UI> providerClass = provider.getUIClass(this,
request);
// If we found something
if (providerClass != null) {
if (uiClass == null) {
// Not looking for anything particular -> anything is ok
return provider;
} else if (providerClass == uiClass) {
// Looking for a specific provider -> only use if matching
return provider;
} else {
getLogger().warning(
"Mismatching UI classes. Expected " + uiClass
+ " but got " + providerClass + " from "
+ provider);
// Continue looking
}
}
}
throw new RuntimeException("No UI provider found for request");
}
/**
* Handles a request by passing it to each registered {@link RequestHandler}
* in turn until one produces a response. This method is used for requests
* that have not been handled by any specific functionality in the terminal
* implementation (e.g. {@link VaadinServlet}).
* * The request handlers are invoked in the revere order in which they were * added to the application until a response has been produced. This means * that the most recently added handler is used first and the first request * handler that was added to the application is invoked towards the end * unless any previous handler has already produced a response. *
* * @param request * the wrapped request to get information from * @param response * the response to which data can be written * @return returnstrue
if a {@link RequestHandler} has
* produced a response and false
if no response has
* been written.
* @throws IOException
*
* @see #addRequestHandler(RequestHandler)
* @see RequestHandler
*
* @since 7.0
*/
public boolean handleRequest(WrappedRequest request,
WrappedResponse response) throws IOException {
// Use a copy to avoid ConcurrentModificationException
for (RequestHandler handler : new ArrayList* Handlers are called in reverse order of addition, so the most recently * added handler will be called first. *
* * @param handler * the request handler to add * * @see #handleRequest(WrappedRequest, WrappedResponse) * @see #removeRequestHandler(RequestHandler) * * @since 7.0 */ public void addRequestHandler(RequestHandler handler) { requestHandlers.addFirst(handler); } /** * Removes a request handler from the application. * * @param handler * the request handler to remove * * @since 7.0 */ public void removeRequestHandler(RequestHandler handler) { requestHandlers.remove(handler); } /** * Gets the request handlers that are registered to the application. 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. * * @return a collection of request handlers, with the iteration order * according to the order they would be invoked * * @see #handleRequest(WrappedRequest, WrappedResponse) * @see #addRequestHandler(RequestHandler) * @see #removeRequestHandler(RequestHandler) * * @since 7.0 */ public Collectionnull
*
* @see #setCurrent(Application)
*
* @since 7.0
*/
public static Application getCurrent() {
return currentApplication.get();
}
/**
* Sets the thread local for the current application. This method is used by
* the framework to set the current application 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 * application outside the normal request handling, e.g. when initiating * custom background threads. *
* * @param application * * @see #getCurrent() * @see ThreadLocal * * @since 7.0 */ public static void setCurrent(Application application) { currentApplication.set(application); } /** * Check whether this application is in production mode. If an application * is in production mode, certain debugging facilities are not available. * * @return the status of the production mode flag * * @since 7.0 */ public boolean isProductionMode() { return configuration.isProductionMode(); } public void addUIProvider(UIProvider uIProvider) { uiProviders.add(uIProvider); } public void removeUIProvider(UIProvider uIProvider) { uiProviders.remove(uIProvider); } /** * Finds the {@link 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 {@link 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
* {@link #isUIInitPending(int)} with the UI's id ( {@link UI#getUIId()} to
* check whether the initialization is still pending.
*
null
if no UI id is defined
*
* @since 7.0
*/
private static Integer getUIId(WrappedRequest request) {
if (request instanceof CombinedRequest) {
// Combined requests has the uiId parameter in the second request
CombinedRequest combinedRequest = (CombinedRequest) request;
request = combinedRequest.getSecondRequest();
}
String uiIdString = request.getParameter(UIConstants.UI_ID_PARAMETER);
Integer uiId = uiIdString == null ? null : new Integer(uiIdString);
return uiId;
}
/**
* Gets all the uIs of this application. This includes uIs that have been
* requested but not yet initialized. Please note, that uIs are not
* automatically removed e.g. if the browser window is closed and that there
* is no way to manually remove a UI. Inactive uIs will thus not be released
* for GC until the entire application is released when the session has
* timed out (unless there are dangling references). Improved support for
* releasing unused uIs is planned for an upcoming alpha release of Vaadin
* 7.
*
* @return a collection of uIs belonging to this application
*
* @since 7.0
*/
public Collection* This is meant for framework internal use. *
* * @param uiId * The UI id * @return The UI with the given id or null if not found */ public UI getUIById(int uiId) { return uIs.get(uiId); } /** * 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. * * @see BootstrapListener#modifyBootstrapFragment(BootstrapFragmentResponse) * @see BootstrapListener#modifyBootstrapPage(BootstrapPageResponse) * * @param listener * the bootstrap listener to add */ public void addBootstrapListener(BootstrapListener listener) { eventRouter.addListener(BootstrapFragmentResponse.class, listener, BOOTSTRAP_FRAGMENT_METHOD); eventRouter.addListener(BootstrapPageResponse.class, listener, BOOTSTRAP_PAGE_METHOD); } /** * Remove a bootstrap listener that was previously added. * * @see #addBootstrapListener(BootstrapListener) * * @param listener * the bootstrap listener to remove */ public void removeBootstrapListener(BootstrapListener listener) { eventRouter.removeListener(BootstrapFragmentResponse.class, listener, BOOTSTRAP_FRAGMENT_METHOD); eventRouter.removeListener(BootstrapPageResponse.class, listener, BOOTSTRAP_PAGE_METHOD); } /** * Fires a bootstrap event to all registered listeners. There are currently * two supported events, both inheriting from {@link BootstrapResponse}: * {@link BootstrapFragmentResponse} and {@link BootstrapPageResponse}. * * @param response * the bootstrap response event for which listeners should be * fired */ public void modifyBootstrapResponse(BootstrapResponse response) { eventRouter.fireEvent(response); } /** * Removes all those UIs from the application for which {@link #isUIAlive} * returns false. Close events are fired for the removed UIs. *
* Called by the framework at the end of every request.
*
* @see UI.CloseEvent
* @see UI.CloseListener
* @see #isUIAlive(UI)
*
* @since 7.0.0
*/
public void closeInactiveUIs() {
for (Iterator
* This timeout only has effect if cleanup of inactive UIs is enabled;
* otherwise heartbeat requests are enough to extend UI lifetime
* indefinitely.
*
* @see DeploymentConfiguration#isIdleUICleanupEnabled()
* @see #getHeartbeatTimeout()
* @see #closeInactiveUIs()
*
* @since 7.0.0
*
* @return The UIDL request timeout in seconds, or a negative number if
* timeout never occurs.
*/
protected int getUidlRequestTimeout() {
return configuration.isIdleUICleanupEnabled() ? getContext()
.getSession().getMaxInactiveInterval() : -1;
}
/**
* Returns whether the given UI is alive (the client-side actively
* communicates with the server) or whether it can be removed from the
* application and eventually collected.
*
* @since 7.0.0
*
* @param ui
* The UI whose status to check
* @return true if the UI is alive, false if it could be removed.
*/
protected boolean isUIAlive(UI ui) {
long now = System.currentTimeMillis();
if (getHeartbeatTimeout() >= 0
&& now - ui.getLastHeartbeatTime() > 1000 * getHeartbeatTimeout()) {
return false;
}
if (getUidlRequestTimeout() >= 0
&& now - ui.getLastUidlRequestTime() > 1000 * getUidlRequestTimeout()) {
return false;
}
return true;
}
/**
* Gets this application'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.
*
* @param 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.
* @return this application's global resource handler, or null
* if there is no handler and the createOnDemand parameter is
* false
.
*
* @since 7.0.0
*/
public GlobalResourceHandler getGlobalResourceHandler(boolean createOnDemand) {
if (globalResourceHandler == null && createOnDemand) {
globalResourceHandler = new GlobalResourceHandler();
addRequestHandler(globalResourceHandler);
}
return globalResourceHandler;
}
}