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

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