You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SuperDevMode.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client;
  5. import com.google.gwt.core.client.GWT;
  6. import com.google.gwt.core.client.JavaScriptObject;
  7. import com.google.gwt.http.client.UrlBuilder;
  8. import com.google.gwt.jsonp.client.JsonpRequestBuilder;
  9. import com.google.gwt.storage.client.Storage;
  10. import com.google.gwt.user.client.Window.Location;
  11. import com.google.gwt.user.client.rpc.AsyncCallback;
  12. import com.vaadin.terminal.gwt.client.ui.notification.VNotification;
  13. import com.vaadin.terminal.gwt.client.ui.notification.VNotification.EventListener;
  14. import com.vaadin.terminal.gwt.client.ui.notification.VNotification.HideEvent;
  15. /**
  16. * Class that enables SuperDevMode using a ?superdevmode parameter in the url.
  17. *
  18. * @author Vaadin Ltd
  19. * @version @VERSION@
  20. * @since 7.0
  21. *
  22. */
  23. public class SuperDevMode {
  24. private static final int COMPILE_TIMEOUT_IN_SECONDS = 60;
  25. protected static final String SKIP_RECOMPILE = "VaadinSuperDevMode_skip_recompile";
  26. public static class RecompileResult extends JavaScriptObject {
  27. protected RecompileResult() {
  28. }
  29. public final native boolean ok()
  30. /*-{
  31. return this.status == "ok";
  32. }-*/;
  33. }
  34. private static void recompileWidgetsetAndStartInDevMode(
  35. final String serverUrl) {
  36. VConsole.log("Recompiling widgetset using<br/>" + serverUrl
  37. + "<br/>and then reloading in super dev mode");
  38. VNotification n = new VNotification();
  39. n.show("<b>Recompiling widgetset, this should not take too long</b>",
  40. VNotification.CENTERED, VNotification.STYLE_SYSTEM);
  41. JsonpRequestBuilder b = new JsonpRequestBuilder();
  42. b.setCallbackParam("_callback");
  43. b.setTimeout(COMPILE_TIMEOUT_IN_SECONDS * 1000);
  44. b.requestObject(serverUrl + "recompile/" + GWT.getModuleName() + "?"
  45. + getRecompileParameters(GWT.getModuleName()),
  46. new AsyncCallback<RecompileResult>() {
  47. public void onSuccess(RecompileResult result) {
  48. VConsole.log("JSONP compile call successful");
  49. if (!result.ok()) {
  50. VConsole.log("* result: " + result);
  51. failed();
  52. return;
  53. }
  54. setSession(
  55. getSuperDevModeHookKey(),
  56. getSuperDevWidgetSetUrl(GWT.getModuleName(),
  57. serverUrl));
  58. setSession(SKIP_RECOMPILE, "1");
  59. VConsole.log("* result: OK. Reloading");
  60. Location.reload();
  61. }
  62. public void onFailure(Throwable caught) {
  63. VConsole.error("JSONP compile call failed");
  64. // Don't log exception as they are shown as
  65. // notifications
  66. VConsole.error(Util.getSimpleName(caught) + ": "
  67. + caught.getMessage());
  68. failed();
  69. }
  70. private void failed() {
  71. VNotification n = new VNotification();
  72. n.addEventListener(new EventListener() {
  73. public void notificationHidden(HideEvent event) {
  74. recompileWidgetsetAndStartInDevMode(serverUrl);
  75. }
  76. });
  77. n.show("Recompilation failed.<br/>"
  78. + "Make sure CodeServer is running, "
  79. + "check its output and click to retry",
  80. VNotification.CENTERED,
  81. VNotification.STYLE_SYSTEM);
  82. }
  83. });
  84. }
  85. protected static String getSuperDevWidgetSetUrl(String widgetsetName,
  86. String serverUrl) {
  87. return serverUrl + GWT.getModuleName() + "/" + GWT.getModuleName()
  88. + ".nocache.js";
  89. }
  90. private native static String getRecompileParameters(String moduleName)
  91. /*-{
  92. var prop_map = $wnd.__gwt_activeModules[moduleName].bindings();
  93. // convert map to URL parameter string
  94. var props = [];
  95. for (var key in prop_map) {
  96. props.push(encodeURIComponent(key) + '=' + encodeURIComponent(prop_map[key]))
  97. }
  98. return props.join('&') + '&';
  99. }-*/;
  100. private static void setSession(String key, String value) {
  101. Storage.getSessionStorageIfSupported().setItem(key, value);
  102. }
  103. private static String getSession(String key) {
  104. return Storage.getSessionStorageIfSupported().getItem(key);
  105. }
  106. private static void removeSession(String key) {
  107. Storage.getSessionStorageIfSupported().removeItem(key);
  108. }
  109. protected static void disableDevModeAndReload() {
  110. removeSession(getSuperDevModeHookKey());
  111. redirect(false);
  112. }
  113. protected static void redirect(boolean devModeOn) {
  114. UrlBuilder createUrlBuilder = Location.createUrlBuilder();
  115. if (!devModeOn) {
  116. createUrlBuilder.removeParameter("superdevmode");
  117. } else {
  118. createUrlBuilder.setParameter("superdevmode", "");
  119. }
  120. Location.assign(createUrlBuilder.buildString());
  121. }
  122. private static String getSuperDevModeHookKey() {
  123. String widgetsetName = GWT.getModuleName();
  124. final String superDevModeKey = "__gwtDevModeHook:" + widgetsetName;
  125. return superDevModeKey;
  126. }
  127. private static boolean hasSession(String key) {
  128. return getSession(key) != null;
  129. }
  130. /**
  131. * The URL of the code server. The default URL (http://localhost:9876/) will
  132. * be used if this is empty or null.
  133. *
  134. * @param serverUrl
  135. * The url of the code server or null to use the default
  136. * @return true if recompile started, false if we are running in
  137. * SuperDevMode
  138. */
  139. protected static boolean recompileIfNeeded(String serverUrl) {
  140. if (serverUrl == null || "".equals(serverUrl)) {
  141. serverUrl = "http://localhost:9876/";
  142. } else {
  143. serverUrl = "http://" + serverUrl + "/";
  144. }
  145. if (hasSession(SKIP_RECOMPILE)) {
  146. VConsole.log("Running in SuperDevMode");
  147. // When we get here, we are running in super dev mode
  148. // Remove the flag so next reload will recompile
  149. removeSession(SKIP_RECOMPILE);
  150. // Remove the gwt flag so we will not end up in dev mode if we
  151. // remove the url parameter manually
  152. removeSession(getSuperDevModeHookKey());
  153. return false;
  154. }
  155. recompileWidgetsetAndStartInDevMode(serverUrl);
  156. return true;
  157. }
  158. protected static boolean isSuperDevModeEnabledInModule() {
  159. String moduleName = GWT.getModuleName();
  160. return isSuperDevModeEnabledInModule(moduleName);
  161. }
  162. protected native static boolean isSuperDevModeEnabledInModule(
  163. String moduleName)
  164. /*-{
  165. if (!$wnd.__gwt_activeModules)
  166. return false;
  167. var mod = $wnd.__gwt_activeModules[moduleName];
  168. if (!mod)
  169. return false;
  170. if (mod.superdevmode) {
  171. // Running in super dev mode already, it is supported
  172. return true;
  173. }
  174. return mod.canRedirect;
  175. }-*/;
  176. /**
  177. * Enables SuperDevMode if the url contains the "superdevmode" parameter.
  178. * <p>
  179. * The caller should not continue initialization of the application if this
  180. * method returns true. The application will be restarted once compilation
  181. * is done and then this method will return false.
  182. * </p>
  183. *
  184. * @return true if a recompile operation has started and the page will be
  185. * reloaded once it is done, false if no recompilation will be done.
  186. */
  187. public static boolean enableBasedOnParameter() {
  188. String superDevModeParameter = Location.getParameter("superdevmode");
  189. if (superDevModeParameter != null) {
  190. // Need to check the recompile flag also because if we are running
  191. // in super dev mode, as a result of the recompile, the enabled
  192. // check will fail...
  193. if (!isSuperDevModeEnabledInModule()) {
  194. showError("SuperDevMode is not enabled for this module/widgetset.<br/>"
  195. + "Ensure that your module definition (.gwt.xml) contains <br/>"
  196. + "&lt;add-linker name=&quot;xsiframe&quot;/&gt;<br/>"
  197. + "&lt;set-configuration-property name=&quot;devModeRedirectEnabled&quot; value=&quot;true&quot; /&gt;<br/>");
  198. return false;
  199. }
  200. return SuperDevMode.recompileIfNeeded(superDevModeParameter);
  201. }
  202. return false;
  203. }
  204. private static void showError(String message) {
  205. VNotification n = new VNotification();
  206. n.show(message, VNotification.CENTERED_TOP, VNotification.STYLE_SYSTEM);
  207. }
  208. }