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

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