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 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  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 === null || value === undefined) {
  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. private native String getUIDL()
  136. /*-{
  137. return this.getConfig("uidl");
  138. }-*/;
  139. }
  140. /**
  141. * Wraps a native javascript object containing fields for an error message
  142. *
  143. * @since 7.0
  144. */
  145. public static final class ErrorMessage extends JavaScriptObject {
  146. protected ErrorMessage() {
  147. // JSO constructor
  148. }
  149. public final native String getCaption()
  150. /*-{
  151. return this.caption;
  152. }-*/;
  153. public final native String getMessage()
  154. /*-{
  155. return this.message;
  156. }-*/;
  157. public final native String getUrl()
  158. /*-{
  159. return this.url;
  160. }-*/;
  161. }
  162. /**
  163. * Builds number. For example 0-custom_tag in 5.0.0-custom_tag.
  164. */
  165. public static final String VERSION;
  166. /* Initialize version numbers from string replaced by build-script. */
  167. static {
  168. if ("@VERSION@".equals("@" + "VERSION" + "@")) {
  169. VERSION = "9.9.9.INTERNAL-DEBUG-BUILD";
  170. } else {
  171. VERSION = "@VERSION@";
  172. }
  173. }
  174. private static WidgetSet widgetSet = GWT.create(WidgetSet.class);
  175. private String id;
  176. private String themeUri;
  177. private String appUri;
  178. private int rootId;
  179. private boolean standalone;
  180. private ErrorMessage communicationError;
  181. private ErrorMessage authorizationError;
  182. private boolean useDebugIdInDom = true;
  183. private boolean usePortletURLs = false;
  184. private String portletUidlURLBase;
  185. private HashMap<String, String> unknownComponents;
  186. private Class<? extends VPaintableWidget>[] classes = new Class[1024];
  187. private String windowId;
  188. private boolean browserDetailsSent = false;
  189. static// TODO consider to make this hashmap per application
  190. LinkedList<Command> callbacks = new LinkedList<Command>();
  191. private static int widgetsLoading;
  192. private static ArrayList<ApplicationConnection> runningApplications = new ArrayList<ApplicationConnection>();
  193. public boolean usePortletURLs() {
  194. return usePortletURLs;
  195. }
  196. public String getPortletUidlURLBase() {
  197. return portletUidlURLBase;
  198. }
  199. public String getRootPanelId() {
  200. return id;
  201. }
  202. /**
  203. * Gets the application base URI. Using this other than as the download
  204. * action URI can cause problems in Portlet 2.0 deployments.
  205. *
  206. * @return application base URI
  207. */
  208. public String getApplicationUri() {
  209. return appUri;
  210. }
  211. public String getThemeUri() {
  212. return themeUri;
  213. }
  214. public void setAppId(String appId) {
  215. id = appId;
  216. }
  217. /**
  218. * Gets the initial UIDL from the DOM, if it was provided during the init
  219. * process.
  220. *
  221. * @return
  222. */
  223. public String getUIDL() {
  224. return getJsoConfiguration(id).getUIDL();
  225. }
  226. /**
  227. * @return true if the application is served by std. Vaadin servlet and is
  228. * considered to be the only or main content of the host page.
  229. */
  230. public boolean isStandalone() {
  231. return standalone;
  232. }
  233. /**
  234. * Gets the root if of this application instance. The root id should be
  235. * included in every request originating from this instance in order to
  236. * associate it with the right Root instance on the server.
  237. *
  238. * @return the root id
  239. */
  240. public int getRootId() {
  241. return rootId;
  242. }
  243. public JavaScriptObject getVersionInfoJSObject() {
  244. return getJsoConfiguration(id).getVersionInfoJSObject();
  245. }
  246. public ErrorMessage getCommunicationError() {
  247. return communicationError;
  248. }
  249. public ErrorMessage getAuthorizationError() {
  250. return authorizationError;
  251. }
  252. /**
  253. * Reads the configuration values defined by the bootstrap javascript.
  254. */
  255. private void loadFromDOM() {
  256. JsoConfiguration jsoConfiguration = getJsoConfiguration(id);
  257. appUri = jsoConfiguration.getConfigString("appUri");
  258. if (appUri != null && !appUri.endsWith("/")) {
  259. appUri += '/';
  260. }
  261. themeUri = jsoConfiguration.getConfigString("themeUri");
  262. rootId = jsoConfiguration.getConfigInteger("rootId").intValue();
  263. // null -> true
  264. useDebugIdInDom = jsoConfiguration.getConfigBoolean("useDebugIdInDom") != Boolean.FALSE;
  265. // null -> false
  266. usePortletURLs = jsoConfiguration.getConfigBoolean("usePortletURLs") == Boolean.TRUE;
  267. portletUidlURLBase = jsoConfiguration
  268. .getConfigString("portletUidlURLBase");
  269. // null -> false
  270. standalone = jsoConfiguration.getConfigBoolean("standalone") == Boolean.TRUE;
  271. communicationError = jsoConfiguration.getConfigError("comErrMsg");
  272. authorizationError = jsoConfiguration.getConfigError("authErrMsg");
  273. // boostrap sets initPending to false if it has sent the browser details
  274. if (jsoConfiguration.getConfigBoolean("initPending") == Boolean.FALSE) {
  275. setBrowserDetailsSent();
  276. }
  277. }
  278. /**
  279. * Starts the application with a given id by reading the configuration
  280. * options stored by the bootstrap javascript.
  281. *
  282. * @param applicationId
  283. * id of the application to load, this is also the id of the html
  284. * element into which the application should be rendered.
  285. */
  286. public static void startApplication(final String applicationId) {
  287. Scheduler.get().scheduleDeferred(new ScheduledCommand() {
  288. public void execute() {
  289. ApplicationConfiguration appConf = getConfigFromDOM(applicationId);
  290. ApplicationConnection a = GWT
  291. .create(ApplicationConnection.class);
  292. a.init(widgetSet, appConf);
  293. a.start();
  294. runningApplications.add(a);
  295. }
  296. });
  297. }
  298. public static List<ApplicationConnection> getRunningApplications() {
  299. return runningApplications;
  300. }
  301. /**
  302. * Gets the configuration object for a specific application from the
  303. * bootstrap javascript.
  304. *
  305. * @param appId
  306. * the id of the application to get configuration data for
  307. * @return a native javascript object containing the configuration data
  308. */
  309. private native static JsoConfiguration getJsoConfiguration(String appId)
  310. /*-{
  311. return $wnd.vaadin.getApp(appId);
  312. }-*/;
  313. public static ApplicationConfiguration getConfigFromDOM(String appId) {
  314. ApplicationConfiguration conf = new ApplicationConfiguration();
  315. conf.setAppId(appId);
  316. conf.loadFromDOM();
  317. return conf;
  318. }
  319. public String getServletVersion() {
  320. return getJsoConfiguration(id).getVaadinVersion();
  321. }
  322. public String getApplicationVersion() {
  323. return getJsoConfiguration(id).getApplicationVersion();
  324. }
  325. public boolean useDebugIdInDOM() {
  326. return useDebugIdInDom;
  327. }
  328. public Class<? extends VPaintableWidget> getWidgetClassByEncodedTag(String tag) {
  329. try {
  330. int parseInt = Integer.parseInt(tag);
  331. return classes[parseInt];
  332. } catch (Exception e) {
  333. // component was not present in mappings
  334. return VUnknownComponent.class;
  335. }
  336. }
  337. public void addComponentMappings(ValueMap valueMap, WidgetSet widgetSet) {
  338. JsArrayString keyArray = valueMap.getKeyArray();
  339. for (int i = 0; i < keyArray.length(); i++) {
  340. String key = keyArray.get(i).intern();
  341. int value = valueMap.getInt(key);
  342. classes[value] = widgetSet.getImplementationByClassName(key);
  343. if (classes[value] == VUnknownComponent.class) {
  344. if (unknownComponents == null) {
  345. unknownComponents = new HashMap<String, String>();
  346. }
  347. unknownComponents.put("" + value, key);
  348. } else if (key == "com.vaadin.ui.Root") {
  349. windowId = "" + value;
  350. }
  351. }
  352. }
  353. /**
  354. * @return the integer value that is used to code top level windows
  355. * "com.vaadin.ui.Window"
  356. */
  357. String getEncodedWindowTag() {
  358. return windowId;
  359. }
  360. String getUnknownServerClassNameByEncodedTagName(String tag) {
  361. if (unknownComponents != null) {
  362. return unknownComponents.get(tag);
  363. }
  364. return null;
  365. }
  366. /**
  367. *
  368. * @param c
  369. */
  370. static void runWhenWidgetsLoaded(Command c) {
  371. if (widgetsLoading == 0) {
  372. c.execute();
  373. } else {
  374. callbacks.add(c);
  375. }
  376. }
  377. static void startWidgetLoading() {
  378. widgetsLoading++;
  379. }
  380. static void endWidgetLoading() {
  381. widgetsLoading--;
  382. if (widgetsLoading == 0 && !callbacks.isEmpty()) {
  383. for (Command cmd : callbacks) {
  384. cmd.execute();
  385. }
  386. callbacks.clear();
  387. } else if (widgetsLoading == 0 && deferredWidgetLoader != null) {
  388. deferredWidgetLoader.trigger();
  389. }
  390. }
  391. /*
  392. * This loop loads widget implementation that should be loaded deferred.
  393. */
  394. static class DeferredWidgetLoader extends Timer {
  395. private static final int FREE_LIMIT = 4;
  396. private static final int FREE_CHECK_TIMEOUT = 100;
  397. int communicationFree = 0;
  398. int nextWidgetIndex = 0;
  399. private boolean pending;
  400. public DeferredWidgetLoader() {
  401. schedule(5000);
  402. }
  403. public void trigger() {
  404. if (!pending) {
  405. schedule(FREE_CHECK_TIMEOUT);
  406. }
  407. }
  408. @Override
  409. public void schedule(int delayMillis) {
  410. super.schedule(delayMillis);
  411. pending = true;
  412. }
  413. @Override
  414. public void run() {
  415. pending = false;
  416. if (!isBusy()) {
  417. Class<? extends VPaintableWidget> nextType = getNextType();
  418. if (nextType == null) {
  419. // ensured that all widgets are loaded
  420. deferredWidgetLoader = null;
  421. } else {
  422. communicationFree = 0;
  423. widgetSet.loadImplementation(nextType);
  424. }
  425. } else {
  426. schedule(FREE_CHECK_TIMEOUT);
  427. }
  428. }
  429. private Class<? extends VPaintableWidget> getNextType() {
  430. Class<? extends VPaintableWidget>[] deferredLoadedWidgets = widgetSet
  431. .getDeferredLoadedWidgets();
  432. if (deferredLoadedWidgets.length <= nextWidgetIndex) {
  433. return null;
  434. } else {
  435. return deferredLoadedWidgets[nextWidgetIndex++];
  436. }
  437. }
  438. private boolean isBusy() {
  439. if (widgetsLoading > 0) {
  440. communicationFree = 0;
  441. return true;
  442. }
  443. for (ApplicationConnection app : runningApplications) {
  444. if (app.hasActiveRequest()) {
  445. // if an UIDL request or widget loading is active, mark as
  446. // busy
  447. communicationFree = 0;
  448. return true;
  449. }
  450. }
  451. communicationFree++;
  452. return communicationFree < FREE_LIMIT;
  453. }
  454. }
  455. private static DeferredWidgetLoader deferredWidgetLoader;
  456. public void onModuleLoad() {
  457. // Prepare VConsole for debugging
  458. if (isDebugMode()) {
  459. Console console = GWT.create(Console.class);
  460. console.setQuietMode(isQuietDebugMode());
  461. console.init();
  462. VConsole.setImplementation(console);
  463. } else {
  464. VConsole.setImplementation((Console) GWT.create(NullConsole.class));
  465. }
  466. /*
  467. * Display some sort of error of exceptions in web mode to debug
  468. * console. After this, exceptions are reported to VConsole and possible
  469. * GWT hosted mode.
  470. */
  471. GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
  472. public void onUncaughtException(Throwable e) {
  473. /*
  474. * Note in case of null console (without ?debug) we eat
  475. * exceptions. "a1 is not an object" style errors helps nobody,
  476. * especially end user. It does not work tells just as much.
  477. */
  478. VConsole.getImplementation().error(e);
  479. }
  480. });
  481. registerCallback(GWT.getModuleName());
  482. deferredWidgetLoader = new DeferredWidgetLoader();
  483. }
  484. /**
  485. * Registers that callback that the bootstrap javascript uses to start
  486. * applications once the widgetset is loaded and all required information is
  487. * available
  488. *
  489. * @param widgetsetName
  490. * the name of this widgetset
  491. */
  492. public native static void registerCallback(String widgetsetName)
  493. /*-{
  494. var callbackHandler = @com.vaadin.terminal.gwt.client.ApplicationConfiguration::startApplication(Ljava/lang/String;);
  495. $wnd.vaadin.registerWidgetset(widgetsetName, callbackHandler);
  496. }-*/;
  497. /**
  498. * Checks if client side is in debug mode. Practically this is invoked by
  499. * adding ?debug parameter to URI.
  500. *
  501. * @return true if client side is currently been debugged
  502. */
  503. public native static boolean isDebugMode()
  504. /*-{
  505. if($wnd.vaadin.debug) {
  506. var parameters = $wnd.location.search;
  507. var re = /debug[^\/]*$/;
  508. return re.test(parameters);
  509. } else {
  510. return false;
  511. }
  512. }-*/;
  513. /**
  514. * Checks whether debug logging should be quiet
  515. *
  516. * @return <code>true</code> if debug logging should be quiet
  517. */
  518. public native static boolean isQuietDebugMode()
  519. /*-{
  520. var uri = $wnd.location;
  521. var re = /debug=q[^\/]*$/;
  522. return re.test(uri);
  523. }-*/;
  524. /**
  525. * Checks whether information from the web browser (e.g. uri fragment and
  526. * screen size) has been sent to the server.
  527. *
  528. * @return <code>true</code> if browser information has already been sent
  529. *
  530. * @see ApplicationConnection#getNativeBrowserDetailsParameters(String)
  531. */
  532. public boolean isBrowserDetailsSent() {
  533. return browserDetailsSent;
  534. }
  535. /**
  536. * Registers that the browser details have been sent.
  537. * {@link #isBrowserDetailsSent()} will return
  538. * <code> after this method has been invoked.
  539. */
  540. public void setBrowserDetailsSent() {
  541. browserDetailsSent = true;
  542. }
  543. }