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.9KB

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