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

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