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

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