From 4402fd86495e703c00e949ca49d9799085432d46 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Thu, 28 Jun 2012 20:20:00 +0300 Subject: [PATCH] Support for SuperDevMode through ?superdevmode (#8983) --- .../gwt/client/ApplicationConfiguration.java | 5 + .../terminal/gwt/client/SuperDevMode.java | 247 ++++++++++++++++++ .../terminal/gwt/client/VDebugConsole.java | 83 ++++-- 3 files changed, 307 insertions(+), 28 deletions(-) create mode 100644 src/com/vaadin/terminal/gwt/client/SuperDevMode.java diff --git a/src/com/vaadin/terminal/gwt/client/ApplicationConfiguration.java b/src/com/vaadin/terminal/gwt/client/ApplicationConfiguration.java index 540841a6ae..31775d1d02 100644 --- a/src/com/vaadin/terminal/gwt/client/ApplicationConfiguration.java +++ b/src/com/vaadin/terminal/gwt/client/ApplicationConfiguration.java @@ -581,6 +581,11 @@ public class ApplicationConfiguration implements EntryPoint { } }); + if (SuperDevMode.enableBasedOnParameter()) { + // Do not start any application as super dev mode will refresh the + // page once done compiling + return; + } registerCallback(GWT.getModuleName()); deferredWidgetLoader = new DeferredWidgetLoader(); } diff --git a/src/com/vaadin/terminal/gwt/client/SuperDevMode.java b/src/com/vaadin/terminal/gwt/client/SuperDevMode.java new file mode 100644 index 0000000000..77a82c9aaf --- /dev/null +++ b/src/com/vaadin/terminal/gwt/client/SuperDevMode.java @@ -0,0 +1,247 @@ +package com.vaadin.terminal.gwt.client; + +import com.google.gwt.core.client.GWT; +import com.google.gwt.core.client.JavaScriptObject; +import com.google.gwt.http.client.UrlBuilder; +import com.google.gwt.jsonp.client.JsonpRequestBuilder; +import com.google.gwt.storage.client.Storage; +import com.google.gwt.user.client.Window.Location; +import com.google.gwt.user.client.rpc.AsyncCallback; +import com.vaadin.terminal.gwt.client.ui.notification.VNotification; +import com.vaadin.terminal.gwt.client.ui.notification.VNotification.EventListener; +import com.vaadin.terminal.gwt.client.ui.notification.VNotification.HideEvent; + +/** + * Class that enables SuperDevMode using a ?superdevmode parameter in the url. + * + * @author Vaadin Ltd + * @version @VERSION@ + * @since 7.0 + * + */ +public class SuperDevMode { + + private static final int COMPILE_TIMEOUT_IN_SECONDS = 60; + protected static final String SKIP_RECOMPILE = "VaadinSuperDevMode_skip_recompile"; + + public static class RecompileResult extends JavaScriptObject { + protected RecompileResult() { + + } + + public final native boolean ok() + /*-{ + return this.status == "ok"; + }-*/; + } + + private static void recompileWidgetsetAndStartInDevMode( + final String serverUrl) { + VConsole.log("Recompiling widgetset using
" + serverUrl + + "
and then reloading in super dev mode"); + VNotification n = new VNotification(); + n.show("Recompiling widgetset, this should not take too long", + VNotification.CENTERED, VNotification.STYLE_SYSTEM); + + JsonpRequestBuilder b = new JsonpRequestBuilder(); + b.setCallbackParam("_callback"); + b.setTimeout(COMPILE_TIMEOUT_IN_SECONDS * 1000); + b.requestObject(serverUrl + "recompile/" + GWT.getModuleName() + "?" + + getRecompileParameters(GWT.getModuleName()), + new AsyncCallback() { + + public void onSuccess(RecompileResult result) { + VConsole.log("JSONP compile call successful"); + + if (!result.ok()) { + VConsole.log("* result: " + result); + failed(); + return; + } + + setSession( + getSuperDevModeHookKey(), + getSuperDevWidgetSetUrl(GWT.getModuleName(), + serverUrl)); + setSession(SKIP_RECOMPILE, "1"); + + VConsole.log("* result: OK. Reloading"); + Location.reload(); + } + + public void onFailure(Throwable caught) { + VConsole.error("JSONP compile call failed"); + // Don't log exception as they are shown as + // notifications + VConsole.error(Util.getSimpleName(caught) + ": " + + caught.getMessage()); + failed(); + + } + + private void failed() { + VNotification n = new VNotification(); + n.addEventListener(new EventListener() { + + public void notificationHidden(HideEvent event) { + recompileWidgetsetAndStartInDevMode(serverUrl); + } + }); + n.show("Recompilation failed.
" + + "Make sure CodeServer is running, " + + "check its output and click to retry", + VNotification.CENTERED, + VNotification.STYLE_SYSTEM); + } + }); + + } + + protected static String getSuperDevWidgetSetUrl(String widgetsetName, + String serverUrl) { + return serverUrl + GWT.getModuleName() + "/" + GWT.getModuleName() + + ".nocache.js"; + } + + private native static String getRecompileParameters(String moduleName) + /*-{ + var prop_map = $wnd.__gwt_activeModules[moduleName].bindings(); + + // convert map to URL parameter string + var props = []; + for (var key in prop_map) { + props.push(encodeURIComponent(key) + '=' + encodeURIComponent(prop_map[key])) + } + + return props.join('&') + '&'; + }-*/; + + private static void setSession(String key, String value) { + Storage.getSessionStorageIfSupported().setItem(key, value); + } + + private static String getSession(String key) { + return Storage.getSessionStorageIfSupported().getItem(key); + } + + private static void removeSession(String key) { + Storage.getSessionStorageIfSupported().removeItem(key); + } + + protected static void disableDevModeAndReload() { + removeSession(getSuperDevModeHookKey()); + redirect(false); + } + + protected static void redirect(boolean devModeOn) { + UrlBuilder createUrlBuilder = Location.createUrlBuilder(); + if (!devModeOn) { + createUrlBuilder.removeParameter("superdevmode"); + } else { + createUrlBuilder.setParameter("superdevmode", ""); + } + + Location.assign(createUrlBuilder.buildString()); + + } + + private static String getSuperDevModeHookKey() { + String widgetsetName = GWT.getModuleName(); + final String superDevModeKey = "__gwtDevModeHook:" + widgetsetName; + return superDevModeKey; + } + + private static boolean hasSession(String key) { + return getSession(key) != null; + } + + /** + * The URL of the code server. The default URL (http://localhost:9876/) will + * be used if this is empty or null. + * + * @param serverUrl + * The url of the code server or null to use the default + * @return true if recompile started, false if we are running in + * SuperDevMode + */ + protected static boolean recompileIfNeeded(String serverUrl) { + if (serverUrl == null || "".equals(serverUrl)) { + serverUrl = "http://localhost:9876/"; + } else { + serverUrl = "http://" + serverUrl + "/"; + } + + if (hasSession(SKIP_RECOMPILE)) { + VConsole.log("Running in SuperDevMode"); + // When we get here, we are running in super dev mode + + // Remove the flag so next reload will recompile + removeSession(SKIP_RECOMPILE); + + // Remove the gwt flag so we will not end up in dev mode if we + // remove the url parameter manually + removeSession(getSuperDevModeHookKey()); + + return false; + } + + recompileWidgetsetAndStartInDevMode(serverUrl); + return true; + } + + protected static boolean isSuperDevModeEnabledInModule() { + String moduleName = GWT.getModuleName(); + return isSuperDevModeEnabledInModule(moduleName); + } + + protected native static boolean isSuperDevModeEnabledInModule( + String moduleName) + /*-{ + if (!$wnd.__gwt_activeModules) + return false; + var mod = $wnd.__gwt_activeModules[moduleName]; + if (!mod) + return false; + + if (mod.superdevmode) { + // Running in super dev mode already, it is supported + return true; + } + + return mod.canRedirect; + }-*/; + + /** + * Enables SuperDevMode if the url contains the "superdevmode" parameter. + *

+ * The caller should not continue initialization of the application if this + * method returns true. The application will be restarted once compilation + * is done and then this method will return false. + *

+ * + * @return true if a recompile operation has started and the page will be + * reloaded once it is done, false if no recompilation will be done. + */ + public static boolean enableBasedOnParameter() { + String superDevModeParameter = Location.getParameter("superdevmode"); + if (superDevModeParameter != null) { + // Need to check the recompile flag also because if we are running + // in super dev mode, as a result of the recompile, the enabled + // check will fail... + if (!isSuperDevModeEnabledInModule()) { + showError("SuperDevMode is not enabled for this module/widgetset.
" + + "Ensure that your module definition (.gwt.xml) contains
" + + "<add-linker name="xsiframe"/>
" + + "<set-configuration-property name="devModeRedirectEnabled" value="true" />
"); + return false; + } + return SuperDevMode.recompileIfNeeded(superDevModeParameter); + } + return false; + } + + private static void showError(String message) { + VNotification n = new VNotification(); + n.show(message, VNotification.CENTERED_TOP, VNotification.STYLE_SYSTEM); + } +} diff --git a/src/com/vaadin/terminal/gwt/client/VDebugConsole.java b/src/com/vaadin/terminal/gwt/client/VDebugConsole.java index 09e939336e..ce893741d3 100644 --- a/src/com/vaadin/terminal/gwt/client/VDebugConsole.java +++ b/src/com/vaadin/terminal/gwt/client/VDebugConsole.java @@ -24,6 +24,8 @@ import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.dom.client.MouseOutEvent; import com.google.gwt.event.dom.client.MouseOutHandler; +import com.google.gwt.event.logical.shared.ValueChangeEvent; +import com.google.gwt.event.logical.shared.ValueChangeHandler; import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.event.shared.UmbrellaException; import com.google.gwt.http.client.Request; @@ -33,6 +35,7 @@ import com.google.gwt.http.client.RequestException; import com.google.gwt.http.client.Response; import com.google.gwt.http.client.UrlBuilder; import com.google.gwt.i18n.client.DateTimeFormat; +import com.google.gwt.storage.client.Storage; import com.google.gwt.user.client.Cookies; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Element; @@ -160,7 +163,8 @@ public class VDebugConsole extends VOverlay implements Console { private Button savePosition = new Button("S"); private Button highlight = new Button("H"); private Button connectorStats = new Button("CS"); - private CheckBox hostedMode = new CheckBox("GWT"); + private CheckBox devMode = new CheckBox("Dev"); + private CheckBox superDevMode = new CheckBox("SDev"); private CheckBox autoScroll = new CheckBox("Autoscroll "); private HorizontalPanel actions; private boolean collapsed = false; @@ -717,33 +721,8 @@ public class VDebugConsole extends VOverlay implements Console { savePosition .setTitle("Saves the position and size of debug console to a cookie"); actions.add(autoScroll); - actions.add(hostedMode); - if (Location.getParameter("gwt.codesvr") != null) { - hostedMode.setValue(true); - } - hostedMode.addClickHandler(new ClickHandler() { - public void onClick(ClickEvent event) { - if (hostedMode.getValue()) { - addHMParameter(); - } else { - removeHMParameter(); - } - } - - private void addHMParameter() { - UrlBuilder createUrlBuilder = Location.createUrlBuilder(); - createUrlBuilder.setParameter("gwt.codesvr", - "localhost:9997"); - Location.assign(createUrlBuilder.buildString()); - } - - private void removeHMParameter() { - UrlBuilder createUrlBuilder = Location.createUrlBuilder(); - createUrlBuilder.removeParameter("gwt.codesvr"); - Location.assign(createUrlBuilder.buildString()); - - } - }); + addDevMode(); + addSuperDevMode(); autoScroll .setTitle("Automatically scroll so that new messages are visible"); @@ -861,6 +840,54 @@ public class VDebugConsole extends VOverlay implements Console { } + private void addSuperDevMode() { + final Storage sessionStorage = Storage.getSessionStorageIfSupported(); + if (sessionStorage == null) { + return; + } + actions.add(superDevMode); + if (Location.getParameter("superdevmode") != null) { + superDevMode.setValue(true); + } + superDevMode.addValueChangeHandler(new ValueChangeHandler() { + + public void onValueChange(ValueChangeEvent event) { + SuperDevMode.redirect(event.getValue()); + } + + }); + + } + + private void addDevMode() { + actions.add(devMode); + if (Location.getParameter("gwt.codesvr") != null) { + devMode.setValue(true); + } + devMode.addClickHandler(new ClickHandler() { + public void onClick(ClickEvent event) { + if (devMode.getValue()) { + addHMParameter(); + } else { + removeHMParameter(); + } + } + + private void addHMParameter() { + UrlBuilder createUrlBuilder = Location.createUrlBuilder(); + createUrlBuilder.setParameter("gwt.codesvr", "localhost:9997"); + Location.assign(createUrlBuilder.buildString()); + } + + private void removeHMParameter() { + UrlBuilder createUrlBuilder = Location.createUrlBuilder(); + createUrlBuilder.removeParameter("gwt.codesvr"); + Location.assign(createUrlBuilder.buildString()); + + } + }); + } + protected void dumpConnectorInfo(ApplicationConnection a) { RootConnector root = a.getRootConnector(); log("================"); -- 2.39.5