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

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