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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package com.vaadin.terminal.gwt.client;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import com.google.gwt.core.client.GWT;
  6. import com.google.gwt.core.client.JavaScriptObject;
  7. public class ApplicationConfiguration {
  8. // can only be inited once, to avoid multiple-entrypoint-problem
  9. private static WidgetSet initedWidgetSet;
  10. private String id;
  11. private String themeUri;
  12. private String pathInfo;
  13. private String appUri;
  14. private JavaScriptObject versionInfo;
  15. private String windowName;
  16. private String communicationErrorCaption;
  17. private String communicationErrorMessage;
  18. private String communicationErrorUrl;
  19. private boolean useDebugIdInDom = true;
  20. private static ArrayList<ApplicationConnection> unstartedApplications = new ArrayList<ApplicationConnection>();
  21. private static ArrayList<ApplicationConnection> runningApplications = new ArrayList<ApplicationConnection>();
  22. public String getRootPanelId() {
  23. return id;
  24. }
  25. public String getApplicationUri() {
  26. return appUri;
  27. }
  28. public String getPathInfo() {
  29. return pathInfo;
  30. }
  31. public String getThemeUri() {
  32. return themeUri;
  33. }
  34. public void setAppId(String appId) {
  35. id = appId;
  36. }
  37. public void setInitialWindowName(String name) {
  38. windowName = name;
  39. }
  40. public String getInitialWindowName() {
  41. return windowName;
  42. }
  43. public JavaScriptObject getVersionInfoJSObject() {
  44. return versionInfo;
  45. }
  46. public String getCommunicationErrorCaption() {
  47. return communicationErrorCaption;
  48. }
  49. public String getCommunicationErrorMessage() {
  50. return communicationErrorMessage;
  51. }
  52. public String getCommunicationErrorUrl() {
  53. return communicationErrorUrl;
  54. }
  55. private native void loadFromDOM()
  56. /*-{
  57. var id = this.@com.vaadin.terminal.gwt.client.ApplicationConfiguration::id;
  58. if($wnd.vaadin.toolkitConfigurations && $wnd.vaadin.toolkitConfigurations[id]) {
  59. var jsobj = $wnd.vaadin.toolkitConfigurations[id];
  60. var uri = jsobj.appUri;
  61. if(uri[uri.length -1] != "/") {
  62. uri = uri + "/";
  63. }
  64. this.@com.vaadin.terminal.gwt.client.ApplicationConfiguration::appUri = uri;
  65. this.@com.vaadin.terminal.gwt.client.ApplicationConfiguration::pathInfo = jsobj.pathInfo;
  66. this.@com.vaadin.terminal.gwt.client.ApplicationConfiguration::themeUri = jsobj.themeUri;
  67. if(jsobj.windowName) {
  68. this.@com.vaadin.terminal.gwt.client.ApplicationConfiguration::windowName = jsobj.windowName;
  69. }
  70. if('useDebugIdInDom' in jsobj && typeof(jsobj.useDebugIdInDom) == "boolean") {
  71. this.@com.vaadin.terminal.gwt.client.ApplicationConfiguration::useDebugIdInDom = jsobj.useDebugIdInDom;
  72. }
  73. if(jsobj.versionInfo) {
  74. this.@com.vaadin.terminal.gwt.client.ApplicationConfiguration::versionInfo = jsobj.versionInfo;
  75. }
  76. if(jsobj.comErrMsg) {
  77. this.@com.vaadin.terminal.gwt.client.ApplicationConfiguration::communicationErrorCaption = jsobj.comErrMsg.caption;
  78. this.@com.vaadin.terminal.gwt.client.ApplicationConfiguration::communicationErrorMessage = jsobj.comErrMsg.message;
  79. this.@com.vaadin.terminal.gwt.client.ApplicationConfiguration::communicationErrorUrl = jsobj.comErrMsg.url;
  80. }
  81. } else {
  82. $wnd.alert("Vaadin app failed to initialize: " + this.id);
  83. }
  84. }-*/;
  85. /**
  86. * Inits the ApplicationConfiguration by reading the DOM and instantiating
  87. * ApplicationConenctions accordingly. Call {@link #startNextApplication()}
  88. * to actually start the applications.
  89. *
  90. * @param widgetset
  91. * the widgetset that is running the apps
  92. */
  93. public static void initConfigurations(WidgetSet widgetset) {
  94. String wsname = widgetset.getClass().getName();
  95. String module = GWT.getModuleName();
  96. int lastdot = module.lastIndexOf(".");
  97. String base = module.substring(0, lastdot);
  98. String simpleName = module.substring(lastdot + 1);
  99. if (!wsname.startsWith(base) || !wsname.endsWith(simpleName)) {
  100. // WidgetSet module name does not match implementation name;
  101. // probably inherited WidgetSet with entry-point. Skip.
  102. GWT.log("Ignored init for " + wsname + " when starting " + module,
  103. null);
  104. return;
  105. }
  106. if (initedWidgetSet != null) {
  107. // Something went wrong: multiple widgetsets inited
  108. String msg = "Tried to init " + widgetset.getClass().getName()
  109. + ", but " + initedWidgetSet.getClass().getName()
  110. + " is already inited.";
  111. System.err.println(msg);
  112. throw new IllegalStateException(msg);
  113. }
  114. initedWidgetSet = widgetset;
  115. ArrayList<String> appIds = new ArrayList<String>();
  116. loadAppIdListFromDOM(appIds);
  117. for (Iterator<String> it = appIds.iterator(); it.hasNext();) {
  118. String appId = it.next();
  119. ApplicationConfiguration appConf = getConfigFromDOM(appId);
  120. ApplicationConnection a = new ApplicationConnection(widgetset,
  121. appConf);
  122. unstartedApplications.add(a);
  123. }
  124. }
  125. /**
  126. * Starts the next unstarted application. The WidgetSet should call this
  127. * once to start the first application; after that, each application should
  128. * call this once it has started. This ensures that the applications are
  129. * started synchronously, which is neccessary to avoid session-id problems.
  130. *
  131. * @return true if an unstarted application was found
  132. */
  133. public static boolean startNextApplication() {
  134. if (unstartedApplications.size() > 0) {
  135. ApplicationConnection a = unstartedApplications.remove(0);
  136. a.start();
  137. runningApplications.add(a);
  138. return true;
  139. } else {
  140. return false;
  141. }
  142. }
  143. public static List<ApplicationConnection> getRunningApplications() {
  144. return runningApplications;
  145. }
  146. private native static void loadAppIdListFromDOM(ArrayList<String> list)
  147. /*-{
  148. var j;
  149. for(j in $wnd.vaadin.toolkitConfigurations) {
  150. list.@java.util.Collection::add(Ljava/lang/Object;)(j);
  151. }
  152. }-*/;
  153. public static ApplicationConfiguration getConfigFromDOM(String appId) {
  154. ApplicationConfiguration conf = new ApplicationConfiguration();
  155. conf.setAppId(appId);
  156. conf.loadFromDOM();
  157. return conf;
  158. }
  159. public native String getServletVersion()
  160. /*-{
  161. return this.@com.vaadin.terminal.gwt.client.ApplicationConfiguration::versionInfo.vaadinVersion;
  162. }-*/;
  163. public native String getApplicationVersion()
  164. /*-{
  165. return this.@com.vaadin.terminal.gwt.client.ApplicationConfiguration::versionInfo.applicationVersion;
  166. }-*/;
  167. public boolean useDebugIdInDOM() {
  168. return useDebugIdInDom;
  169. }
  170. }