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

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