diff options
author | Leif Åstrand <leif@vaadin.com> | 2012-09-07 10:07:42 +0300 |
---|---|---|
committer | Leif Åstrand <leif@vaadin.com> | 2012-09-07 10:07:42 +0300 |
commit | 73f50d5027288b4358880a638911bec276384a34 (patch) | |
tree | 308d97749fcf2add11cce2d8d96cf48fce41e47d | |
parent | 0cffb22cf4f746746b1398a88a26b7bd572e84b4 (diff) | |
download | vaadin-framework-73f50d5027288b4358880a638911bec276384a34.tar.gz vaadin-framework-73f50d5027288b4358880a638911bec276384a34.zip |
Redesign to make session inited in Application.init (#9402)
5 files changed, 98 insertions, 64 deletions
diff --git a/server/src/com/vaadin/server/LegacyVaadinPortlet.java b/server/src/com/vaadin/server/LegacyVaadinPortlet.java index 0f71f102c7..efc341f369 100644 --- a/server/src/com/vaadin/server/LegacyVaadinPortlet.java +++ b/server/src/com/vaadin/server/LegacyVaadinPortlet.java @@ -45,17 +45,25 @@ public class LegacyVaadinPortlet extends VaadinPortlet { } @Override - protected VaadinPortletSession createApplication(PortletRequest request) - throws PortletException { - VaadinPortletSession application = super.createApplication(request); + protected void onVaadinSessionStarted(WrappedPortletRequest request, + VaadinPortletSession session) throws PortletException { + if (shouldCreateApplication(request)) { + // Must set current before running init() + VaadinSession.setCurrent(session); + + // XXX Must update details here so they are available in init. + session.getBrowser().updateRequestDetails(request); - // Must set current before running init() - VaadinSession.setCurrent(application); + Application legacyApplication = getNewApplication(request + .getPortletRequest()); + legacyApplication.doInit(); + session.addUIProvider(legacyApplication); + } - Application legacyApplication = getNewApplication(request); - legacyApplication.doInit(); - application.addUIProvider(legacyApplication); + super.onVaadinSessionStarted(request, session); + } - return application; + protected boolean shouldCreateApplication(WrappedPortletRequest request) { + return true; } } diff --git a/server/src/com/vaadin/server/LegacyVaadinServlet.java b/server/src/com/vaadin/server/LegacyVaadinServlet.java index a631ae8319..359f18905c 100644 --- a/server/src/com/vaadin/server/LegacyVaadinServlet.java +++ b/server/src/com/vaadin/server/LegacyVaadinServlet.java @@ -44,19 +44,28 @@ public class LegacyVaadinServlet extends VaadinServlet { } } - @Override - protected VaadinServletSession createApplication(HttpServletRequest request) + protected boolean shouldCreateApplication(WrappedHttpServletRequest request) throws ServletException { - VaadinServletSession application = super.createApplication(request); + return true; + } + + @Override + protected void onVaadinSessionStarted(WrappedHttpServletRequest request, + VaadinServletSession session) throws ServletException { + + if (shouldCreateApplication(request)) { + // Must set current before running init() + VaadinSession.setCurrent(session); - // Must set current before running init() - VaadinSession.setCurrent(application); + // XXX Must update details here so they are available in init. + session.getBrowser().updateRequestDetails(request); - Application legacyApplication = getNewApplication(request); - legacyApplication.doInit(); - application.addUIProvider(legacyApplication); + Application legacyApplication = getNewApplication(request); + legacyApplication.doInit(); + session.addUIProvider(legacyApplication); + } - return application; + super.onVaadinSessionStarted(request, session); } } diff --git a/server/src/com/vaadin/server/VaadinPortlet.java b/server/src/com/vaadin/server/VaadinPortlet.java index 4bd68bde66..1e74cda833 100644 --- a/server/src/com/vaadin/server/VaadinPortlet.java +++ b/server/src/com/vaadin/server/VaadinPortlet.java @@ -783,7 +783,7 @@ public class VaadinPortlet extends GenericPortlet implements Constants { if (restartApplication) { closeApplication(application, request.getPortletSession(false)); - return createAndRegisterApplication(request); + return createAndRegisterApplication(wrappedRequest); } else if (closeApplication) { closeApplication(application, request.getPortletSession(false)); return null; @@ -795,7 +795,7 @@ public class VaadinPortlet extends GenericPortlet implements Constants { // No existing application was found if (requestCanCreateApplication) { - return createAndRegisterApplication(request); + return createAndRegisterApplication(wrappedRequest); } else { throw new SessionExpiredException(); } @@ -811,9 +811,9 @@ public class VaadinPortlet extends GenericPortlet implements Constants { application.removeFromSession(); } - private VaadinSession createAndRegisterApplication(PortletRequest request) - throws PortletException { - VaadinSession newApplication = createApplication(request); + private VaadinSession createAndRegisterApplication( + WrappedPortletRequest request) throws PortletException { + VaadinPortletSession newApplication = createApplication(); try { ServletPortletHelper.checkUiProviders(newApplication); @@ -822,7 +822,7 @@ public class VaadinPortlet extends GenericPortlet implements Constants { } newApplication.storeInSession(new WrappedPortletSession(request - .getPortletSession())); + .getPortletRequest().getPortletSession())); Locale locale = request.getLocale(); newApplication.setLocale(locale); @@ -830,13 +830,22 @@ public class VaadinPortlet extends GenericPortlet implements Constants { newApplication.start(new SessionStartEvent(null, getVaadinService() .getDeploymentConfiguration(), new PortletCommunicationManager( newApplication))); - addonContext.fireApplicationStarted(newApplication); + onVaadinSessionStarted(request, newApplication); return newApplication; } - protected VaadinPortletSession createApplication(PortletRequest request) - throws PortletException { + protected void onVaadinSessionStarted(WrappedPortletRequest request, + VaadinPortletSession session) throws PortletException { + addonContext.fireApplicationStarted(session); + try { + ServletPortletHelper.checkUiProviders(session); + } catch (ApplicationClassException e) { + throw new PortletException(e); + } + } + + private VaadinPortletSession createApplication() throws PortletException { VaadinPortletSession application = new VaadinPortletSession(); try { diff --git a/server/src/com/vaadin/server/VaadinServlet.java b/server/src/com/vaadin/server/VaadinServlet.java index cbf5765658..12e0d05379 100644 --- a/server/src/com/vaadin/server/VaadinServlet.java +++ b/server/src/com/vaadin/server/VaadinServlet.java @@ -569,9 +569,10 @@ public class VaadinServlet extends HttpServlet implements Constants { * @throws ServletException * @throws SessionExpiredException */ - private VaadinSession findApplicationInstance(HttpServletRequest request, - RequestType requestType) throws MalformedURLException, - ServletException, SessionExpiredException { + private VaadinSession findApplicationInstance( + WrappedHttpServletRequest request, RequestType requestType) + throws MalformedURLException, ServletException, + SessionExpiredException { boolean requestCanCreateApplication = requestCanCreateApplication( request, requestType); @@ -621,31 +622,35 @@ public class VaadinServlet extends HttpServlet implements Constants { } private VaadinSession createAndRegisterApplication( - HttpServletRequest request) throws ServletException, + WrappedHttpServletRequest request) throws ServletException, MalformedURLException { - VaadinSession newApplication = createApplication(request); + VaadinServletSession session = createVaadinSession(request); - try { - ServletPortletHelper.checkUiProviders(newApplication); - } catch (ApplicationClassException e) { - throw new ServletException(e); - } - - newApplication.storeInSession(new WrappedHttpSession(request - .getSession())); + session.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 SessionStartEvent(applicationUrl, - getVaadinService().getDeploymentConfiguration(), - createCommunicationManager(newApplication))); + session.setLocale(locale); + session.start(new SessionStartEvent(applicationUrl, getVaadinService() + .getDeploymentConfiguration(), + createCommunicationManager(session))); - addonContext.fireApplicationStarted(newApplication); + onVaadinSessionStarted(request, session); - return newApplication; + return session; + } + + protected void onVaadinSessionStarted(WrappedHttpServletRequest request, + VaadinServletSession session) throws ServletException { + addonContext.fireApplicationStarted(session); + + try { + ServletPortletHelper.checkUiProviders(session); + } catch (ApplicationClassException e) { + throw new ServletException(e); + } } /** @@ -708,27 +713,25 @@ public class VaadinServlet extends HttpServlet implements Constants { } /** - * Creates a new application and registers it into WebApplicationContext - * (aka session). This is not meant to be overridden. Override - * getNewApplication to create the application instance in a custom way. + * Creates a new vaadin session. * * @param request * @return * @throws ServletException * @throws MalformedURLException */ - protected VaadinServletSession createApplication(HttpServletRequest request) + private VaadinServletSession createVaadinSession(HttpServletRequest request) throws ServletException { - VaadinServletSession newApplication = new VaadinServletSession(); + VaadinServletSession session = new VaadinServletSession(); try { - ServletPortletHelper.initDefaultUIProvider(newApplication, + ServletPortletHelper.initDefaultUIProvider(session, getVaadinService()); } catch (ApplicationClassException e) { throw new ServletException(e); } - return newApplication; + return session; } private void handleServiceException(WrappedHttpServletRequest request, diff --git a/uitest/src/com/vaadin/launcher/ApplicationRunnerServlet.java b/uitest/src/com/vaadin/launcher/ApplicationRunnerServlet.java index 029bebd9c9..df7c9c5db9 100644 --- a/uitest/src/com/vaadin/launcher/ApplicationRunnerServlet.java +++ b/uitest/src/com/vaadin/launcher/ApplicationRunnerServlet.java @@ -111,28 +111,32 @@ public class ApplicationRunnerServlet extends LegacyVaadinServlet { } @Override - protected VaadinServletSession createApplication(HttpServletRequest request) + protected boolean shouldCreateApplication(WrappedHttpServletRequest request) throws ServletException { try { + return Application.class.isAssignableFrom(getClassToRun()); + } catch (ClassNotFoundException e) { + throw new ServletException(e); + } + } + + @Override + protected void onVaadinSessionStarted(WrappedHttpServletRequest request, + VaadinServletSession session) throws ServletException { + try { final Class<?> classToRun = getClassToRun(); if (UI.class.isAssignableFrom(classToRun)) { - VaadinServletSession application = new VaadinServletSession(); - application.addUIProvider(new AbstractUIProvider() { + session.addUIProvider(new AbstractUIProvider() { @Override - public Class<? extends UI> getUIClass( - WrappedRequest request) { + public Class<? extends UI> getUIClass(WrappedRequest request) { return (Class<? extends UI>) classToRun; } }); - return application; } else if (Application.class.isAssignableFrom(classToRun)) { - return super.createApplication(request); + // Avoid using own UIProvider for legacy Application } else if (UIProvider.class.isAssignableFrom(classToRun)) { - VaadinServletSession application = new VaadinServletSession(); - application - .addUIProvider((UIProvider) classToRun.newInstance()); - return application; + session.addUIProvider((UIProvider) classToRun.newInstance()); } else { throw new ServletException(classToRun.getCanonicalName() + " is neither an Application nor a UI"); @@ -148,6 +152,7 @@ public class ApplicationRunnerServlet extends LegacyVaadinServlet { + getApplicationRunnerApplicationClassName(request))); } + super.onVaadinSessionStarted(request, session); } private String getApplicationRunnerApplicationClassName( |