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

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