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.

ApplicationConfiguration.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import com.google.gwt.core.client.EntryPoint;
  10. import com.google.gwt.core.client.GWT;
  11. import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
  12. import com.google.gwt.core.client.JavaScriptObject;
  13. import com.google.gwt.core.client.JsArrayString;
  14. import com.google.gwt.core.client.Scheduler;
  15. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  16. import com.google.gwt.user.client.Command;
  17. import com.google.gwt.user.client.Timer;
  18. import com.vaadin.terminal.gwt.client.ui.VUnknownComponent;
  19. public class ApplicationConfiguration implements EntryPoint {
  20. /**
  21. * Helper class for reading configuration options from the bootstap
  22. * javascript
  23. *
  24. * @since 7.0
  25. */
  26. private static class JsoConfiguration extends JavaScriptObject {
  27. protected JsoConfiguration() {
  28. // JSO Constructor
  29. }
  30. /**
  31. * Reads a configuration parameter as a string. Please note that the
  32. * javascript value of the parameter should also be a string, or else an
  33. * undefined exception may be thrown.
  34. *
  35. * @param name
  36. * name of the configuration parameter
  37. * @return value of the configuration parameter, or <code>null</code> if
  38. * not defined
  39. */
  40. private native String getConfigString(String name)
  41. /*-{
  42. var value = this.getConfig(name);
  43. if (!value) {
  44. return null;
  45. } else {
  46. return value +"";
  47. }
  48. }-*/;
  49. /**
  50. * Reads a configuration parameter as a boolean object. Please note that
  51. * the javascript value of the parameter should also be a boolean, or
  52. * else an undefined exception may be thrown.
  53. *
  54. * @param name
  55. * name of the configuration parameter
  56. * @return boolean value of the configuration paramter, or
  57. * <code>null</code> if no value is defined
  58. */
  59. private native Boolean getConfigBoolean(String name)
  60. /*-{
  61. var value = this.getConfig(name);
  62. if (value === null || value === undefined) {
  63. return null;
  64. } else {
  65. return @java.lang.Boolean::valueOf(Z)(value);
  66. }
  67. }-*/;
  68. /**
  69. * Reads a configuration parameter as an integer object. Please note
  70. * that the javascript value of the parameter should also be an integer,
  71. * or else an undefined exception may be thrown.
  72. *
  73. * @param name
  74. * name of the configuration parameter
  75. * @return integer value of the configuration paramter, or
  76. * <code>null</code> if no value is defined
  77. */
  78. private native Integer getConfigInteger(String name)
  79. /*-{
  80. var value = this.getConfig(name);
  81. if (value === null || value === undefined) {
  82. return null;
  83. } else {
  84. return @java.lang.Integer::valueOf(I)(value);
  85. }
  86. }-*/;
  87. /**
  88. * Reads a configuration parameter as an {@link ErrorMessage} object.
  89. * Please note that the javascript value of the parameter should also be
  90. * an object with appropriate fields, or else an undefined exception may
  91. * be thrown when calling this method or when calling methods on the
  92. * returned object.
  93. *
  94. * @param name
  95. * name of the configuration parameter
  96. * @return error message with the given name, or <code>null</code> if no
  97. * value is defined
  98. */
  99. private native ErrorMessage getConfigError(String name)
  100. /*-{
  101. return this.getConfig(name);
  102. }-*/;
  103. /**
  104. * Returns a native javascript object containing version information
  105. * from the server.
  106. *
  107. * @return a javascript object with the version information
  108. */
  109. private native JavaScriptObject getVersionInfoJSObject()
  110. /*-{
  111. return this.getConfig("versionInfo");
  112. }-*/;
  113. /**
  114. * Gets the version of the Vaadin framework used on the server.
  115. *
  116. * @return a string with the version
  117. *
  118. * @see com.vaadin.terminal.gwt.server.AbstractApplicationServlet#VERSION
  119. */
  120. private native String getVaadinVersion()
  121. /*-{
  122. return this.getConfig("versionInfo").vaadinVersion;
  123. }-*/;
  124. /**
  125. * Gets the version of the application running on the server.
  126. *
  127. * @return a string with the application version
  128. *
  129. * @see com.vaadin.Application#getVersion()
  130. */
  131. private native String getApplicationVersion()
  132. /*-{
  133. return this.getConfig("versionInfo").applicationVersion;
  134. }-*/;
  135. }
  136. /**
  137. * Wraps a native javascript object containing fields for an error message
  138. *
  139. * @since 7.0
  140. */
  141. public static final class ErrorMessage extends JavaScriptObject {
  142. protected ErrorMessage() {
  143. // JSO constructor
  144. }
  145. public final native String getCaption()
  146. /*-{
  147. return this.caption;
  148. }-*/;
  149. public final native String getMessage()
  150. /*-{
  151. return this.message;
  152. }-*/;
  153. public final native String getUrl()
  154. /*-{
  155. return this.url;
  156. }-*/;
  157. }
  158. /**
  159. * Builds number. For example 0-custom_tag in 5.0.0-custom_tag.
  160. */
  161. public static final String VERSION;
  162. /* Initialize version numbers from string replaced by build-script. */
  163. static {
  164. if ("@VERSION@".equals("@" + "VERSION" + "@")) {
  165. VERSION = "9.9.9.INTERNAL-DEBUG-BUILD";
  166. } else {
  167. VERSION = "@VERSION@";
  168. }
  169. }
  170. private static WidgetSet widgetSet = GWT.create(WidgetSet.class);
  171. private String id;
  172. private String themeUri;
  173. private String appUri;
  174. private int rootId;
  175. private boolean standalone;
  176. private ErrorMessage communicationError;
  177. private ErrorMessage authorizationError;
  178. private boolean useDebugIdInDom = true;
  179. private boolean usePortletURLs = false;
  180. private String portletUidlURLBase;
  181. private HashMap<String, String> unknownComponents;
  182. private Class<? extends Paintable>[] classes = new Class[1024];
  183. private String windowId;
  184. static// TODO consider to make this hashmap per application
  185. LinkedList<Command> callbacks = new LinkedList<Command>();
  186. private static int widgetsLoading;
  187. private static ArrayList<ApplicationConnection> runningApplications = new ArrayList<ApplicationConnection>();
  188. public boolean usePortletURLs() {
  189. return usePortletURLs;
  190. }
  191. public String getPortletUidlURLBase() {
  192. return portletUidlURLBase;
  193. }
  194. public String getRootPanelId() {
  195. return id;
  196. }
  197. /**
  198. * Gets the application base URI. Using this other than as the download
  199. * action URI can cause problems in Portlet 2.0 deployments.
  200. *
  201. * @return application base URI
  202. */
  203. public String getApplicationUri() {
  204. return appUri;
  205. }
  206. public String getThemeUri() {
  207. return themeUri;
  208. }
  209. public void setAppId(String appId) {
  210. id = appId;
  211. }
  212. /**
  213. * @return true if the application is served by std. Vaadin servlet and is
  214. * considered to be the only or main content of the host page.
  215. */
  216. public boolean isStandalone() {
  217. return standalone;
  218. }
  219. /**
  220. * Gets the root if of this application instance. The root id should be
  221. * included in every request originating from this instance in order to
  222. * associate it with the right Root instance on the server.
  223. *
  224. * @return the root id
  225. */
  226. public int getRootId() {
  227. return rootId;
  228. }
  229. public JavaScriptObject getVersionInfoJSObject() {
  230. return getJsoConfiguration(id).getVersionInfoJSObject();
  231. }
  232. public ErrorMessage getCommunicationError() {
  233. return communicationError;
  234. }
  235. public ErrorMessage getAuthorizationError() {
  236. return authorizationError;
  237. }
  238. /**
  239. * Reads the configuration values defined by the bootstrap javascript.
  240. */
  241. private void loadFromDOM() {
  242. JsoConfiguration jsoConfiguration = getJsoConfiguration(id);
  243. appUri = jsoConfiguration.getConfigString("appUri");
  244. if (appUri != null && !appUri.endsWith("/")) {
  245. appUri += '/';
  246. }
  247. themeUri = jsoConfiguration.getConfigString("themeUri");
  248. rootId = jsoConfiguration.getConfigInteger("rootId").intValue();
  249. // null -> true
  250. useDebugIdInDom = jsoConfiguration.getConfigBoolean("useDebugIdInDom") != Boolean.FALSE;
  251. // null -> false
  252. usePortletURLs = jsoConfiguration.getConfigBoolean("usePortletURLs") == Boolean.TRUE;
  253. portletUidlURLBase = jsoConfiguration
  254. .getConfigString("portletUidlURLBase");
  255. // null -> false
  256. standalone = jsoConfiguration.getConfigBoolean("standalone") == Boolean.TRUE;
  257. communicationError = jsoConfiguration.getConfigError("comErrMsg");
  258. authorizationError = jsoConfiguration.getConfigError("authErrMsg");
  259. }
  260. /**
  261. * Starts the application with a given id by reading the configuration
  262. * options stored by the bootstrap javascript.
  263. *
  264. * @param applicationId
  265. * id of the application to load, this is also the id of the html
  266. * element into which the application should be rendered.
  267. */
  268. public static void startApplication(final String applicationId) {
  269. Scheduler.get().scheduleDeferred(new ScheduledCommand() {
  270. public void execute() {
  271. ApplicationConfiguration appConf = getConfigFromDOM(applicationId);
  272. ApplicationConnection a = GWT
  273. .create(ApplicationConnection.class);
  274. a.init(widgetSet, appConf);
  275. a.start();
  276. runningApplications.add(a);
  277. }
  278. });
  279. }
  280. public static List<ApplicationConnection> getRunningApplications() {
  281. return runningApplications;
  282. }
  283. /**
  284. * Gets the configuration object for a specific application from the
  285. * bootstrap javascript.
  286. *
  287. * @param appId
  288. * the id of the application to get configuration data for
  289. * @return a native javascript object containing the configuration data
  290. */
  291. private native static JsoConfiguration getJsoConfiguration(String appId)
  292. /*-{
  293. return $wnd.vaadin.getApp(appId);
  294. }-*/;
  295. public static ApplicationConfiguration getConfigFromDOM(String appId) {
  296. ApplicationConfiguration conf = new ApplicationConfiguration();
  297. conf.setAppId(appId);
  298. conf.loadFromDOM();
  299. return conf;
  300. }
  301. public String getServletVersion() {
  302. return getJsoConfiguration(id).getVaadinVersion();
  303. }
  304. public String getApplicationVersion() {
  305. return getJsoConfiguration(id).getApplicationVersion();
  306. }
  307. public boolean useDebugIdInDOM() {
  308. return useDebugIdInDom;
  309. }
  310. public Class<? extends Paintable> getWidgetClassByEncodedTag(String tag) {
  311. try {
  312. int parseInt = Integer.parseInt(tag);
  313. return classes[parseInt];
  314. } catch (Exception e) {
  315. // component was not present in mappings
  316. return VUnknownComponent.class;
  317. }
  318. }
  319. public void addComponentMappings(ValueMap valueMap, WidgetSet widgetSet) {
  320. JsArrayString keyArray = valueMap.getKeyArray();
  321. for (int i = 0; i < keyArray.length(); i++) {
  322. String key = keyArray.get(i).intern();
  323. int value = valueMap.getInt(key);
  324. classes[value] = widgetSet.getImplementationByClassName(key);
  325. if (classes[value] == VUnknownComponent.class) {
  326. if (unknownComponents == null) {
  327. unknownComponents = new HashMap<String, String>();
  328. }
  329. unknownComponents.put("" + value, key);
  330. } else if (key == "com.vaadin.ui.Root") {
  331. windowId = "" + value;
  332. }
  333. }
  334. }
  335. /**
  336. * @return the integer value that is used to code top level windows
  337. * "com.vaadin.ui.Window"
  338. */
  339. String getEncodedWindowTag() {
  340. return windowId;
  341. }
  342. String getUnknownServerClassNameByEncodedTagName(String tag) {
  343. if (unknownComponents != null) {
  344. return unknownComponents.get(tag);
  345. }
  346. return null;
  347. }
  348. /**
  349. *
  350. * @param c
  351. */
  352. static void runWhenWidgetsLoaded(Command c) {
  353. if (widgetsLoading == 0) {
  354. c.execute();
  355. } else {
  356. callbacks.add(c);
  357. }
  358. }
  359. static void startWidgetLoading() {
  360. widgetsLoading++;
  361. }
  362. static void endWidgetLoading() {
  363. widgetsLoading--;
  364. if (widgetsLoading == 0 && !callbacks.isEmpty()) {
  365. for (Command cmd : callbacks) {
  366. cmd.execute();
  367. }
  368. callbacks.clear();
  369. } else if (widgetsLoading == 0 && deferredWidgetLoader != null) {
  370. deferredWidgetLoader.trigger();
  371. }
  372. }
  373. /*
  374. * This loop loads widget implementation that should be loaded deferred.
  375. */
  376. static class DeferredWidgetLoader extends Timer {
  377. private static final int FREE_LIMIT = 4;
  378. private static final int FREE_CHECK_TIMEOUT = 100;
  379. int communicationFree = 0;
  380. int nextWidgetIndex = 0;
  381. private boolean pending;
  382. public DeferredWidgetLoader() {
  383. schedule(5000);
  384. }
  385. public void trigger() {
  386. if (!pending) {
  387. schedule(FREE_CHECK_TIMEOUT);
  388. }
  389. }
  390. @Override
  391. public void schedule(int delayMillis) {
  392. super.schedule(delayMillis);
  393. pending = true;
  394. }
  395. @Override
  396. public void run() {
  397. pending = false;
  398. if (!isBusy()) {
  399. Class<? extends Paintable> nextType = getNextType();
  400. if (nextType == null) {
  401. // ensured that all widgets are loaded
  402. deferredWidgetLoader = null;
  403. } else {
  404. communicationFree = 0;
  405. widgetSet.loadImplementation(nextType);
  406. }
  407. } else {
  408. schedule(FREE_CHECK_TIMEOUT);
  409. }
  410. }
  411. private Class<? extends Paintable> getNextType() {
  412. Class<? extends Paintable>[] deferredLoadedWidgets = widgetSet
  413. .getDeferredLoadedWidgets();
  414. if (deferredLoadedWidgets.length <= nextWidgetIndex) {
  415. return null;
  416. } else {
  417. return deferredLoadedWidgets[nextWidgetIndex++];
  418. }
  419. }
  420. private boolean isBusy() {
  421. if (widgetsLoading > 0) {
  422. communicationFree = 0;
  423. return true;
  424. }
  425. for (ApplicationConnection app : runningApplications) {
  426. if (app.hasActiveRequest()) {
  427. // if an UIDL request or widget loading is active, mark as
  428. // busy
  429. communicationFree = 0;
  430. return true;
  431. }
  432. }
  433. communicationFree++;
  434. return communicationFree < FREE_LIMIT;
  435. }
  436. }
  437. private static DeferredWidgetLoader deferredWidgetLoader;
  438. public void onModuleLoad() {
  439. // Prepare VConsole for debugging
  440. if (isDebugMode()) {
  441. VDebugConsole console = GWT.create(VDebugConsole.class);
  442. console.setQuietMode(isQuietDebugMode());
  443. console.init();
  444. VConsole.setImplementation(console);
  445. } else {
  446. VConsole.setImplementation((Console) GWT.create(NullConsole.class));
  447. }
  448. /*
  449. * Display some sort of error of exceptions in web mode to debug
  450. * console. After this, exceptions are reported to VConsole and possible
  451. * GWT hosted mode.
  452. */
  453. GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
  454. public void onUncaughtException(Throwable e) {
  455. /*
  456. * Note in case of null console (without ?debug) we eat
  457. * exceptions. "a1 is not an object" style errors helps nobody,
  458. * especially end user. It does not work tells just as much.
  459. */
  460. VConsole.getImplementation().error(e);
  461. }
  462. });
  463. registerCallback(GWT.getModuleName());
  464. deferredWidgetLoader = new DeferredWidgetLoader();
  465. }
  466. /**
  467. * Registers that callback that the bootstrap javascript uses to start
  468. * applications once the widgetset is loaded and all required information is
  469. * available
  470. *
  471. * @param widgetsetName
  472. * the name of this widgetset
  473. */
  474. public native static void registerCallback(String widgetsetName)
  475. /*-{
  476. var callbackHandler = @com.vaadin.terminal.gwt.client.ApplicationConfiguration::startApplication(Ljava/lang/String;);
  477. $wnd.vaadin.registerWidgetset(widgetsetName, callbackHandler);
  478. }-*/;
  479. /**
  480. * Checks if client side is in debug mode. Practically this is invoked by
  481. * adding ?debug parameter to URI.
  482. *
  483. * @return true if client side is currently been debugged
  484. */
  485. public native static boolean isDebugMode()
  486. /*-{
  487. if($wnd.vaadin.debug) {
  488. var parameters = $wnd.location.search;
  489. var re = /debug[^\/]*$/;
  490. return re.test(parameters);
  491. } else {
  492. return false;
  493. }
  494. }-*/;
  495. private native static boolean isQuietDebugMode()
  496. /*-{
  497. var uri = $wnd.location;
  498. var re = /debug=q[^\/]*$/;
  499. return re.test(uri);
  500. }-*/;
  501. }