From: Leif Åstrand Date: Wed, 5 Sep 2012 14:10:31 +0000 (+0300) Subject: Combine ApplicationContext into Application (#9402) X-Git-Tag: 7.0.0.beta1~184^2~3 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5ab35cc6080653a7e9b25712f637db6c95c183ce;p=vaadin-framework.git Combine ApplicationContext into Application (#9402) --- diff --git a/server/src/com/vaadin/Application.java b/server/src/com/vaadin/Application.java index 2baf252a62..6d37fbd5ae 100644 --- a/server/src/com/vaadin/Application.java +++ b/server/src/com/vaadin/Application.java @@ -31,17 +31,19 @@ import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; +import javax.servlet.http.HttpSessionBindingEvent; +import javax.servlet.http.HttpSessionBindingListener; + 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.AbstractCommunicationManager; import com.vaadin.server.AbstractErrorMessage; import com.vaadin.server.ApplicationConfiguration; -import com.vaadin.server.ApplicationContext; import com.vaadin.server.BootstrapFragmentResponse; import com.vaadin.server.BootstrapListener; import com.vaadin.server.BootstrapPageResponse; @@ -52,14 +54,15 @@ 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.WebBrowser; import com.vaadin.server.WrappedRequest; import com.vaadin.server.WrappedRequest.BrowserDetails; import com.vaadin.server.WrappedResponse; +import com.vaadin.server.WrappedSession; import com.vaadin.shared.ui.ui.UIConstants; import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.AbstractField; @@ -121,7 +124,8 @@ import com.vaadin.util.ReflectTools; * @since 3.0 */ @SuppressWarnings("serial") -public class Application implements Terminal.ErrorListener, Serializable { +public class Application implements Terminal.ErrorListener, + HttpSessionBindingListener, Serializable { /** * The name of the parameter that is by default used in e.g. web.xml to @@ -147,22 +151,22 @@ public class Application implements Terminal.ErrorListener, Serializable { private final ApplicationConfiguration configuration; - private final ApplicationContext context; + private final AbstractCommunicationManager communicationManager; /** * @param applicationUrl * the URL the application should respond to. * @param configuration * the application configuration for the application. - * @param context - * the context application will be running in. + * @param communicationManager + * the communication manager for the application. */ public ApplicationStartEvent(URL applicationUrl, ApplicationConfiguration configuration, - ApplicationContext context) { + AbstractCommunicationManager communicationManager) { this.applicationUrl = applicationUrl; this.configuration = configuration; - this.context = context; + this.communicationManager = communicationManager; } /** @@ -187,25 +191,20 @@ public class Application implements Terminal.ErrorListener, Serializable { } /** - * Gets the context application will be running in. + * Gets the communication manager for this application. * - * @return the context application will be running in. + * @return the communication manager for this application. * - * @see Application#getContext() + * @see Application#getCommunicationManager */ - public ApplicationContext getContext() { - return context; + public AbstractCommunicationManager getCommunicationManager() { + return communicationManager; } } private final static Logger logger = Logger.getLogger(Application.class .getName()); - /** - * Application context the application is running in. - */ - private ApplicationContext context; - /** * Configuration for the application. */ @@ -257,6 +256,87 @@ public class Application implements Terminal.ErrorListener, Serializable { private GlobalResourceHandler globalResourceHandler; + protected WebBrowser browser = new WebBrowser(); + + private AbstractCommunicationManager communicationManager; + + private long totalSessionTime = 0; + + private long lastRequestTime = -1; + + private transient WrappedSession session; + + /** + * @see javax.servlet.http.HttpSessionBindingListener#valueBound(HttpSessionBindingEvent) + */ + @Override + public void valueBound(HttpSessionBindingEvent arg0) { + // We are not interested in bindings + } + + /** + * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent) + */ + @Override + public void valueUnbound(HttpSessionBindingEvent event) { + // If we are going to be unbound from the session, the session must be + // closing + close(); + } + + /** + * Get the web browser associated with this application context. + * + * Because application context is related to the http session and server + * maintains one session per browser-instance, each context has exactly one + * web browser associated with it. + * + * @return + */ + public WebBrowser getBrowser() { + return browser; + } + + /** + * @return The total time spent servicing requests in this session. + */ + public long getTotalSessionTime() { + return totalSessionTime; + } + + /** + * Sets the time spent servicing the last request in the session and updates + * the total time spent servicing requests in this session. + * + * @param time + * the time spent in the last request. + */ + public void setLastRequestTime(long time) { + lastRequestTime = time; + totalSessionTime += time; + } + + /** + * @return the time spent servicing the last request in this session. + */ + public long getLastRequestTime() { + return lastRequestTime; + } + + /** + * Gets the session to which this application context is currently + * associated. + * + * @return the wrapped session for this context + */ + public WrappedSession getSession() { + return session; + } + + public AbstractCommunicationManager getApplicationManager() { + return communicationManager; + } + /** * Gets the URL of the application. * @@ -291,6 +371,28 @@ public class Application implements Terminal.ErrorListener, Serializable { } } + public static Application getForSession(WrappedSession session) { + Object attribute = session.getAttribute(Application.class.getName()); + if (attribute instanceof Application) { + Application application = (Application) attribute; + application.session = session; + return application; + } + + return null; + } + + public void removeFromSession() { + assert (getForSession(session) == this); + + session.setAttribute(Application.class.getName(), null); + } + + public void storeInSession(WrappedSession session) { + session.setAttribute(Application.class.getName(), this); + this.session = session; + } + /** * Starts the application on the given URL. * @@ -316,8 +418,7 @@ public class Application implements Terminal.ErrorListener, Serializable { public void start(ApplicationStartEvent event) { applicationUrl = event.getApplicationUrl(); configuration = event.getConfiguration(); - context = event.getContext(); - init(); + communicationManager = event.getCommunicationManager(); applicationIsRunning = true; } @@ -325,9 +426,8 @@ public class Application implements Terminal.ErrorListener, Serializable { * 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. + * Application starts running when its {@link #start(ApplicationStartEvent)} + * method has been called and stops when the {@link #close()} is called. *

* * @return true if the application is running, @@ -337,17 +437,6 @@ public class Application implements Terminal.ErrorListener, Serializable { 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. - *

- */ - public void init() { - // Default implementation does nothing - } - /** * Gets the configuration for this application * @@ -574,28 +663,6 @@ public class Application implements Terminal.ErrorListener, Serializable { 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. * @@ -1871,8 +1938,8 @@ public class Application implements Terminal.ErrorListener, Serializable { * timeout never occurs. */ protected int getUidlRequestTimeout() { - return configuration.isIdleUICleanupEnabled() ? getContext() - .getSession().getMaxInactiveInterval() : -1; + return configuration.isIdleUICleanupEnabled() ? getSession() + .getMaxInactiveInterval() : -1; } /** diff --git a/server/src/com/vaadin/LegacyApplication.java b/server/src/com/vaadin/LegacyApplication.java index 5bfc466fa4..b884136767 100644 --- a/server/src/com/vaadin/LegacyApplication.java +++ b/server/src/com/vaadin/LegacyApplication.java @@ -26,27 +26,25 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import com.vaadin.server.AbstractUIProvider; -import com.vaadin.server.ApplicationContext; -import com.vaadin.server.WrappedRequest; import com.vaadin.server.Terminal.ErrorEvent; import com.vaadin.server.Terminal.ErrorListener; +import com.vaadin.server.WrappedRequest; import com.vaadin.ui.UI; -import com.vaadin.ui.UI.LegacyWindow; /** - * 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. + * 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. + * @deprecated This class is only intended to ease migration and should not be + * used for new projects. * * @since 7.0 */ @Deprecated -public abstract class LegacyApplication extends AbstractUIProvider - implements ErrorListener { +public abstract class LegacyApplication extends AbstractUIProvider implements + ErrorListener { /** * Ignore initial / and then get everything up to the next / */ @@ -59,16 +57,15 @@ public abstract class LegacyApplication extends AbstractUIProvider private Map legacyUINames = new HashMap(); /** - * Sets the main window of this application. Setting window as a main - * window of this application also adds the window to this application. + * Sets the main window of this application. Setting window as a main window + * of this application also adds the window to this application. * * @param mainWindow * the UI to set as the default window */ public void setMainWindow(UI.LegacyWindow mainWindow) { if (this.mainWindow != null) { - throw new IllegalStateException( - "mainWindow has already been set"); + throw new IllegalStateException("mainWindow has already been set"); } if (mainWindow.getApplication() == null) { mainWindow.setApplication(Application.getCurrent()); @@ -102,8 +99,8 @@ public abstract class LegacyApplication extends AbstractUIProvider } @Override - public UI createInstance(Application application, - Class type, WrappedRequest request) { + public UI createInstance(Application application, Class type, + WrappedRequest request) { return getUIInstance(request); } @@ -159,9 +156,9 @@ public abstract class LegacyApplication extends AbstractUIProvider } /** - * 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)}. + * 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} */ @@ -196,9 +193,9 @@ public abstract class LegacyApplication extends AbstractUIProvider /** * Gets the application's theme. The application's theme is the default - * theme used by all the uIs for which a theme is not explicitly - * defined. If the application theme is not explicitly set, - * null is returned. + * theme used by all the uIs for which a theme is not explicitly defined. If + * the application theme is not explicitly set, null is + * returned. * * @return the name of the application's theme. */ @@ -208,15 +205,14 @@ public abstract class LegacyApplication extends AbstractUIProvider /** *

- * Gets a UI by name. Returns null if the application is - * not running or it does not contain a window corresponding to the - * name. + * Gets a UI by name. Returns null if the application is not + * running or it does not contain a window corresponding to the name. *

* * @param name * the name of the requested window - * @return a UI corresponding to the name, or null to use - * the default window + * @return a UI corresponding to the name, or null to use the + * default window */ public UI.LegacyWindow getWindow(String name) { return legacyUINames.get(name); @@ -228,9 +224,9 @@ public abstract class LegacyApplication extends AbstractUIProvider 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)} + * 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 @@ -249,21 +245,20 @@ public abstract class LegacyApplication extends AbstractUIProvider } /** - * Removes the specified window from the application. This also removes - * all name mappings for the window (see {@link #addWindow(UI, String) - * and #getWindowName(UI)}. + * 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. + * 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 entry : legacyUINames - .entrySet()) { + for (Entry entry : legacyUINames.entrySet()) { if (entry.getValue() == uI) { legacyUINames.remove(entry.getKey()); } @@ -288,8 +283,8 @@ public abstract class LegacyApplication extends AbstractUIProvider Application.getCurrent().terminalError(event); } - public ApplicationContext getContext() { - return Application.getCurrent().getContext(); + public Application getContext() { + return Application.getCurrent(); } protected void close() { diff --git a/server/src/com/vaadin/server/AbstractCommunicationManager.java b/server/src/com/vaadin/server/AbstractCommunicationManager.java index 3d27dd18b9..47bf652a3a 100644 --- a/server/src/com/vaadin/server/AbstractCommunicationManager.java +++ b/server/src/com/vaadin/server/AbstractCommunicationManager.java @@ -1313,9 +1313,9 @@ public abstract class AbstractCommunicationManager implements Serializable { * response. */ private void writePerformanceData(final PrintWriter outWriter) { - ApplicationContext ctx = application.getContext(); outWriter.write(String.format(", \"timings\":[%d, %d]", - ctx.getTotalSessionTime(), ctx.getLastRequestTime())); + application.getTotalSessionTime(), + application.getLastRequestTime())); } private void legacyPaint(PaintTarget paintTarget, diff --git a/server/src/com/vaadin/server/ApplicationContext.java b/server/src/com/vaadin/server/ApplicationContext.java deleted file mode 100644 index 0b317486e4..0000000000 --- a/server/src/com/vaadin/server/ApplicationContext.java +++ /dev/null @@ -1,182 +0,0 @@ -/* - * 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.server; - -import java.io.Serializable; -import java.util.logging.Level; -import java.util.logging.Logger; - -import javax.servlet.http.HttpSessionBindingEvent; -import javax.servlet.http.HttpSessionBindingListener; - -import com.vaadin.Application; - -/** - * ApplicationContext provides information about the running - * context of the application. Each context is shared by all applications that - * are open for one user. In a web-environment this corresponds to a - * HttpSession. - *

- * Base class for web application contexts (including portlet contexts) that - * handles the common tasks. - * - * @author Vaadin Ltd. - * @since 3.1 - */ -public abstract class ApplicationContext implements HttpSessionBindingListener, - Serializable { - - private Application application; - - protected WebBrowser browser = new WebBrowser(); - - private AbstractCommunicationManager communicationManager; - - private long totalSessionTime = 0; - - private long lastRequestTime = -1; - - private transient WrappedSession session; - - /** - * @see javax.servlet.http.HttpSessionBindingListener#valueBound(HttpSessionBindingEvent) - */ - @Override - public void valueBound(HttpSessionBindingEvent arg0) { - // We are not interested in bindings - } - - /** - * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent) - */ - @Override - public void valueUnbound(HttpSessionBindingEvent event) { - // If we are going to be unbound from the session, the session must be - // closing - removeApplication(); - } - - /** - * Get the web browser associated with this application context. - * - * Because application context is related to the http session and server - * maintains one session per browser-instance, each context has exactly one - * web browser associated with it. - * - * @return - */ - public WebBrowser getBrowser() { - return browser; - } - - /** - * Returns the applications in this context. - * - * Each application context contains the application for one user. - * - * @return The application of this context, or null if there is - * no application - */ - public Application getApplication() { - return application; - } - - public void removeApplication() { - if (application == null) { - return; - } - try { - application.close(); - } catch (Exception e) { - // This should never happen but is possible with rare - // configurations (e.g. robustness tests). If you have one - // thread doing HTTP socket write and another thread trying to - // remove same application here. Possible if you got e.g. session - // lifetime 1 min but socket write may take longer than 1 min. - // FIXME: Handle exception - getLogger().log(Level.SEVERE, - "Could not close application, leaking memory.", e); - } finally { - application = null; - communicationManager = null; - } - } - - /** - * @return The total time spent servicing requests in this session. - */ - public long getTotalSessionTime() { - return totalSessionTime; - } - - /** - * Sets the time spent servicing the last request in the session and updates - * the total time spent servicing requests in this session. - * - * @param time - * the time spent in the last request. - */ - public void setLastRequestTime(long time) { - lastRequestTime = time; - totalSessionTime += time; - } - - /** - * @return the time spent servicing the last request in this session. - */ - public long getLastRequestTime() { - return lastRequestTime; - } - - private Logger getLogger() { - return Logger.getLogger(ApplicationContext.class.getName()); - } - - /** - * Gets the session to which this application context is currently - * associated. - * - * @return the wrapped session for this context - */ - public WrappedSession getSession() { - return session; - } - - /** - * Sets the session to which this application context is currently - * associated. - * - * @param session - * the wrapped session for this context - */ - public void setSession(WrappedSession session) { - this.session = session; - } - - public AbstractCommunicationManager getApplicationManager() { - return communicationManager; - } - - public void setApplication(Application application, - AbstractCommunicationManager communicationManager) { - if (this.application != null) { - removeApplication(); - } - this.application = application; - this.communicationManager = communicationManager; - } - -} \ No newline at end of file diff --git a/server/src/com/vaadin/server/CombinedRequest.java b/server/src/com/vaadin/server/CombinedRequest.java index 3432cda942..9176754d93 100644 --- a/server/src/com/vaadin/server/CombinedRequest.java +++ b/server/src/com/vaadin/server/CombinedRequest.java @@ -143,9 +143,7 @@ public class CombinedRequest implements WrappedRequest { @Override public WebBrowser getWebBrowser() { - ApplicationContext context = Application.getCurrent() - .getContext(); - return context.getBrowser(); + return Application.getCurrent().getBrowser(); } }; } diff --git a/server/src/com/vaadin/server/CommunicationManager.java b/server/src/com/vaadin/server/CommunicationManager.java index cc92023919..beea884aae 100644 --- a/server/src/com/vaadin/server/CommunicationManager.java +++ b/server/src/com/vaadin/server/CommunicationManager.java @@ -113,7 +113,7 @@ public class CommunicationManager extends AbstractCommunicationManager { protected InputStream getThemeResourceAsStream(UI uI, String themeName, String resource) { ServletApplicationContext context = (ServletApplicationContext) uI - .getApplication().getContext(); + .getApplication(); ServletContext servletContext = context.getHttpSession() .getServletContext(); return servletContext.getResourceAsStream("/" diff --git a/server/src/com/vaadin/server/GAEVaadinServlet.java b/server/src/com/vaadin/server/GAEVaadinServlet.java index 642737f73b..1175d6a960 100644 --- a/server/src/com/vaadin/server/GAEVaadinServlet.java +++ b/server/src/com/vaadin/server/GAEVaadinServlet.java @@ -47,6 +47,7 @@ import com.google.appengine.api.memcache.Expiration; import com.google.appengine.api.memcache.MemcacheService; import com.google.appengine.api.memcache.MemcacheServiceFactory; import com.google.apphosting.api.DeadlineExceededException; +import com.vaadin.Application; /** * ApplicationServlet to be used when deploying to Google App Engine, in @@ -241,7 +242,7 @@ public class GAEVaadinServlet extends VaadinServlet { } // de-serialize or create application context, store in session - ApplicationContext ctx = getApplicationContext(request, memcache); + Application ctx = getApplicationContext(request, memcache); super.service(request, response); @@ -291,8 +292,8 @@ public class GAEVaadinServlet extends VaadinServlet { } } - protected ApplicationContext getApplicationContext( - HttpServletRequest request, MemcacheService memcache) { + protected Application getApplicationContext(HttpServletRequest request, + MemcacheService memcache) { HttpSession session = request.getSession(); String id = AC_BASE + session.getId(); byte[] serializedAC = (byte[]) memcache.get(id); @@ -320,10 +321,9 @@ public class GAEVaadinServlet extends VaadinServlet { ObjectInputStream ois; try { ois = new ObjectInputStream(bais); - ApplicationContext applicationContext = (ApplicationContext) ois - .readObject(); - session.setAttribute(ServletApplicationContext.class.getName(), - applicationContext); + Application applicationContext = (Application) ois.readObject(); + applicationContext.storeInSession(new WrappedHttpSession( + session)); } catch (IOException e) { getLogger().log( Level.WARNING, diff --git a/server/src/com/vaadin/server/LegacyVaadinPortlet.java b/server/src/com/vaadin/server/LegacyVaadinPortlet.java index 7de38eaf94..77a94e4d0a 100644 --- a/server/src/com/vaadin/server/LegacyVaadinPortlet.java +++ b/server/src/com/vaadin/server/LegacyVaadinPortlet.java @@ -46,9 +46,10 @@ public class LegacyVaadinPortlet extends VaadinPortlet { } @Override - protected Application createApplication(PortletRequest request) - throws PortletException { - Application application = super.createApplication(request); + protected PortletApplicationContext2 createApplication( + PortletRequest request) throws PortletException { + PortletApplicationContext2 application = super + .createApplication(request); // Must set current before running init() Application.setCurrent(application); diff --git a/server/src/com/vaadin/server/LegacyVaadinServlet.java b/server/src/com/vaadin/server/LegacyVaadinServlet.java index 21419a2e33..aa78c401f5 100644 --- a/server/src/com/vaadin/server/LegacyVaadinServlet.java +++ b/server/src/com/vaadin/server/LegacyVaadinServlet.java @@ -46,9 +46,10 @@ public class LegacyVaadinServlet extends VaadinServlet { } @Override - protected Application createApplication(HttpServletRequest request) - throws ServletException { - Application application = super.createApplication(request); + protected ServletApplicationContext createApplication( + HttpServletRequest request) throws ServletException { + ServletApplicationContext application = super + .createApplication(request); // Must set current before running init() Application.setCurrent(application); diff --git a/server/src/com/vaadin/server/Page.java b/server/src/com/vaadin/server/Page.java index 015c6c907f..a1c181dcb9 100644 --- a/server/src/com/vaadin/server/Page.java +++ b/server/src/com/vaadin/server/Page.java @@ -391,7 +391,7 @@ public class Page implements Serializable { } public WebBrowser getWebBrowser() { - return uI.getApplication().getContext().getBrowser(); + return uI.getApplication().getBrowser(); } public void setBrowserWindowSize(int width, int height) { diff --git a/server/src/com/vaadin/server/PortletApplicationContext2.java b/server/src/com/vaadin/server/PortletApplicationContext2.java index a7b6d5b40a..ff23d12f4c 100644 --- a/server/src/com/vaadin/server/PortletApplicationContext2.java +++ b/server/src/com/vaadin/server/PortletApplicationContext2.java @@ -41,6 +41,7 @@ import javax.portlet.StateAwareResponse; import javax.servlet.http.HttpSessionBindingListener; import javax.xml.namespace.QName; +import com.vaadin.Application; import com.vaadin.ui.UI; import com.vaadin.util.CurrentInstance; @@ -53,7 +54,7 @@ import com.vaadin.util.CurrentInstance; * @author peholmst */ @SuppressWarnings("serial") -public class PortletApplicationContext2 extends ApplicationContext { +public class PortletApplicationContext2 extends Application { private final Set portletListeners = new LinkedHashSet(); @@ -63,26 +64,6 @@ public class PortletApplicationContext2 extends ApplicationContext { private final Map sharedParameterActionNameMap = new HashMap(); private final Map sharedParameterActionValueMap = new HashMap(); - public static PortletApplicationContext2 getApplicationContext( - PortletSession session) { - Object cxattr = session.getAttribute(PortletApplicationContext2.class - .getName()); - PortletApplicationContext2 cx = null; - // can be false also e.g. if old context comes from another - // classloader when using - // false - // and redeploying the portlet - see #7461 - if (cxattr instanceof PortletApplicationContext2) { - cx = (PortletApplicationContext2) cxattr; - } - if (cx == null) { - cx = new PortletApplicationContext2(); - session.setAttribute(PortletApplicationContext2.class.getName(), cx); - } - cx.setSession(new WrappedPortletSession(session)); - return cx; - } - public PortletSession getPortletSession() { WrappedSession wrappedSession = getSession(); PortletSession session = ((WrappedPortletSession) wrappedSession) diff --git a/server/src/com/vaadin/server/PortletCommunicationManager.java b/server/src/com/vaadin/server/PortletCommunicationManager.java index f7214f108c..ada42140c6 100644 --- a/server/src/com/vaadin/server/PortletCommunicationManager.java +++ b/server/src/com/vaadin/server/PortletCommunicationManager.java @@ -158,7 +158,7 @@ public class PortletCommunicationManager extends AbstractCommunicationManager { protected InputStream getThemeResourceAsStream(UI uI, String themeName, String resource) { PortletApplicationContext2 context = (PortletApplicationContext2) uI - .getApplication().getContext(); + .getApplication(); PortletContext portletContext = context.getPortletSession() .getPortletContext(); return portletContext.getResourceAsStream("/" diff --git a/server/src/com/vaadin/server/RequestTimer.java b/server/src/com/vaadin/server/RequestTimer.java index 470677e331..afff901d2f 100644 --- a/server/src/com/vaadin/server/RequestTimer.java +++ b/server/src/com/vaadin/server/RequestTimer.java @@ -18,6 +18,7 @@ package com.vaadin.server; import java.io.Serializable; +import com.vaadin.Application; /** * Times the handling of requests and stores the information as an attribute in @@ -44,7 +45,7 @@ public class RequestTimer implements Serializable { * * @param context */ - public void stop(ApplicationContext context) { + public void stop(Application context) { // Measure and store the total handling time. This data can be // used in TestBench 3 tests. long time = (System.nanoTime() - requestStartTime) / 1000000; diff --git a/server/src/com/vaadin/server/ServletApplicationContext.java b/server/src/com/vaadin/server/ServletApplicationContext.java index a1f5a81624..0945de6cb5 100644 --- a/server/src/com/vaadin/server/ServletApplicationContext.java +++ b/server/src/com/vaadin/server/ServletApplicationContext.java @@ -23,6 +23,7 @@ import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; +import com.vaadin.Application; import com.vaadin.util.CurrentInstance; /** @@ -35,18 +36,10 @@ import com.vaadin.util.CurrentInstance; * @since 3.1 */ @SuppressWarnings("serial") -public class ServletApplicationContext extends ApplicationContext { +public class ServletApplicationContext extends Application { private transient boolean reinitializingSession = false; - /** - * Creates a new Web Application Context. - * - */ - protected ServletApplicationContext() { - - } - @Override public void valueUnbound(HttpSessionBindingEvent event) { if (!reinitializingSession) { @@ -62,7 +55,6 @@ public class ServletApplicationContext extends ApplicationContext { * contents. The purpose of this is to introduce a new session key in order * to avoid session fixation attacks. */ - @SuppressWarnings("unchecked") public void reinitializeSession() { HttpSession oldSession = getHttpSession(); @@ -93,7 +85,7 @@ public class ServletApplicationContext extends ApplicationContext { } // Update the "current session" variable - setSession(new WrappedHttpSession(newSession)); + storeInSession(new WrappedHttpSession(newSession)); } /** @@ -106,22 +98,4 @@ public class ServletApplicationContext extends ApplicationContext { return ((WrappedHttpSession) session).getHttpSession(); } - /** - * Gets the application context for an HttpSession. - * - * @param session - * the HTTP session. - * @return the application context for HttpSession. - */ - static public ServletApplicationContext getApplicationContext( - HttpSession session) { - ServletApplicationContext cx = (ServletApplicationContext) session - .getAttribute(ServletApplicationContext.class.getName()); - if (cx == null) { - cx = new ServletApplicationContext(); - session.setAttribute(ServletApplicationContext.class.getName(), cx); - } - cx.setSession(new WrappedHttpSession(session)); - return cx; - } } diff --git a/server/src/com/vaadin/server/VaadinPortlet.java b/server/src/com/vaadin/server/VaadinPortlet.java index eae01e9369..e69a635b76 100644 --- a/server/src/com/vaadin/server/VaadinPortlet.java +++ b/server/src/com/vaadin/server/VaadinPortlet.java @@ -474,8 +474,7 @@ public class VaadinPortlet extends GenericPortlet implements Constants { * Get or create an application context and an application * manager for the session */ - PortletApplicationContext2 applicationContext = getApplicationContext(request - .getPortletSession()); + PortletApplicationContext2 applicationContext = (PortletApplicationContext2) application; PortletCommunicationManager applicationManager = (PortletCommunicationManager) applicationContext .getApplicationManager(); @@ -494,8 +493,6 @@ public class VaadinPortlet extends GenericPortlet implements Constants { applicationContext.getBrowser().updateRequestDetails( wrappedRequest); - /* Start the newly created application */ - startApplication(request, application, applicationContext); applicationRunning = true; /* Notify listeners */ @@ -596,9 +593,8 @@ public class VaadinPortlet extends GenericPortlet implements Constants { CurrentInstance.clearAll(); - PortletSession session = request.getPortletSession(false); - if (session != null) { - requestTimer.stop(getApplicationContext(session)); + if (application != null) { + requestTimer.stop(application); } } } @@ -764,27 +760,10 @@ public class VaadinPortlet extends GenericPortlet implements Constants { && (request.getParameter(URL_PARAMETER_REPAINT_ALL).equals("1")); } - private void startApplication(PortletRequest request, - Application application, PortletApplicationContext2 context) - throws PortletException, MalformedURLException { - if (!application.isRunning()) { - Locale locale = request.getLocale(); - application.setLocale(locale); - // No application URL when running inside a portlet - application.start(new ApplicationStartEvent(null, - getDeploymentConfiguration().getApplicationConfiguration(), - context)); - addonContext.fireApplicationStarted(application); - } - } - private void endApplication(PortletRequest request, PortletResponse response, Application application) throws IOException { - final PortletSession session = request.getPortletSession(); - if (session != null) { - getApplicationContext(session).removeApplication(); - } + application.removeFromSession(); // Do not send any redirects when running inside a portlet. } @@ -839,10 +818,7 @@ public class VaadinPortlet extends GenericPortlet implements Constants { } application.close(); - if (session != null) { - PortletApplicationContext2 context = getApplicationContext(session); - context.removeApplication(); - } + application.removeFromSession(); } private Application createAndRegisterApplication(PortletRequest request) @@ -855,16 +831,23 @@ public class VaadinPortlet extends GenericPortlet implements Constants { throw new PortletException(e); } - final PortletApplicationContext2 context = getApplicationContext(request - .getPortletSession()); - context.setApplication(newApplication, new PortletCommunicationManager( - newApplication)); + newApplication.storeInSession(new WrappedPortletSession(request + .getPortletSession())); + + Locale locale = request.getLocale(); + newApplication.setLocale(locale); + // No application URL when running inside a portlet + newApplication.start(new ApplicationStartEvent(null, + getDeploymentConfiguration().getApplicationConfiguration(), + new PortletCommunicationManager(newApplication))); + addonContext.fireApplicationStarted(newApplication); + return newApplication; } - protected Application createApplication(PortletRequest request) - throws PortletException { - Application application = new Application(); + protected PortletApplicationContext2 createApplication( + PortletRequest request) throws PortletException { + PortletApplicationContext2 application = new PortletApplicationContext2(); try { ServletPortletHelper.initDefaultUIProvider(application, @@ -887,18 +870,17 @@ public class VaadinPortlet extends GenericPortlet implements Constants { throw new SessionExpiredException(); } - PortletApplicationContext2 context = getApplicationContext(session); - Application application = context.getApplication(); + Application application = Application + .getForSession(new WrappedPortletSession(session)); if (application == null) { return null; } - if (application.isRunning()) { - return application; + if (!application.isRunning()) { + application.removeFromSession(); + return null; } - // application found but not running - context.removeApplication(); - return null; + return application; } private void handleServiceException(WrappedPortletRequest request, @@ -1003,21 +985,6 @@ public class VaadinPortlet extends GenericPortlet implements Constants { outWriter.close(); } - /** - * - * Gets the application context for a PortletSession. If no context is - * currently stored in a session a new context is created and stored in the - * session. - * - * @param portletSession - * the portlet session. - * @return the application context for the session. - */ - protected PortletApplicationContext2 getApplicationContext( - PortletSession portletSession) { - return PortletApplicationContext2.getApplicationContext(portletSession); - } - private static final Logger getLogger() { return Logger.getLogger(VaadinPortlet.class.getName()); } diff --git a/server/src/com/vaadin/server/VaadinServlet.java b/server/src/com/vaadin/server/VaadinServlet.java index 8f33382110..eb21096c9b 100644 --- a/server/src/com/vaadin/server/VaadinServlet.java +++ b/server/src/com/vaadin/server/VaadinServlet.java @@ -318,8 +318,7 @@ public class VaadinServlet extends HttpServlet implements Constants { * Get or create a WebApplicationContext and an ApplicationManager * for the session */ - ServletApplicationContext webApplicationContext = getApplicationContext(request - .getSession()); + ServletApplicationContext webApplicationContext = (ServletApplicationContext) application; CommunicationManager applicationManager = (CommunicationManager) webApplicationContext .getApplicationManager(); @@ -335,8 +334,6 @@ public class VaadinServlet extends HttpServlet implements Constants { /* Update browser information from the request */ webApplicationContext.getBrowser().updateRequestDetails(request); - // Start the application if it's newly created - startApplication(request, application, webApplicationContext); applicationRunning = true; /* Handle the request */ @@ -388,9 +385,8 @@ public class VaadinServlet extends HttpServlet implements Constants { CurrentInstance.clearAll(); - HttpSession session = request.getSession(false); - if (session != null) { - requestTimer.stop(getApplicationContext(session)); + if (application != null) { + requestTimer.stop(application); } } } @@ -631,7 +627,7 @@ public class VaadinServlet extends HttpServlet implements Constants { } private Application createAndRegisterApplication(HttpServletRequest request) - throws ServletException { + throws ServletException, MalformedURLException { Application newApplication = createApplication(request); try { @@ -640,10 +636,19 @@ public class VaadinServlet extends HttpServlet implements Constants { throw new ServletException(e); } - final ServletApplicationContext context = getApplicationContext(request - .getSession()); - context.setApplication(newApplication, - createCommunicationManager(newApplication)); + newApplication.storeInSession(new WrappedHttpSession(request + .getSession())); + + final URL applicationUrl = getApplicationUrl(request); + + // Initial locale comes from the request + Locale locale = request.getLocale(); + newApplication.setLocale(locale); + newApplication.start(new ApplicationStartEvent(applicationUrl, + getDeploymentConfiguration().getApplicationConfiguration(), + createCommunicationManager(newApplication))); + + addonContext.fireApplicationStarted(newApplication); return newApplication; } @@ -717,9 +722,9 @@ public class VaadinServlet extends HttpServlet implements Constants { * @throws ServletException * @throws MalformedURLException */ - protected Application createApplication(HttpServletRequest request) - throws ServletException { - Application newApplication = new Application(); + protected ServletApplicationContext createApplication( + HttpServletRequest request) throws ServletException { + ServletApplicationContext newApplication = new ServletApplicationContext(); try { ServletPortletHelper.initDefaultUIProvider(newApplication, @@ -868,34 +873,6 @@ public class VaadinServlet extends HttpServlet implements Constants { log("Invalid security key received from " + request.getRemoteHost()); } - /** - * Starts the application if it is not already running. - * - * @param request - * @param application - * @param webApplicationContext - * @throws ServletException - * @throws MalformedURLException - */ - private void startApplication(HttpServletRequest request, - Application application, - ServletApplicationContext webApplicationContext) - throws ServletException, MalformedURLException { - - if (!application.isRunning()) { - // Create application - final URL applicationUrl = getApplicationUrl(request); - - // Initial locale comes from the request - Locale locale = request.getLocale(); - application.setLocale(locale); - application.start(new ApplicationStartEvent(applicationUrl, - getDeploymentConfiguration().getApplicationConfiguration(), - webApplicationContext)); - addonContext.fireApplicationStarted(application); - } - } - /** * Check if this is a request for a static resource and, if it is, serve the * resource to the client. @@ -1303,24 +1280,18 @@ public class VaadinServlet extends HttpServlet implements Constants { throw new SessionExpiredException(); } - ServletApplicationContext context = getApplicationContext(session); - - Application sessionApplication = context.getApplication(); + Application sessionApplication = getApplicationContext(session); if (sessionApplication == null) { return null; } - if (sessionApplication.isRunning()) { - // Found a running application - return sessionApplication; + if (!sessionApplication.isRunning()) { + sessionApplication.removeFromSession(); + return null; } - // Application has stopped, so remove it before creating a new - // application - getApplicationContext(session).removeApplication(); - // Existing application not found - return null; + return sessionApplication; } /** @@ -1346,7 +1317,7 @@ public class VaadinServlet extends HttpServlet implements Constants { final HttpSession session = request.getSession(); if (session != null) { - getApplicationContext(session).removeApplication(); + application.removeFromSession(); } response.sendRedirect(response.encodeRedirectURL(logoutUrl)); @@ -1398,29 +1369,14 @@ public class VaadinServlet extends HttpServlet implements Constants { application.close(); if (session != null) { - ServletApplicationContext context = getApplicationContext(session); - context.removeApplication(); + application.removeFromSession(); } } - /** - * - * Gets the application context from an HttpSession. If no context is - * currently stored in a session a new context is created and stored in the - * session. - * - * @param session - * the HTTP session. - * @return the application context for HttpSession. - */ - protected ServletApplicationContext getApplicationContext( - HttpSession session) { - /* - * TODO the ApplicationContext.getApplicationContext() should be removed - * and logic moved here. Now overriding context type is possible, but - * the whole creation logic should be here. MT 1101 - */ - return ServletApplicationContext.getApplicationContext(session); + protected Application getApplicationContext(final HttpSession session) { + Application sessionApplication = Application + .getForSession(new WrappedHttpSession(session)); + return sessionApplication; } public class RequestError implements Terminal.ErrorEvent, Serializable { diff --git a/server/src/com/vaadin/server/WrappedHttpServletRequest.java b/server/src/com/vaadin/server/WrappedHttpServletRequest.java index 9285f92035..05d9b13736 100644 --- a/server/src/com/vaadin/server/WrappedHttpServletRequest.java +++ b/server/src/com/vaadin/server/WrappedHttpServletRequest.java @@ -89,9 +89,7 @@ public class WrappedHttpServletRequest extends HttpServletRequestWrapper @Override public WebBrowser getWebBrowser() { - ApplicationContext context = Application.getCurrent() - .getContext(); - return context.getBrowser(); + return Application.getCurrent().getBrowser(); } }; } diff --git a/server/src/com/vaadin/server/WrappedPortletRequest.java b/server/src/com/vaadin/server/WrappedPortletRequest.java index 2d444d86ca..dffbebb379 100644 --- a/server/src/com/vaadin/server/WrappedPortletRequest.java +++ b/server/src/com/vaadin/server/WrappedPortletRequest.java @@ -152,7 +152,7 @@ public class WrappedPortletRequest implements WrappedRequest { @Override public WebBrowser getWebBrowser() { PortletApplicationContext2 context = (PortletApplicationContext2) Application - .getCurrent().getContext(); + .getCurrent(); return context.getBrowser(); } }; diff --git a/server/tests/src/com/vaadin/tests/server/TestMimeTypes.java b/server/tests/src/com/vaadin/tests/server/TestMimeTypes.java index dc730f6cc4..12e5b09632 100644 --- a/server/tests/src/com/vaadin/tests/server/TestMimeTypes.java +++ b/server/tests/src/com/vaadin/tests/server/TestMimeTypes.java @@ -2,21 +2,12 @@ package com.vaadin.tests.server; import junit.framework.TestCase; -import com.vaadin.Application; import com.vaadin.server.ClassResource; import com.vaadin.ui.Embedded; public class TestMimeTypes extends TestCase { public void testEmbeddedPDF() { - Application app = new Application() { - - @Override - public void init() { - // TODO Auto-generated method stub - - } - }; Embedded e = new Embedded("A pdf", new ClassResource("file.pddf")); assertEquals("Invalid mimetype", "application/octet-stream", e.getMimeType()); diff --git a/server/tests/src/com/vaadin/tests/server/TestStreamVariableMapping.java b/server/tests/src/com/vaadin/tests/server/TestStreamVariableMapping.java index f3b367483a..775348fb5c 100644 --- a/server/tests/src/com/vaadin/tests/server/TestStreamVariableMapping.java +++ b/server/tests/src/com/vaadin/tests/server/TestStreamVariableMapping.java @@ -66,12 +66,7 @@ public class TestStreamVariableMapping extends TestCase { } private CommunicationManager createCommunicationManager() { - return new CommunicationManager(new Application() { - @Override - public void init() { - // TODO Auto-generated method stub - } - }); + return new CommunicationManager(new Application()); } } diff --git a/uitest/src/com/vaadin/launcher/ApplicationRunnerServlet.java b/uitest/src/com/vaadin/launcher/ApplicationRunnerServlet.java index a9a988f296..34c1cc1fce 100644 --- a/uitest/src/com/vaadin/launcher/ApplicationRunnerServlet.java +++ b/uitest/src/com/vaadin/launcher/ApplicationRunnerServlet.java @@ -34,6 +34,7 @@ import com.vaadin.LegacyApplication; import com.vaadin.server.AbstractUIProvider; import com.vaadin.server.ApplicationConfiguration; import com.vaadin.server.LegacyVaadinServlet; +import com.vaadin.server.ServletApplicationContext; import com.vaadin.server.UIProvider; import com.vaadin.server.WrappedHttpServletRequest; import com.vaadin.server.WrappedRequest; @@ -112,12 +113,12 @@ public class ApplicationRunnerServlet extends LegacyVaadinServlet { } @Override - protected Application createApplication(HttpServletRequest request) - throws ServletException { + protected ServletApplicationContext createApplication( + HttpServletRequest request) throws ServletException { try { final Class classToRun = getClassToRun(); if (UI.class.isAssignableFrom(classToRun)) { - Application application = new Application(); + ServletApplicationContext application = new ServletApplicationContext(); application.addUIProvider(new AbstractUIProvider() { @Override @@ -130,7 +131,7 @@ public class ApplicationRunnerServlet extends LegacyVaadinServlet { } else if (LegacyApplication.class.isAssignableFrom(classToRun)) { return super.createApplication(request); } else if (UIProvider.class.isAssignableFrom(classToRun)) { - Application application = new Application(); + ServletApplicationContext application = new ServletApplicationContext(); application .addUIProvider((UIProvider) classToRun.newInstance()); return application; diff --git a/uitest/src/com/vaadin/tests/VerifyBrowserVersion.java b/uitest/src/com/vaadin/tests/VerifyBrowserVersion.java index f4cf236a24..022a343196 100644 --- a/uitest/src/com/vaadin/tests/VerifyBrowserVersion.java +++ b/uitest/src/com/vaadin/tests/VerifyBrowserVersion.java @@ -1,6 +1,5 @@ package com.vaadin.tests; -import com.vaadin.server.ApplicationContext; import com.vaadin.server.WebBrowser; import com.vaadin.tests.components.TestBase; import com.vaadin.ui.Label; @@ -9,8 +8,7 @@ public class VerifyBrowserVersion extends TestBase { @Override protected void setup() { - ApplicationContext context = getContext(); - WebBrowser browser = context.getBrowser(); + WebBrowser browser = getBrowser(); addComponent(new Label(browser.getBrowserApplication())); addComponent(new Label("Touch device? " + (browser.isTouchDevice() ? "YES" : "No"))); diff --git a/uitest/src/com/vaadin/tests/application/ApplicationCloseTest.java b/uitest/src/com/vaadin/tests/application/ApplicationCloseTest.java index f2fe90f4b1..7250ba3cfb 100644 --- a/uitest/src/com/vaadin/tests/application/ApplicationCloseTest.java +++ b/uitest/src/com/vaadin/tests/application/ApplicationCloseTest.java @@ -14,9 +14,9 @@ public class ApplicationCloseTest extends TestBase { protected void setup() { Label applications = new Label("Applications in session:
", ContentMode.XHTML); - if (getContext().getApplication() != null) { + if (getContext() != null) { applications.setValue(applications.getValue() + "App: " - + getContext().getApplication() + "
"); + + getContext() + "
"); } applications.setValue(applications.getValue() + "

"); diff --git a/uitest/src/com/vaadin/tests/components/AbstractTestCase.java b/uitest/src/com/vaadin/tests/components/AbstractTestCase.java index 6f539af016..5dc5fd7128 100644 --- a/uitest/src/com/vaadin/tests/components/AbstractTestCase.java +++ b/uitest/src/com/vaadin/tests/components/AbstractTestCase.java @@ -1,7 +1,7 @@ package com.vaadin.tests.components; +import com.vaadin.Application; import com.vaadin.LegacyApplication; -import com.vaadin.server.ApplicationContext; import com.vaadin.server.WebBrowser; public abstract class AbstractTestCase extends LegacyApplication { @@ -11,8 +11,7 @@ public abstract class AbstractTestCase extends LegacyApplication { protected abstract Integer getTicketNumber(); protected WebBrowser getBrowser() { - ApplicationContext context = getContext(); - WebBrowser webBrowser = context.getBrowser(); + WebBrowser webBrowser = Application.getCurrent().getBrowser(); return webBrowser; } diff --git a/uitest/src/com/vaadin/tests/components/AbstractTestUI.java b/uitest/src/com/vaadin/tests/components/AbstractTestUI.java index 21eda56891..92ac336df7 100644 --- a/uitest/src/com/vaadin/tests/components/AbstractTestUI.java +++ b/uitest/src/com/vaadin/tests/components/AbstractTestUI.java @@ -1,7 +1,5 @@ package com.vaadin.tests.components; -import com.vaadin.Application; -import com.vaadin.server.ApplicationContext; import com.vaadin.server.WebBrowser; import com.vaadin.server.WrappedRequest; import com.vaadin.shared.ui.label.ContentMode; @@ -56,9 +54,7 @@ public abstract class AbstractTestUI extends UI { protected abstract Integer getTicketNumber(); protected WebBrowser getBrowser() { - ApplicationContext context = Application.getCurrent().getContext(); - ApplicationContext webContext = context; - return webContext.getBrowser(); + return getApplication().getBrowser(); } } diff --git a/uitest/src/com/vaadin/tests/components/AbstractTestUIProvider.java b/uitest/src/com/vaadin/tests/components/AbstractTestUIProvider.java index 3d380c7835..4311cd6a0f 100644 --- a/uitest/src/com/vaadin/tests/components/AbstractTestUIProvider.java +++ b/uitest/src/com/vaadin/tests/components/AbstractTestUIProvider.java @@ -2,7 +2,6 @@ package com.vaadin.tests.components; import com.vaadin.Application; import com.vaadin.server.AbstractUIProvider; -import com.vaadin.server.ApplicationContext; import com.vaadin.server.WebBrowser; public abstract class AbstractTestUIProvider extends AbstractUIProvider { @@ -11,8 +10,7 @@ public abstract class AbstractTestUIProvider extends AbstractUIProvider { protected abstract Integer getTicketNumber(); protected WebBrowser getBrowser() { - ApplicationContext context = Application.getCurrent().getContext(); - WebBrowser webBrowser = context.getBrowser(); + WebBrowser webBrowser = Application.getCurrent().getBrowser(); return webBrowser; } }