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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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. */
  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 || "".equals(serviceUrl)) {
  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(new ScheduledCommand() {
  418. @Override
  419. public void execute() {
  420. Profiler.enter("ApplicationConfiguration.startApplication");
  421. ApplicationConfiguration appConf = getConfigFromDOM(
  422. applicationId);
  423. ApplicationConnection a = GWT
  424. .create(ApplicationConnection.class);
  425. a.init(widgetSet, appConf);
  426. runningApplications.add(a);
  427. Profiler.leave("ApplicationConfiguration.startApplication");
  428. a.start();
  429. }
  430. });
  431. }
  432. public static List<ApplicationConnection> getRunningApplications() {
  433. return runningApplications;
  434. }
  435. /**
  436. * Gets the configuration object for a specific application from the
  437. * bootstrap javascript.
  438. *
  439. * @param appId
  440. * the id of the application to get configuration data for
  441. * @return a native javascript object containing the configuration data
  442. */
  443. private native static JsoConfiguration getJsoConfiguration(String appId)
  444. /*-{
  445. return $wnd.vaadin.getApp(appId);
  446. }-*/;
  447. public static ApplicationConfiguration getConfigFromDOM(String appId) {
  448. ApplicationConfiguration conf = new ApplicationConfiguration();
  449. conf.setAppId(appId);
  450. conf.loadFromDOM();
  451. return conf;
  452. }
  453. public String getServletVersion() {
  454. return getJsoConfiguration(id).getVaadinVersion();
  455. }
  456. /**
  457. * Return Atmosphere version.
  458. *
  459. * @since 7.4
  460. *
  461. * @return Atmosphere version.
  462. */
  463. public String getAtmosphereVersion() {
  464. return getJsoConfiguration(id).getAtmosphereVersion();
  465. }
  466. /**
  467. * Return Atmosphere JS version.
  468. *
  469. * @since 7.4
  470. *
  471. * @return Atmosphere JS version.
  472. */
  473. public String getAtmosphereJSVersion() {
  474. return getJsoConfiguration(id).getAtmosphereJSVersion();
  475. }
  476. public Class<? extends ServerConnector> getConnectorClassByEncodedTag(
  477. int tag) {
  478. Class<? extends ServerConnector> type = classes.get(tag);
  479. if (type == null && !classes.containsKey(tag)) {
  480. // Initialize if not already loaded
  481. Integer currentTag = Integer.valueOf(tag);
  482. while (type == null && currentTag != null) {
  483. String serverSideClassNameForTag = getServerSideClassNameForTag(
  484. currentTag);
  485. if (TypeData.hasIdentifier(serverSideClassNameForTag)) {
  486. try {
  487. type = (Class<? extends ServerConnector>) TypeData
  488. .getClass(serverSideClassNameForTag);
  489. } catch (NoDataException e) {
  490. throw new RuntimeException(e);
  491. }
  492. }
  493. currentTag = getParentTag(currentTag.intValue());
  494. }
  495. if (type == null) {
  496. if (isExtensionType(tag)) {
  497. type = UnknownExtensionConnector.class;
  498. } else {
  499. type = UnknownComponentConnector.class;
  500. }
  501. if (unknownComponents == null) {
  502. unknownComponents = new HashMap<>();
  503. }
  504. unknownComponents.put(tag, getServerSideClassNameForTag(tag));
  505. }
  506. classes.put(tag, type);
  507. }
  508. return type;
  509. }
  510. private boolean isExtensionType(int tag) {
  511. Integer currentTag = Integer.valueOf(tag);
  512. while (currentTag != null) {
  513. String serverSideClassNameForTag = getServerSideClassNameForTag(
  514. currentTag);
  515. if ("com.vaadin.server.AbstractExtension"
  516. .equals(serverSideClassNameForTag)) {
  517. return true;
  518. }
  519. currentTag = getParentTag(currentTag.intValue());
  520. }
  521. return false;
  522. }
  523. public void addComponentInheritanceInfo(ValueMap valueMap) {
  524. JsArrayString keyArray = valueMap.getKeyArray();
  525. for (int i = 0; i < keyArray.length(); i++) {
  526. String key = keyArray.get(i);
  527. int value = valueMap.getInt(key);
  528. componentInheritanceMap.put(Integer.parseInt(key), value);
  529. }
  530. }
  531. public void addComponentMappings(ValueMap valueMap, WidgetSet widgetSet) {
  532. JsArrayString keyArray = valueMap.getKeyArray();
  533. for (int i = 0; i < keyArray.length(); i++) {
  534. String key = keyArray.get(i).intern();
  535. int value = valueMap.getInt(key);
  536. tagToServerSideClassName.put(value, key);
  537. }
  538. for (int i = 0; i < keyArray.length(); i++) {
  539. String key = keyArray.get(i).intern();
  540. int value = valueMap.getInt(key);
  541. widgetSet.ensureConnectorLoaded(value, this);
  542. }
  543. }
  544. /**
  545. * Returns all tags for given class. Tags are used in
  546. * {@link ApplicationConfiguration} to keep track of different classes and
  547. * their hierarchy
  548. *
  549. * @since 7.2
  550. * @param classname
  551. * name of class which tags we want
  552. * @return Integer array of tags pointing to this classname
  553. */
  554. public Integer[] getTagsForServerSideClassName(String classname) {
  555. List<Integer> tags = new ArrayList<>();
  556. for (Map.Entry<Integer, String> entry : tagToServerSideClassName
  557. .entrySet()) {
  558. if (classname.equals(entry.getValue())) {
  559. tags.add(entry.getKey());
  560. }
  561. }
  562. Integer[] out = new Integer[tags.size()];
  563. return tags.toArray(out);
  564. }
  565. public Integer getParentTag(int tag) {
  566. return componentInheritanceMap.get(tag);
  567. }
  568. public String getServerSideClassNameForTag(Integer tag) {
  569. return tagToServerSideClassName.get(tag);
  570. }
  571. String getUnknownServerClassNameByTag(int tag) {
  572. if (unknownComponents != null) {
  573. String className = unknownComponents.get(tag);
  574. if (className == null) {
  575. className = "unknown class with id " + tag;
  576. }
  577. return className;
  578. }
  579. return null;
  580. }
  581. /**
  582. * Runs the given command when all pending dependencies have been loaded, or
  583. * immediately if no dependencies are being loaded.
  584. *
  585. * @since 7.6
  586. * @param command
  587. * the command to run
  588. */
  589. public static void runWhenDependenciesLoaded(Command command) {
  590. if (dependenciesLoading == 0) {
  591. command.execute();
  592. } else {
  593. callbacks.add(command);
  594. }
  595. }
  596. static void startDependencyLoading() {
  597. dependenciesLoading++;
  598. }
  599. static void endDependencyLoading() {
  600. dependenciesLoading--;
  601. if (dependenciesLoading == 0 && !callbacks.isEmpty()) {
  602. for (Command cmd : callbacks) {
  603. cmd.execute();
  604. }
  605. callbacks.clear();
  606. }
  607. }
  608. private boolean vaadinBootstrapLoaded() {
  609. Element window = ScriptInjector.TOP_WINDOW.cast();
  610. return window.getPropertyJSO("vaadin") != null;
  611. }
  612. @Override
  613. public void onModuleLoad() {
  614. // Don't run twice if the module has been inherited several times,
  615. // and don't continue if vaadinBootstrap was not executed.
  616. if (moduleLoaded || !vaadinBootstrapLoaded()) {
  617. getLogger().log(Level.WARNING,
  618. "vaadinBootstrap.js was not loaded, skipping vaadin application configuration.");
  619. return;
  620. }
  621. moduleLoaded = true;
  622. Profiler.initialize();
  623. Profiler.enter("ApplicationConfiguration.onModuleLoad");
  624. BrowserInfo browserInfo = BrowserInfo.get();
  625. // Enable iOS6 cast fix (see #10460)
  626. if (browserInfo.isIOS6() && browserInfo.isWebkit()) {
  627. enableIOS6castFix();
  628. }
  629. // Enable IE prompt fix (#13367)
  630. if (browserInfo.isIE() && browserInfo.getBrowserMajorVersion() >= 10) {
  631. enableIEPromptFix();
  632. }
  633. // Register pointer events (must be done before any events are used)
  634. PointerEventSupport.init();
  635. // Prepare the debugging window
  636. if (isDebugMode()) {
  637. /*
  638. * XXX Lots of implementation details here right now. This should be
  639. * cleared up when an API for extending the debug window is
  640. * implemented.
  641. */
  642. VDebugWindow window = VDebugWindow.get();
  643. if (LogConfiguration.loggingIsEnabled()) {
  644. window.addSection((Section) GWT.create(LogSection.class));
  645. }
  646. window.addSection((Section) GWT.create(InfoSection.class));
  647. window.addSection((Section) GWT.create(HierarchySection.class));
  648. window.addSection((Section) GWT.create(NetworkSection.class));
  649. window.addSection((Section) GWT.create(TestBenchSection.class));
  650. if (Profiler.isEnabled()) {
  651. window.addSection((Section) GWT.create(ProfilerSection.class));
  652. }
  653. if (isQuietDebugMode()) {
  654. window.close();
  655. } else {
  656. // Load debug window styles asynchronously
  657. GWT.runAsync(new RunAsyncCallback() {
  658. @Override
  659. public void onSuccess() {
  660. DebugWindowStyles dws = GWT
  661. .create(DebugWindowStyles.class);
  662. dws.css().ensureInjected();
  663. }
  664. @Override
  665. public void onFailure(Throwable reason) {
  666. Window.alert(
  667. "Failed to load Vaadin debug window styles");
  668. }
  669. });
  670. window.init();
  671. }
  672. // Connect to the legacy API
  673. VConsole.setImplementation(window);
  674. Handler errorNotificationHandler = GWT
  675. .create(ErrorNotificationHandler.class);
  676. Logger.getLogger("").addHandler(errorNotificationHandler);
  677. }
  678. if (LogConfiguration.loggingIsEnabled()) {
  679. GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
  680. @Override
  681. public void onUncaughtException(Throwable e) {
  682. /*
  683. * If the debug window is not enabled (?debug), this will
  684. * not show anything to normal users. "a1 is not an object"
  685. * style errors helps nobody, especially end user. It does
  686. * not work tells just as much.
  687. */
  688. getLogger().log(Level.SEVERE, e.getMessage(), e);
  689. }
  690. });
  691. if (isProductionMode()) {
  692. // Disable all logging if in production mode
  693. Logger.getLogger("").setLevel(Level.OFF);
  694. }
  695. }
  696. Profiler.leave("ApplicationConfiguration.onModuleLoad");
  697. if (SuperDevMode.enableBasedOnParameter()) {
  698. // Do not start any application as super dev mode will refresh the
  699. // page once done compiling
  700. return;
  701. }
  702. registerCallback(GWT.getModuleName());
  703. }
  704. /**
  705. * Fix to iOS6 failing when comparing with 0 directly after the kind of
  706. * comparison done by GWT when a double or float is cast to an int. Forcing
  707. * another trivial operation (other than a compare to 0) after the dangerous
  708. * comparison makes the issue go away. See #10460.
  709. */
  710. private static native void enableIOS6castFix()
  711. /*-{
  712. Math.max = function(a,b) {return (a > b === 1 < 2)? a : b}
  713. Math.min = function(a,b) {return (a < b === 1 < 2)? a : b}
  714. }-*/;
  715. /**
  716. * Make Metro versions of IE suggest switching to the desktop when
  717. * window.prompt is called.
  718. */
  719. private static native void enableIEPromptFix()
  720. /*-{
  721. var prompt = $wnd.prompt;
  722. $wnd.prompt = function () {
  723. var result = prompt.apply($wnd, Array.prototype.slice.call(arguments));
  724. if (result === undefined) {
  725. // force the browser to suggest desktop mode
  726. showModalDialog();
  727. return null;
  728. } else {
  729. return result;
  730. }
  731. };
  732. }-*/;
  733. /**
  734. * Registers that callback that the bootstrap javascript uses to start
  735. * applications once the widgetset is loaded and all required information is
  736. * available
  737. *
  738. * @param widgetsetName
  739. * the name of this widgetset
  740. */
  741. public native static void registerCallback(String widgetsetName)
  742. /*-{
  743. var callbackHandler = $entry(@com.vaadin.client.ApplicationConfiguration::startApplication(Ljava/lang/String;));
  744. $wnd.vaadin.registerWidgetset(widgetsetName, callbackHandler);
  745. }-*/;
  746. /**
  747. * Checks if client side is in debug mode. Practically this is invoked by
  748. * adding ?debug parameter to URI. Please note that debug mode is always
  749. * disabled if production mode is enabled, but disabling production mode
  750. * does not automatically enable debug mode.
  751. *
  752. * @see #isProductionMode()
  753. *
  754. * @return true if client side is currently been debugged
  755. */
  756. public static boolean isDebugMode() {
  757. return isDebugAvailable()
  758. && Window.Location.getParameter("debug") != null;
  759. }
  760. /**
  761. * Checks if production mode is enabled. When production mode is enabled,
  762. * client-side logging is disabled. There may also be other performance
  763. * optimizations.
  764. *
  765. * @since 7.1.2
  766. * @return <code>true</code> if production mode is enabled; otherwise
  767. * <code>false</code>.
  768. */
  769. public static boolean isProductionMode() {
  770. return !isDebugAvailable();
  771. }
  772. private native static boolean isDebugAvailable()
  773. /*-{
  774. if($wnd.vaadin.debug) {
  775. return true;
  776. } else {
  777. return false;
  778. }
  779. }-*/;
  780. /**
  781. * Checks whether debug logging should be quiet
  782. *
  783. * @return <code>true</code> if debug logging should be quiet
  784. */
  785. public static boolean isQuietDebugMode() {
  786. String debugParameter = Window.Location.getParameter("debug");
  787. return isDebugAvailable() && debugParameter != null
  788. && debugParameter.startsWith("q");
  789. }
  790. /**
  791. * Checks whether the widget set version has been sent to the server. It is
  792. * sent in the first UIDL request.
  793. *
  794. * @return <code>true</code> if browser information has already been sent
  795. */
  796. public boolean isWidgetsetVersionSent() {
  797. return widgetsetVersionSent;
  798. }
  799. /**
  800. * Registers that the widget set version has been sent to the server.
  801. */
  802. public void setWidgetsetVersionSent() {
  803. widgetsetVersionSent = true;
  804. }
  805. private static final Logger getLogger() {
  806. return Logger.getLogger(ApplicationConfiguration.class.getName());
  807. }
  808. }