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

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