Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ApplicationConfiguration.java 21KB

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