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.

WebApplicationContext.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* *************************************************************************
  2. IT Mill Toolkit
  3. Development of Browser User Interfaces Made Easy
  4. Copyright (C) 2000-2006 IT Mill Ltd
  5. *************************************************************************
  6. This product is distributed under commercial license that can be found
  7. from the product package on license.pdf. Use of this product might
  8. require purchasing a commercial license from IT Mill Ltd. For guidelines
  9. on usage, see licensing-guidelines.html
  10. *************************************************************************
  11. For more information, contact:
  12. IT Mill Ltd phone: +358 2 4802 7180
  13. Ruukinkatu 2-4 fax: +358 2 4802 7181
  14. 20540, Turku email: info@itmill.com
  15. Finland company www: www.itmill.com
  16. Primary source for information and releases: www.itmill.com
  17. ********************************************************************** */
  18. package com.itmill.toolkit.terminal.gwt.server;
  19. import java.io.File;
  20. import java.io.PrintWriter;
  21. import java.io.StringWriter;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.HashSet;
  25. import java.util.Iterator;
  26. import java.util.LinkedList;
  27. import java.util.List;
  28. import java.util.WeakHashMap;
  29. import javax.servlet.http.HttpServletRequest;
  30. import javax.servlet.http.HttpSession;
  31. import javax.servlet.http.HttpSessionBindingEvent;
  32. import javax.servlet.http.HttpSessionBindingListener;
  33. import com.itmill.toolkit.Application;
  34. import com.itmill.toolkit.service.ApplicationContext;
  35. import com.itmill.toolkit.ui.Window;
  36. /**
  37. * Web application context for the IT Mill Toolkit applications.
  38. *
  39. * @author IT Mill Ltd.
  40. * @version
  41. * @VERSION@
  42. * @since 3.1
  43. */
  44. public class WebApplicationContext implements ApplicationContext,
  45. HttpSessionBindingListener {
  46. private List listeners;
  47. private HttpSession session;
  48. private WeakHashMap formActions = new WeakHashMap();
  49. private HashSet applications = new HashSet();
  50. private WebBrowser browser = new WebBrowser();
  51. /**
  52. * Creates a new Web Application Context.
  53. *
  54. * @param session
  55. * the HTTP session.
  56. */
  57. WebApplicationContext(HttpSession session) {
  58. this.session = session;
  59. }
  60. /**
  61. * Gets the form action for given window.
  62. * <p>
  63. * By default, this action is "", which preserves the current url. Commonly
  64. * this is wanted to be set to <code>application.getUrl.toString</code> or
  65. * <code>window.getUrl.toString</code> in order to clean any local links
  66. * or parameters set from the action.
  67. * </p>
  68. *
  69. * @param window
  70. * the Window for which the action is queried.
  71. * @return the Action to be set into Form action attribute.
  72. */
  73. public String getWindowFormAction(Window window) {
  74. String action = (String) formActions.get(window);
  75. return action == null ? "" : action;
  76. }
  77. /**
  78. * Sets the form action for given window.
  79. * <p>
  80. * By default, this action is "", which preserves the current url. Commonly
  81. * this is wanted to be set to <code>application.getUrl.toString</code> or
  82. * <code>window.getUrl.toString</code> in order to clean any local links
  83. * or parameters set from the action.
  84. * </p>
  85. *
  86. * @param window
  87. * the Window for which the action is set.
  88. * @param action
  89. * the New action for the window.
  90. */
  91. public void setWindowFormAction(Window window, String action) {
  92. if (action == null || action == "")
  93. formActions.remove(window);
  94. else
  95. formActions.put(window, action);
  96. }
  97. /**
  98. * Gets the application context base directory.
  99. *
  100. * @see com.itmill.toolkit.service.ApplicationContext#getBaseDirectory()
  101. */
  102. public File getBaseDirectory() {
  103. String realPath = ApplicationServlet.getResourcePath(session
  104. .getServletContext(), "/");
  105. if (realPath == null)
  106. return null;
  107. return new File(realPath);
  108. }
  109. /**
  110. * Gets the http-session application is running in.
  111. *
  112. * @return HttpSession this application context resides in.
  113. */
  114. public HttpSession getHttpSession() {
  115. return session;
  116. }
  117. /**
  118. * Gets the applications in this context.
  119. *
  120. * @see com.itmill.toolkit.service.ApplicationContext#getApplications()
  121. */
  122. public Collection getApplications() {
  123. return Collections.unmodifiableCollection(applications);
  124. }
  125. /**
  126. * Gets the application context for HttpSession.
  127. *
  128. * @param session
  129. * the HTTP session.
  130. * @return the application context for HttpSession.
  131. */
  132. static public WebApplicationContext getApplicationContext(
  133. HttpSession session) {
  134. WebApplicationContext cx = (WebApplicationContext) session
  135. .getAttribute(WebApplicationContext.class.getName());
  136. if (cx == null) {
  137. cx = new WebApplicationContext(session);
  138. session.setAttribute(WebApplicationContext.class.getName(), cx);
  139. }
  140. return cx;
  141. }
  142. /**
  143. * Returns <code>true</code> if and only if the argument is not
  144. * <code>null</code> and is a Boolean object that represents the same
  145. * boolean value as this object.
  146. *
  147. * @param obj
  148. * the object to compare with.
  149. * @see java.lang.Object#equals(java.lang.Object)
  150. */
  151. public boolean equals(Object obj) {
  152. return session.equals(obj);
  153. }
  154. /**
  155. * Returns the hash code value .
  156. *
  157. * @see java.lang.Object#hashCode()
  158. */
  159. public int hashCode() {
  160. return session.hashCode();
  161. }
  162. /**
  163. * Adds the transaction listener to this context.
  164. *
  165. * @see com.itmill.toolkit.service.ApplicationContext#addTransactionListener(com.itmill.toolkit.service.ApplicationContext.TransactionListener)
  166. */
  167. public void addTransactionListener(TransactionListener listener) {
  168. if (this.listeners == null)
  169. this.listeners = new LinkedList();
  170. this.listeners.add(listener);
  171. }
  172. /**
  173. * Removes the transaction listener from this context.
  174. *
  175. * @see com.itmill.toolkit.service.ApplicationContext#removeTransactionListener(com.itmill.toolkit.service.ApplicationContext.TransactionListener)
  176. */
  177. public void removeTransactionListener(TransactionListener listener) {
  178. if (this.listeners != null)
  179. this.listeners.remove(listener);
  180. }
  181. /**
  182. * Notifies the transaction start.
  183. *
  184. * @param application
  185. * @param request
  186. * the HTTP request.
  187. */
  188. protected void startTransaction(Application application,
  189. HttpServletRequest request) {
  190. if (this.listeners == null)
  191. return;
  192. for (Iterator i = this.listeners.iterator(); i.hasNext();) {
  193. ((ApplicationContext.TransactionListener) i.next())
  194. .transactionStart(application, request);
  195. }
  196. }
  197. /**
  198. * Notifies the transaction end.
  199. *
  200. * @param application
  201. * @param request
  202. * the HTTP request.
  203. */
  204. protected void endTransaction(Application application,
  205. HttpServletRequest request) {
  206. if (this.listeners == null)
  207. return;
  208. LinkedList exceptions = null;
  209. for (Iterator i = this.listeners.iterator(); i.hasNext();)
  210. try {
  211. ((ApplicationContext.TransactionListener) i.next())
  212. .transactionEnd(application, request);
  213. } catch (RuntimeException t) {
  214. if (exceptions == null)
  215. exceptions = new LinkedList();
  216. exceptions.add(t);
  217. }
  218. // If any runtime exceptions occurred, throw a combined exception
  219. if (exceptions != null) {
  220. StringBuffer msg = new StringBuffer();
  221. for (Iterator i = listeners.iterator(); i.hasNext();) {
  222. RuntimeException e = (RuntimeException) i.next();
  223. if (msg.length() == 0)
  224. msg.append("\n\n--------------------------\n\n");
  225. msg.append(e.getMessage() + "\n");
  226. StringWriter trace = new StringWriter();
  227. e.printStackTrace(new PrintWriter(trace, true));
  228. msg.append(trace.toString());
  229. }
  230. throw new RuntimeException(msg.toString());
  231. }
  232. }
  233. protected void removeApplication(Application application) {
  234. applications.remove(application);
  235. }
  236. protected void addApplication(Application application) {
  237. applications.add(application);
  238. }
  239. /**
  240. * @see javax.servlet.http.HttpSessionBindingListener#valueBound(HttpSessionBindingEvent)
  241. */
  242. public void valueBound(HttpSessionBindingEvent arg0) {
  243. // We are not interested in bindings
  244. }
  245. /**
  246. * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent)
  247. */
  248. public void valueUnbound(HttpSessionBindingEvent event) {
  249. // If we are going to be unbound from the session, the session must be
  250. // closing
  251. while (!applications.isEmpty()) {
  252. Application app = (Application) applications.iterator().next();
  253. app.close();
  254. removeApplication(app);
  255. }
  256. }
  257. /**
  258. * Get the web browser associated with this application context.
  259. *
  260. * Because application context is related to the http session and server
  261. * maintains one session per browser-instance, each context has exactly one
  262. * web browser associated with it.
  263. *
  264. * @return
  265. */
  266. public WebBrowser getBrowser() {
  267. return browser;
  268. }
  269. }