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

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