]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix serialization with ApplicationRunnerServlet
authorLeif Åstrand <leif@vaadin.com>
Tue, 30 Dec 2014 08:22:44 +0000 (10:22 +0200)
committerVaadin Code Review <review@vaadin.com>
Wed, 7 Jan 2015 08:09:32 +0000 (08:09 +0000)
Make the InvocationHandler used by the DeploymentConfiguration proxying
feature serializable. Also make findDeploymentConfiguration static to
avoid serializing an ApplicatinRunnerServlet instance.

Change-Id: I360276ae42a875e9227df34e8aabf8ce2a697bc2

uitest/src/com/vaadin/launcher/ApplicationRunnerServlet.java
uitest/src/com/vaadin/tests/components/ui/UISerializationTest.java

index 1f724955969bf250fec4598ea069d5e2d26b1604..1cbb1aa03974945c8ff8c7c79751a0fe70b1b9ad 100644 (file)
@@ -17,6 +17,7 @@ package com.vaadin.launcher;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.Serializable;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
@@ -48,7 +49,10 @@ import com.vaadin.server.SessionInitListener;
 import com.vaadin.server.UIClassSelectionEvent;
 import com.vaadin.server.UIProvider;
 import com.vaadin.server.VaadinRequest;
+import com.vaadin.server.VaadinService;
+import com.vaadin.server.VaadinServlet;
 import com.vaadin.server.VaadinServletRequest;
+import com.vaadin.server.VaadinServletService;
 import com.vaadin.server.VaadinSession;
 import com.vaadin.tests.components.TestBase;
 import com.vaadin.ui.UI;
@@ -184,6 +188,29 @@ public class ApplicationRunnerServlet extends LegacyVaadinServlet {
         return getApplicationRunnerURIs(request).applicationClassname;
     }
 
+    private final static class ProxyDeploymentConfiguration implements
+            InvocationHandler, Serializable {
+        private final DeploymentConfiguration originalConfiguration;
+
+        private ProxyDeploymentConfiguration(
+                DeploymentConfiguration originalConfiguration) {
+            this.originalConfiguration = originalConfiguration;
+        }
+
+        @Override
+        public Object invoke(Object proxy, Method method, Object[] args)
+                throws Throwable {
+            if (method.getDeclaringClass() == DeploymentConfiguration.class) {
+                // Find the configuration instance to delegate to
+                DeploymentConfiguration configuration = findDeploymentConfiguration(originalConfiguration);
+
+                return method.invoke(configuration, args);
+            } else {
+                return method.invoke(proxy, args);
+            }
+        }
+    }
+
     private static final class ApplicationRunnerUIProvider extends UIProvider {
         private final Class<?> classToRun;
 
@@ -309,23 +336,10 @@ public class ApplicationRunnerServlet extends LegacyVaadinServlet {
         return (DeploymentConfiguration) Proxy.newProxyInstance(
                 DeploymentConfiguration.class.getClassLoader(),
                 new Class[] { DeploymentConfiguration.class },
-                new InvocationHandler() {
-                    @Override
-                    public Object invoke(Object proxy, Method method,
-                            Object[] args) throws Throwable {
-                        if (method.getDeclaringClass() == DeploymentConfiguration.class) {
-                            // Find the configuration instance to delegate to
-                            DeploymentConfiguration configuration = findDeploymentConfiguration(originalConfiguration);
-
-                            return method.invoke(configuration, args);
-                        } else {
-                            return method.invoke(proxy, args);
-                        }
-                    }
-                });
+                new ProxyDeploymentConfiguration(originalConfiguration));
     }
 
-    private DeploymentConfiguration findDeploymentConfiguration(
+    private static DeploymentConfiguration findDeploymentConfiguration(
             DeploymentConfiguration originalConfiguration) throws Exception {
         // First level of cache
         DeploymentConfiguration configuration = CurrentInstance
@@ -344,16 +358,19 @@ public class ApplicationRunnerServlet extends LegacyVaadinServlet {
                  * request.
                  */
 
-                HttpServletRequest currentRequest = request.get();
+                HttpServletRequest currentRequest = VaadinServletService
+                        .getCurrentServletRequest();
                 if (currentRequest != null) {
                     HttpSession httpSession = currentRequest.getSession(false);
                     if (httpSession != null) {
                         Map<Class<?>, CurrentInstance> oldCurrent = CurrentInstance
                                 .setCurrent((VaadinSession) null);
                         try {
-                            session = getService().findVaadinSession(
-                                    new VaadinServletRequest(currentRequest,
-                                            getService()));
+                            VaadinServletService service = (VaadinServletService) VaadinService
+                                    .getCurrent();
+                            session = service
+                                    .findVaadinSession(new VaadinServletRequest(
+                                            currentRequest, service));
                         } finally {
                             /*
                              * Clear some state set by findVaadinSession to
@@ -377,9 +394,11 @@ public class ApplicationRunnerServlet extends LegacyVaadinServlet {
                             .getAttribute(name);
 
                     if (configuration == null) {
+                        ApplicationRunnerServlet servlet = (ApplicationRunnerServlet) VaadinServlet
+                                .getCurrent();
                         Class<?> classToRun;
                         try {
-                            classToRun = getClassToRun();
+                            classToRun = servlet.getClassToRun();
                         } catch (ClassNotFoundException e) {
                             /*
                              * This happens e.g. if the UI class defined in the
@@ -402,7 +421,7 @@ public class ApplicationRunnerServlet extends LegacyVaadinServlet {
                             }
 
                             configuration = new DefaultDeploymentConfiguration(
-                                    getClass(), initParameters);
+                                    servlet.getClass(), initParameters);
                         } else {
                             configuration = originalConfiguration;
                         }
index f499f29b9bafa37ca7da513bdbe818a6bf119bf8..649f48c9ce301beb4946b5f6e7df0bf7f39c9d93 100644 (file)
@@ -1,21 +1,24 @@
 package com.vaadin.tests.components.ui;
 
-import com.vaadin.testbench.elements.ButtonElement;
-import com.vaadin.tests.tb3.SingleBrowserTest;
-import org.junit.Ignore;
+import static org.hamcrest.Matchers.allOf;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.endsWith;
+import static org.hamcrest.Matchers.startsWith;
+import static org.junit.Assert.assertThat;
+
 import org.junit.Test;
 
-import static org.hamcrest.Matchers.*;
-import static org.junit.Assert.assertThat;
+import com.vaadin.testbench.elements.ButtonElement;
+import com.vaadin.tests.tb3.SingleBrowserTest;
 
 public class UISerializationTest extends SingleBrowserTest {
 
     @Test
-    @Ignore
-    // Broken on all browsers since 9696e6c3e7e952b66ac3f5c9ddc3dfca4233451e
-    public void tb2test() throws Exception {
+    public void uiIsSerialized() throws Exception {
         openTestURL();
-        $(ButtonElement.class).first().click();
+
+        serialize();
+
         assertThat(getLogRow(0), startsWith("3. Diff states match, size: "));
         assertThat(getLogRow(1), startsWith("2. Deserialized UI in "));
         assertThat(
@@ -23,4 +26,8 @@ public class UISerializationTest extends SingleBrowserTest {
                 allOf(startsWith("1. Serialized UI in"),
                         containsString(" into "), endsWith(" bytes")));
     }
+
+    private void serialize() {
+        $(ButtonElement.class).first().click();
+    }
 }