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.1KB

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