Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ApplicationConfiguration.java 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.logging.Handler;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import com.google.gwt.core.client.EntryPoint;
  26. import com.google.gwt.core.client.GWT;
  27. import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
  28. import com.google.gwt.core.client.JavaScriptObject;
  29. import com.google.gwt.core.client.JsArrayString;
  30. import com.google.gwt.core.client.RunAsyncCallback;
  31. import com.google.gwt.core.client.Scheduler;
  32. import com.google.gwt.core.client.ScriptInjector;
  33. import com.google.gwt.dom.client.Element;
  34. import com.google.gwt.logging.client.LogConfiguration;
  35. import com.google.gwt.user.client.Command;
  36. import com.google.gwt.user.client.Window;
  37. import com.vaadin.client.debug.internal.ErrorNotificationHandler;
  38. import com.vaadin.client.debug.internal.HierarchySection;
  39. import com.vaadin.client.debug.internal.InfoSection;
  40. import com.vaadin.client.debug.internal.LogSection;
  41. import com.vaadin.client.debug.internal.NetworkSection;
  42. import com.vaadin.client.debug.internal.ProfilerSection;
  43. import com.vaadin.client.debug.internal.Section;
  44. import com.vaadin.client.debug.internal.TestBenchSection;
  45. import com.vaadin.client.debug.internal.VDebugWindow;
  46. import com.vaadin.client.debug.internal.theme.DebugWindowStyles;
  47. import com.vaadin.client.event.PointerEventSupport;
  48. import com.vaadin.client.metadata.NoDataException;
  49. import com.vaadin.client.metadata.TypeData;
  50. import com.vaadin.client.ui.UnknownComponentConnector;
  51. import com.vaadin.client.ui.UnknownExtensionConnector;
  52. import com.vaadin.client.ui.ui.UIConnector;
  53. import com.vaadin.shared.ApplicationConstants;
  54. import com.vaadin.shared.ui.ui.UIConstants;
  55. public class ApplicationConfiguration implements EntryPoint {
  56. /**
  57. * Helper class for reading configuration options from the bootstap
  58. * javascript
  59. *
  60. * @since 7.0
  61. */
  62. private static class JsoConfiguration extends JavaScriptObject {
  63. protected JsoConfiguration() {
  64. // JSO Constructor
  65. }
  66. /**
  67. * Reads a configuration parameter as a string. Please note that the
  68. * javascript value of the parameter should also be a string, or else an
  69. * undefined exception may be thrown.
  70. *
  71. * @param name
  72. * name of the configuration parameter
  73. * @return value of the configuration parameter, or <code>null</code> if
  74. * not defined
  75. */
  76. private native String getConfigString(String name)
  77. /*-{
  78. var value = this.getConfig(name);
  79. if (value === null || value === undefined) {
  80. return null;
  81. } else {
  82. return value +"";
  83. }
  84. }-*/;
  85. /**
  86. * Reads a configuration parameter as a boolean object. Please note that
  87. * the javascript value of the parameter should also be a boolean, or
  88. * else an undefined exception may be thrown.
  89. *
  90. * @param name
  91. * name of the configuration parameter
  92. * @return boolean value of the configuration paramter, or
  93. * <code>null</code> if no value is defined
  94. */
  95. private native Boolean getConfigBoolean(String name)
  96. /*-{
  97. var value = this.getConfig(name);
  98. if (value === null || value === undefined) {
  99. return null;
  100. } else {
  101. // $entry not needed as function is not exported
  102. return @java.lang.Boolean::valueOf(Z)(value);
  103. }
  104. }-*/;
  105. /**
  106. * Reads a configuration parameter as an integer object. Please note
  107. * that the javascript value of the parameter should also be an integer,
  108. * or else an undefined exception may be thrown.
  109. *
  110. * @param name
  111. * name of the configuration parameter
  112. * @return integer value of the configuration paramter, or
  113. * <code>null</code> if no value is defined
  114. */
  115. private native Integer getConfigInteger(String name)
  116. /*-{
  117. var value = this.getConfig(name);
  118. if (value === null || value === undefined) {
  119. return null;
  120. } else {
  121. // $entry not needed as function is not exported
  122. return @java.lang.Integer::valueOf(I)(value);
  123. }
  124. }-*/;
  125. /**
  126. * Reads a configuration parameter as an {@link ErrorMessage} object.
  127. * Please note that the javascript value of the parameter should also be
  128. * an object with appropriate fields, or else an undefined exception may
  129. * be thrown when calling this method or when calling methods on the
  130. * returned object.
  131. *
  132. * @param name
  133. * name of the configuration parameter
  134. * @return error message with the given name, or <code>null</code> if no
  135. * value is defined
  136. */
  137. private native ErrorMessage getConfigError(String name)
  138. /*-{
  139. return this.getConfig(name);
  140. }-*/;
  141. /**
  142. * Returns a native javascript object containing version information
  143. * from the server.
  144. *
  145. * @return a javascript object with the version information
  146. */
  147. private native JavaScriptObject getVersionInfoJSObject()
  148. /*-{
  149. return this.getConfig("versionInfo");
  150. }-*/;
  151. /**
  152. * Gets the version of the Vaadin framework used on the server.
  153. *
  154. * @return a string with the version
  155. *
  156. * @see com.vaadin.server.VaadinServlet#VERSION
  157. */
  158. private native String getVaadinVersion()
  159. /*-{
  160. return this.getConfig("versionInfo").vaadinVersion;
  161. }-*/;
  162. /**
  163. * Gets the version of the Atmosphere framework.
  164. *
  165. * @return a string with the version
  166. *
  167. * @see org.atmosphere.util#getRawVersion()
  168. */
  169. private native String getAtmosphereVersion()
  170. /*-{
  171. return this.getConfig("versionInfo").atmosphereVersion;
  172. }-*/;
  173. /**
  174. * Gets the JS version used in the Atmosphere framework.
  175. *
  176. * @return a string with the version
  177. */
  178. private native String getAtmosphereJSVersion()
  179. /*-{
  180. if ($wnd.vaadinPush && $wnd.vaadinPush.atmosphere) {
  181. return $wnd.vaadinPush.atmosphere.version;
  182. } else {
  183. return null;
  184. }
  185. }-*/;
  186. private native String getUIDL()
  187. /*-{
  188. return this.getConfig("uidl");
  189. }-*/;
  190. }
  191. /**
  192. * Wraps a native javascript object containing fields for an error message.
  193. *
  194. * @since 7.0
  195. */
  196. public static final class ErrorMessage extends JavaScriptObject {
  197. protected ErrorMessage() {
  198. // JSO constructor
  199. }
  200. public final native String getCaption()
  201. /*-{
  202. return this.caption;
  203. }-*/;
  204. public final native String getMessage()
  205. /*-{
  206. return this.message;
  207. }-*/;
  208. public final native String getUrl()
  209. /*-{
  210. return this.url;
  211. }-*/;
  212. }
  213. private static WidgetSet widgetSet = GWT.create(WidgetSet.class);
  214. private String id;
  215. /**
  216. * The URL to the VAADIN directory containing themes and widgetsets. Should
  217. * always end with a slash (/).
  218. */
  219. private String vaadinDirUrl;
  220. private String frontendUrl;
  221. private String serviceUrl;
  222. private String contextRootUrl;
  223. private int uiId;
  224. private boolean standalone;
  225. private ErrorMessage communicationError;
  226. private ErrorMessage authorizationError;
  227. private ErrorMessage sessionExpiredError;
  228. private int heartbeatInterval;
  229. private Map<Integer, String> unknownComponents;
  230. private Map<Integer, Class<? extends ServerConnector>> classes = new HashMap<>();
  231. private boolean widgetsetVersionSent = false;
  232. private static boolean moduleLoaded = false;
  233. static// TODO consider to make this hashmap per application
  234. List<Command> callbacks = new LinkedList<>();
  235. private static int dependenciesLoading;
  236. private static List<ApplicationConnection> runningApplications = new ArrayList<>();
  237. private Map<Integer, Integer> componentInheritanceMap = new HashMap<>();
  238. private Map<Integer, String> tagToServerSideClassName = new HashMap<>();
  239. /**
  240. * Checks whether path info in requests to the server-side service should be
  241. * in a request parameter (named <code>v-resourcePath</code>) or appended to
  242. * the end of the service URL.
  243. *
  244. * @see #getServiceUrl()
  245. *
  246. * @return <code>true</code> if path info should be a request parameter;
  247. * <code>false</code> if the path info goes after the service URL
  248. */
  249. public boolean useServiceUrlPathParam() {
  250. return getServiceUrlParameterName() != null;
  251. }
  252. /**
  253. * Return the name of the parameter used to to send data to the service url.
  254. * This method should only be called if {@link #useServiceUrlPathParam()} is
  255. * true.
  256. *
  257. * @since 7.1.6
  258. * @return The parameter name, by default <code>v-resourcePath</code>
  259. */
  260. public String getServiceUrlParameterName() {
  261. return getJsoConfiguration(id).getConfigString(
  262. ApplicationConstants.SERVICE_URL_PARAMETER_NAME);
  263. }
  264. public String getRootPanelId() {
  265. return id;
  266. }
  267. /**
  268. * Gets the URL to the server-side VaadinService. If
  269. * {@link #useServiceUrlPathParam()} return <code>true</code>, the requested
  270. * path info should be in the <code>v-resourcePath</code> query parameter;
  271. * else the path info should be appended to the end of the URL.
  272. *
  273. * @see #useServiceUrlPathParam()
  274. *
  275. * @return the URL to the server-side service as a string
  276. */
  277. public String getServiceUrl() {
  278. return serviceUrl;
  279. }
  280. /**
  281. * Gets the URL to the context root of the web application.
  282. *
  283. * @return the URL to the server-side context root as a string
  284. *
  285. * @since 8.0.3
  286. */
  287. public String getContextRootUrl() {
  288. return contextRootUrl;
  289. }
  290. /**
  291. * @return the theme name used when initializing the application
  292. * @deprecated as of 7.3. Use {@link UIConnector#getActiveTheme()} to get
  293. * the theme currently in use
  294. */
  295. @Deprecated
  296. public String getThemeName() {
  297. return getJsoConfiguration(id).getConfigString("theme");
  298. }
  299. /**
  300. * Gets the URL of the VAADIN directory on the server.
  301. *
  302. * @return the URL of the VAADIN directory
  303. */
  304. public String getVaadinDirUrl() {
  305. return vaadinDirUrl;
  306. }
  307. /**
  308. * Gets the URL of the that the {@literal frontend://} protocol should
  309. * resolve to.
  310. *
  311. * @return the URL of the frontend protocol
  312. * @since 8.1
  313. */
  314. public String getFrontendUrl() {
  315. return frontendUrl;
  316. }
  317. public void setAppId(String appId) {
  318. id = appId;
  319. }
  320. /**
  321. * Gets the initial UIDL from the DOM, if it was provided during the init
  322. * process.
  323. *
  324. * @return
  325. */
  326. public String getUIDL() {
  327. return getJsoConfiguration(id).getUIDL();
  328. }
  329. /**
  330. * @return true if the application is served by std. Vaadin servlet and is
  331. * considered to be the only or main content of the host page.
  332. */
  333. public boolean isStandalone() {
  334. return standalone;
  335. }
  336. /**
  337. * Gets the UI id of the server-side UI associated with this client-side
  338. * instance. The UI id should be included in every request originating from
  339. * this instance in order to associate the request with the right UI
  340. * instance on the server.
  341. *
  342. * @return the UI id
  343. */
  344. public int getUIId() {
  345. return uiId;
  346. }
  347. /**
  348. * @return The interval in seconds between heartbeat requests, or a
  349. * non-positive number if heartbeat is disabled.
  350. */
  351. public int getHeartbeatInterval() {
  352. return heartbeatInterval;
  353. }
  354. public JavaScriptObject getVersionInfoJSObject() {
  355. return getJsoConfiguration(id).getVersionInfoJSObject();
  356. }
  357. public ErrorMessage getCommunicationError() {
  358. return communicationError;
  359. }
  360. public ErrorMessage getAuthorizationError() {
  361. return authorizationError;
  362. }
  363. public ErrorMessage getSessionExpiredError() {
  364. return sessionExpiredError;
  365. }
  366. /**
  367. * Reads the configuration values defined by the bootstrap javascript.
  368. */
  369. private void loadFromDOM() {
  370. JsoConfiguration jsoConfiguration = getJsoConfiguration(id);
  371. serviceUrl = jsoConfiguration
  372. .getConfigString(ApplicationConstants.SERVICE_URL);
  373. if (serviceUrl == null || serviceUrl.isEmpty()) {
  374. /*
  375. * Use the current url without query parameters and fragment as the
  376. * default value.
  377. */
  378. serviceUrl = Window.Location.getHref().replaceFirst("[?#].*", "");
  379. } else {
  380. /*
  381. * Resolve potentially relative URLs to ensure they point to the
  382. * desired locations even if the base URL of the page changes later
  383. * (e.g. with pushState)
  384. */
  385. serviceUrl = WidgetUtil.getAbsoluteUrl(serviceUrl);
  386. }
  387. // Ensure there's an ending slash (to make appending e.g. UIDL work)
  388. if (!useServiceUrlPathParam() && !serviceUrl.endsWith("/")) {
  389. serviceUrl += '/';
  390. }
  391. contextRootUrl = jsoConfiguration
  392. .getConfigString(ApplicationConstants.CONTEXT_ROOT_URL);
  393. vaadinDirUrl = WidgetUtil.getAbsoluteUrl(jsoConfiguration
  394. .getConfigString(ApplicationConstants.VAADIN_DIR_URL));
  395. frontendUrl = WidgetUtil.getAbsoluteUrl(jsoConfiguration
  396. .getConfigString(ApplicationConstants.FRONTEND_URL));
  397. uiId = jsoConfiguration.getConfigInteger(UIConstants.UI_ID_PARAMETER)
  398. .intValue();
  399. // null -> false
  400. standalone = jsoConfiguration
  401. .getConfigBoolean("standalone") == Boolean.TRUE;
  402. heartbeatInterval = jsoConfiguration
  403. .getConfigInteger("heartbeatInterval");
  404. communicationError = jsoConfiguration.getConfigError("comErrMsg");
  405. authorizationError = jsoConfiguration.getConfigError("authErrMsg");
  406. sessionExpiredError = jsoConfiguration.getConfigError("sessExpMsg");
  407. }
  408. /**
  409. * Starts the application with a given id by reading the configuration
  410. * options stored by the bootstrap javascript.
  411. *
  412. * @param applicationId
  413. * id of the application to load, this is also the id of the html
  414. * element into which the application should be rendered.
  415. */
  416. public static void startApplication(final String applicationId) {
  417. Scheduler.get().scheduleDeferred(() -> {
  418. Profiler.enter("ApplicationConfiguration.startApplication");
  419. ApplicationConfiguration appConf = getConfigFromDOM(applicationId);
  420. ApplicationConnection a = GWT.create(ApplicationConnection.class);
  421. a.init(widgetSet, appConf);
  422. runningApplications.add(a);
  423. Profiler.leave("ApplicationConfiguration.startApplication");
  424. a.start();
  425. });
  426. }
  427. public static List<ApplicationConnection> getRunningApplications() {
  428. return runningApplications;
  429. }
  430. /**
  431. * Gets the configuration object for a specific application from the
  432. * bootstrap javascript.
  433. *
  434. * @param appId
  435. * the id of the application to get configuration data for
  436. * @return a native javascript object containing the configuration data
  437. */
  438. private static native JsoConfiguration getJsoConfiguration(String appId)
  439. /*-{
  440. return $wnd.vaadin.getApp(appId);
  441. }-*/;
  442. public static ApplicationConfiguration getConfigFromDOM(String appId) {
  443. ApplicationConfiguration conf = new ApplicationConfiguration();
  444. conf.setAppId(appId);
  445. conf.loadFromDOM();
  446. return conf;
  447. }
  448. public String getServletVersion() {
  449. return getJsoConfiguration(id).getVaadinVersion();
  450. }
  451. /**
  452. * Return Atmosphere version.
  453. *
  454. * @since 7.4
  455. *
  456. * @return Atmosphere version.
  457. */
  458. public String getAtmosphereVersion() {
  459. return getJsoConfiguration(id).getAtmosphereVersion();
  460. }
  461. /**
  462. * Return Atmosphere JS version.
  463. *
  464. * @since 7.4
  465. *
  466. * @return Atmosphere JS version.
  467. */
  468. public String getAtmosphereJSVersion() {
  469. return getJsoConfiguration(id).getAtmosphereJSVersion();
  470. }
  471. public Class<? extends ServerConnector> getConnectorClassByEncodedTag(
  472. int tag) {
  473. Class<? extends ServerConnector> type = classes.get(tag);
  474. if (type == null && !classes.containsKey(tag)) {
  475. // Initialize if not already loaded
  476. Integer currentTag = Integer.valueOf(tag);
  477. while (type == null && currentTag != null) {
  478. String serverSideClassNameForTag = getServerSideClassNameForTag(
  479. currentTag);
  480. if (TypeData.hasIdentifier(serverSideClassNameForTag)) {
  481. try {
  482. type = (Class<? extends ServerConnector>) TypeData
  483. .getClass(serverSideClassNameForTag);
  484. } catch (NoDataException e) {
  485. throw new RuntimeException(e);
  486. }
  487. }
  488. currentTag = getParentTag(currentTag.intValue());
  489. }
  490. if (type == null) {
  491. if (isExtensionType(tag)) {
  492. type = UnknownExtensionConnector.class;
  493. } else {
  494. type = UnknownComponentConnector.class;
  495. }
  496. if (unknownComponents == null) {
  497. unknownComponents = new HashMap<>();
  498. }
  499. unknownComponents.put(tag, getServerSideClassNameForTag(tag));
  500. }
  501. classes.put(tag, type);
  502. }
  503. return type;
  504. }
  505. private boolean isExtensionType(int tag) {
  506. Integer currentTag = Integer.valueOf(tag);
  507. while (currentTag != null) {
  508. String serverSideClassNameForTag = getServerSideClassNameForTag(
  509. currentTag);
  510. if ("com.vaadin.server.AbstractExtension"
  511. .equals(serverSideClassNameForTag)) {
  512. return true;
  513. }
  514. currentTag = getParentTag(currentTag.intValue());
  515. }
  516. return false;
  517. }
  518. public void addComponentInheritanceInfo(ValueMap valueMap) {
  519. JsArrayString keyArray = valueMap.getKeyArray();
  520. for (int i = 0; i < keyArray.length(); i++) {
  521. String key = keyArray.get(i);
  522. int value = valueMap.getInt(key);
  523. componentInheritanceMap.put(Integer.parseInt(key), value);
  524. }
  525. }
  526. public void addComponentMappings(ValueMap valueMap, WidgetSet widgetSet) {
  527. JsArrayString keyArray = valueMap.getKeyArray();
  528. for (int i = 0; i < keyArray.length(); i++) {
  529. String key = keyArray.get(i).intern();
  530. int value = valueMap.getInt(key);
  531. tagToServerSideClassName.put(value, key);
  532. }
  533. for (int i = 0; i < keyArray.length(); i++) {
  534. String key = keyArray.get(i).intern();
  535. int value = valueMap.getInt(key);
  536. widgetSet.ensureConnectorLoaded(value, this);
  537. }
  538. }
  539. /**
  540. * Returns all tags for given class. Tags are used in
  541. * {@link ApplicationConfiguration} to keep track of different classes and
  542. * their hierarchy
  543. *
  544. * @since 7.2
  545. * @param classname
  546. * name of class which tags we want
  547. * @return Integer array of tags pointing to this classname
  548. */
  549. public Integer[] getTagsForServerSideClassName(String classname) {
  550. List<Integer> tags = new ArrayList<>();
  551. for (Map.Entry<Integer, String> entry : tagToServerSideClassName
  552. .entrySet()) {
  553. if (classname.equals(entry.getValue())) {
  554. tags.add(entry.getKey());
  555. }
  556. }
  557. Integer[] out = new Integer[tags.size()];
  558. return tags.toArray(out);
  559. }
  560. public Integer getParentTag(int tag) {
  561. return componentInheritanceMap.get(tag);
  562. }
  563. public String getServerSideClassNameForTag(Integer tag) {
  564. return tagToServerSideClassName.get(tag);
  565. }
  566. String getUnknownServerClassNameByTag(int tag) {
  567. if (unknownComponents != null) {
  568. String className = unknownComponents.get(tag);
  569. if (className == null) {
  570. className = "unknown class with id " + tag;
  571. }
  572. return className;
  573. }
  574. return null;
  575. }
  576. /**
  577. * Runs the given command when all pending dependencies have been loaded, or
  578. * immediately if no dependencies are being loaded.
  579. *
  580. * @since 7.6
  581. * @param command
  582. * the command to run
  583. */
  584. public static void runWhenDependenciesLoaded(Command command) {
  585. if (dependenciesLoading == 0) {
  586. command.execute();
  587. } else {
  588. callbacks.add(command);
  589. }
  590. }
  591. static void startDependencyLoading() {
  592. dependenciesLoading++;
  593. }
  594. static void endDependencyLoading() {
  595. dependenciesLoading--;
  596. if (dependenciesLoading == 0 && !callbacks.isEmpty()) {
  597. for (Command cmd : callbacks) {
  598. cmd.execute();
  599. }
  600. callbacks.clear();
  601. }
  602. }
  603. private boolean vaadinBootstrapLoaded() {
  604. Element window = ScriptInjector.TOP_WINDOW.cast();
  605. return window.getPropertyJSO("vaadin") != null;
  606. }
  607. @Override
  608. public void onModuleLoad() {
  609. // Don't run twice if the module has been inherited several times,
  610. // and don't continue if vaadinBootstrap was not executed.
  611. if (moduleLoaded || !vaadinBootstrapLoaded()) {
  612. getLogger().log(Level.WARNING,
  613. "vaadinBootstrap.js was not loaded, skipping vaadin application configuration.");
  614. return;
  615. }
  616. moduleLoaded = true;
  617. Profiler.initialize();
  618. Profiler.enter("ApplicationConfiguration.onModuleLoad");
  619. BrowserInfo browserInfo = BrowserInfo.get();
  620. // Enable iOS6 cast fix (see #10460)
  621. if (browserInfo.isIOS6() && browserInfo.isWebkit()) {
  622. enableIOS6castFix();
  623. }
  624. // Enable IE prompt fix (#13367)
  625. if (browserInfo.isIE() && browserInfo.getBrowserMajorVersion() >= 10) {
  626. enableIEPromptFix();
  627. }
  628. // Register pointer events (must be done before any events are used)
  629. PointerEventSupport.init();
  630. if (LogConfiguration.loggingIsEnabled()) {
  631. GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
  632. @Override
  633. public void onUncaughtException(Throwable e) {
  634. /*
  635. * If the debug window is not enabled (?debug), this will
  636. * not show anything to normal users. "a1 is not an object"
  637. * style errors helps nobody, especially end user. It does
  638. * not work tells just as much.
  639. */
  640. getLogger().log(Level.SEVERE, e.getMessage(), e);
  641. }
  642. });
  643. if (isProductionMode()) {
  644. // Disable all logging if in production mode
  645. Logger.getLogger("").setLevel(Level.OFF);
  646. }
  647. }
  648. Profiler.leave("ApplicationConfiguration.onModuleLoad");
  649. if (SuperDevMode.enableBasedOnParameter()) {
  650. // Do not start any application as super dev mode will refresh the
  651. // page once done compiling
  652. return;
  653. }
  654. if (isDebugMode()) {
  655. // Load debug window bundle and continue the bootstrap sequence once
  656. // it's loaded
  657. GWT.runAsync(VDebugWindow.class, new RunAsyncCallback() {
  658. @Override
  659. public void onSuccess() {
  660. initDebugWindow();
  661. registerCallback(GWT.getModuleName());
  662. }
  663. @Override
  664. public void onFailure(Throwable reason) {
  665. Window.alert("Failed to load Vaadin debug window");
  666. registerCallback(GWT.getModuleName());
  667. }
  668. });
  669. } else {
  670. // Continue the bootstrap sequence right away
  671. registerCallback(GWT.getModuleName());
  672. }
  673. }
  674. private static void initDebugWindow() {
  675. /*
  676. * XXX Lots of implementation details here right now. This should be
  677. * cleared up when an API for extending the debug window is implemented.
  678. */
  679. VDebugWindow window = VDebugWindow.get();
  680. if (LogConfiguration.loggingIsEnabled()) {
  681. window.addSection((Section) GWT.create(LogSection.class));
  682. }
  683. window.addSection((Section) GWT.create(InfoSection.class));
  684. window.addSection((Section) GWT.create(HierarchySection.class));
  685. window.addSection((Section) GWT.create(NetworkSection.class));
  686. window.addSection((Section) GWT.create(TestBenchSection.class));
  687. if (Profiler.isEnabled()) {
  688. window.addSection((Section) GWT.create(ProfilerSection.class));
  689. }
  690. if (isQuietDebugMode()) {
  691. window.close();
  692. } else {
  693. DebugWindowStyles dws = GWT.create(DebugWindowStyles.class);
  694. dws.css().ensureInjected();
  695. window.init();
  696. }
  697. // Connect to the legacy API
  698. VConsole.setImplementation(window);
  699. Handler errorNotificationHandler = GWT
  700. .create(ErrorNotificationHandler.class);
  701. Logger.getLogger("").addHandler(errorNotificationHandler);
  702. }
  703. /**
  704. * Fix to iOS6 failing when comparing with 0 directly after the kind of
  705. * comparison done by GWT when a double or float is cast to an int. Forcing
  706. * another trivial operation (other than a compare to 0) after the dangerous
  707. * comparison makes the issue go away. See #10460.
  708. */
  709. private static native void enableIOS6castFix()
  710. /*-{
  711. Math.max = function(a,b) {return (a > b === 1 < 2)? a : b}
  712. Math.min = function(a,b) {return (a < b === 1 < 2)? a : b}
  713. }-*/;
  714. /**
  715. * Make Metro versions of IE suggest switching to the desktop when
  716. * window.prompt is called.
  717. */
  718. private static native void enableIEPromptFix()
  719. /*-{
  720. var prompt = $wnd.prompt;
  721. $wnd.prompt = function () {
  722. var result = prompt.apply($wnd, Array.prototype.slice.call(arguments));
  723. if (result === undefined) {
  724. // force the browser to suggest desktop mode
  725. showModalDialog();
  726. return null;
  727. } else {
  728. return result;
  729. }
  730. };
  731. }-*/;
  732. /**
  733. * Registers that callback that the bootstrap javascript uses to start
  734. * applications once the widgetset is loaded and all required information is
  735. * available.
  736. *
  737. * @param widgetsetName
  738. * the name of this widgetset
  739. */
  740. public static native void registerCallback(String widgetsetName)
  741. /*-{
  742. var callbackHandler = $entry(@com.vaadin.client.ApplicationConfiguration::startApplication(Ljava/lang/String;));
  743. $wnd.vaadin.registerWidgetset(widgetsetName, callbackHandler);
  744. }-*/;
  745. /**
  746. * Checks if client side is in debug mode. Practically this is invoked by
  747. * adding ?debug parameter to URI. Please note that debug mode is always
  748. * disabled if production mode is enabled, but disabling production mode
  749. * does not automatically enable debug mode.
  750. *
  751. * @see #isProductionMode()
  752. *
  753. * @return true if client side is currently been debugged
  754. */
  755. public static boolean isDebugMode() {
  756. return isDebugAvailable()
  757. && Window.Location.getParameter("debug") != null;
  758. }
  759. /**
  760. * Checks if production mode is enabled. When production mode is enabled,
  761. * client-side logging is disabled. There may also be other performance
  762. * optimizations.
  763. *
  764. * @since 7.1.2
  765. * @return <code>true</code> if production mode is enabled; otherwise
  766. * <code>false</code>.
  767. */
  768. public static boolean isProductionMode() {
  769. return !isDebugAvailable();
  770. }
  771. private static native boolean isDebugAvailable()
  772. /*-{
  773. return $wnd.vaadin.debug;
  774. }-*/;
  775. /**
  776. * Checks whether debug logging should be quiet.
  777. *
  778. * @return <code>true</code> if debug logging should be quiet
  779. */
  780. public static boolean isQuietDebugMode() {
  781. String debugParameter = Window.Location.getParameter("debug");
  782. return isDebugAvailable() && debugParameter != null
  783. && debugParameter.startsWith("q");
  784. }
  785. /**
  786. * Checks whether the widget set version has been sent to the server. It is
  787. * sent in the first UIDL request.
  788. *
  789. * @return <code>true</code> if browser information has already been sent
  790. */
  791. public boolean isWidgetsetVersionSent() {
  792. return widgetsetVersionSent;
  793. }
  794. /**
  795. * Registers that the widget set version has been sent to the server.
  796. */
  797. public void setWidgetsetVersionSent() {
  798. widgetsetVersionSent = true;
  799. }
  800. private static final Logger getLogger() {
  801. return Logger.getLogger(ApplicationConfiguration.class.getName());
  802. }
  803. }