]> source.dussan.org Git - vaadin-framework.git/commitdiff
Redesign to make session inited in Application.init (#9402)
authorLeif Åstrand <leif@vaadin.com>
Fri, 7 Sep 2012 07:07:42 +0000 (10:07 +0300)
committerLeif Åstrand <leif@vaadin.com>
Fri, 7 Sep 2012 07:07:42 +0000 (10:07 +0300)
server/src/com/vaadin/server/LegacyVaadinPortlet.java
server/src/com/vaadin/server/LegacyVaadinServlet.java
server/src/com/vaadin/server/VaadinPortlet.java
server/src/com/vaadin/server/VaadinServlet.java
uitest/src/com/vaadin/launcher/ApplicationRunnerServlet.java

index 0f71f102c7de6ecf9260b874ca3b53a121b6ab74..efc341f36972ce19b2f7567c4593743baf51f0b7 100644 (file)
@@ -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;
     }
 }
index a631ae831981cb990785fa9139abb82db5e935de..359f18905c9066ae3d8c2e34d0d7897e51fc577c 100644 (file)
@@ -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);
     }
 
 }
index 4bd68bde66e1427df59e6057408422df8c7bd553..1e74cda833b79b0bf835f50532fd37cbce14b38b 100644 (file)
@@ -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 {
index cbf576565886ad74f4f77bb544913fa59394fe7a..12e0d053794f3cf3a9cb96941e3e0b8b17d79ce6 100644 (file)
@@ -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,
index 029bebd9c9f0e481b8164577d2cb74db9db1fe83..df7c9c5db93b191a5b83a6857147e22b981483fa 100644 (file)
@@ -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(