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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal.gwt.server;
  5. import java.io.File;
  6. import java.io.PrintWriter;
  7. import java.io.StringWriter;
  8. import java.util.Collection;
  9. import java.util.Collections;
  10. import java.util.HashSet;
  11. import java.util.Iterator;
  12. import java.util.LinkedList;
  13. import java.util.List;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpSession;
  16. import javax.servlet.http.HttpSessionBindingEvent;
  17. import javax.servlet.http.HttpSessionBindingListener;
  18. import com.itmill.toolkit.Application;
  19. import com.itmill.toolkit.service.ApplicationContext;
  20. import com.itmill.toolkit.terminal.gwt.client.ClientExceptionHandler;
  21. /**
  22. * Web application context for the IT Mill Toolkit applications.
  23. *
  24. * @author IT Mill Ltd.
  25. * @version
  26. * @VERSION@
  27. * @since 3.1
  28. */
  29. public class WebApplicationContext implements ApplicationContext,
  30. HttpSessionBindingListener {
  31. protected List listeners;
  32. protected HttpSession session;
  33. protected final HashSet applications = new HashSet();
  34. protected WebBrowser browser = new WebBrowser();
  35. /**
  36. * Creates a new Web Application Context.
  37. *
  38. */
  39. WebApplicationContext() {
  40. }
  41. /**
  42. * Gets the application context base directory.
  43. *
  44. * @see com.itmill.toolkit.service.ApplicationContext#getBaseDirectory()
  45. */
  46. public File getBaseDirectory() {
  47. final String realPath = ApplicationServlet.getResourcePath(session
  48. .getServletContext(), "/");
  49. if (realPath == null) {
  50. return null;
  51. }
  52. return new File(realPath);
  53. }
  54. /**
  55. * Gets the http-session application is running in.
  56. *
  57. * @return HttpSession this application context resides in.
  58. */
  59. public HttpSession getHttpSession() {
  60. return session;
  61. }
  62. /**
  63. * Gets the applications in this context.
  64. *
  65. * @see com.itmill.toolkit.service.ApplicationContext#getApplications()
  66. */
  67. public Collection getApplications() {
  68. return Collections.unmodifiableCollection(applications);
  69. }
  70. /**
  71. * Gets the application context for HttpSession.
  72. *
  73. * @param session
  74. * the HTTP session.
  75. * @return the application context for HttpSession.
  76. */
  77. static public WebApplicationContext getApplicationContext(
  78. HttpSession session) {
  79. WebApplicationContext cx = (WebApplicationContext) session
  80. .getAttribute(WebApplicationContext.class.getName());
  81. if (cx == null) {
  82. cx = new WebApplicationContext();
  83. session.setAttribute(WebApplicationContext.class.getName(), cx);
  84. }
  85. if (cx.session == null) {
  86. cx.session = session;
  87. }
  88. return cx;
  89. }
  90. /**
  91. * Returns <code>true</code> if and only if the argument is not
  92. * <code>null</code> and is a Boolean object that represents the same
  93. * boolean value as this object.
  94. *
  95. * @param obj
  96. * the object to compare with.
  97. * @see java.lang.Object#equals(java.lang.Object)
  98. */
  99. public boolean equals(Object obj) {
  100. return session.equals(obj);
  101. }
  102. /**
  103. * Returns the hash code value .
  104. *
  105. * @see java.lang.Object#hashCode()
  106. */
  107. public int hashCode() {
  108. return session.hashCode();
  109. }
  110. /**
  111. * Adds the transaction listener to this context.
  112. *
  113. * @see com.itmill.toolkit.service.ApplicationContext#addTransactionListener(com.itmill.toolkit.service.ApplicationContext.TransactionListener)
  114. */
  115. public void addTransactionListener(TransactionListener listener) {
  116. if (listeners == null) {
  117. listeners = new LinkedList();
  118. }
  119. listeners.add(listener);
  120. }
  121. /**
  122. * Removes the transaction listener from this context.
  123. *
  124. * @see com.itmill.toolkit.service.ApplicationContext#removeTransactionListener(com.itmill.toolkit.service.ApplicationContext.TransactionListener)
  125. */
  126. public void removeTransactionListener(TransactionListener listener) {
  127. if (listeners != null) {
  128. listeners.remove(listener);
  129. }
  130. }
  131. /**
  132. * Notifies the transaction start.
  133. *
  134. * @param application
  135. * @param request
  136. * the HTTP request.
  137. */
  138. protected void startTransaction(Application application,
  139. HttpServletRequest request) {
  140. if (listeners == null) {
  141. return;
  142. }
  143. for (final Iterator i = listeners.iterator(); i.hasNext();) {
  144. ((ApplicationContext.TransactionListener) i.next())
  145. .transactionStart(application, request);
  146. }
  147. }
  148. /**
  149. * Notifies the transaction end.
  150. *
  151. * @param application
  152. * @param request
  153. * the HTTP request.
  154. */
  155. protected void endTransaction(Application application,
  156. HttpServletRequest request) {
  157. if (listeners == null) {
  158. return;
  159. }
  160. LinkedList exceptions = null;
  161. for (final Iterator i = listeners.iterator(); i.hasNext();) {
  162. try {
  163. ((ApplicationContext.TransactionListener) i.next())
  164. .transactionEnd(application, request);
  165. } catch (final RuntimeException t) {
  166. if (exceptions == null) {
  167. exceptions = new LinkedList();
  168. }
  169. exceptions.add(t);
  170. }
  171. }
  172. // If any runtime exceptions occurred, throw a combined exception
  173. if (exceptions != null) {
  174. final StringBuffer msg = new StringBuffer();
  175. for (final Iterator i = exceptions.iterator(); i.hasNext();) {
  176. final RuntimeException e = (RuntimeException) i.next();
  177. if (msg.length() == 0) {
  178. msg.append("\n\n--------------------------\n\n");
  179. }
  180. msg.append(e.getMessage() + "\n");
  181. final StringWriter trace = new StringWriter();
  182. e.printStackTrace(new PrintWriter(trace, true));
  183. msg.append(trace.toString());
  184. }
  185. throw new RuntimeException(msg.toString());
  186. }
  187. }
  188. protected void removeApplication(Application application) {
  189. applications.remove(application);
  190. }
  191. protected void addApplication(Application application) {
  192. applications.add(application);
  193. }
  194. /**
  195. * @see javax.servlet.http.HttpSessionBindingListener#valueBound(HttpSessionBindingEvent)
  196. */
  197. public void valueBound(HttpSessionBindingEvent arg0) {
  198. // We are not interested in bindings
  199. }
  200. /**
  201. * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent)
  202. */
  203. public void valueUnbound(HttpSessionBindingEvent event) {
  204. // If we are going to be unbound from the session, the session must be
  205. // closing
  206. try {
  207. while (!applications.isEmpty()) {
  208. final Application app = (Application) applications.iterator()
  209. .next();
  210. app.close();
  211. ApplicationServlet.applicationToAjaxAppMgrMap.remove(app);
  212. removeApplication(app);
  213. }
  214. } catch (Exception e) {
  215. // This should never happen but is possible with rare
  216. // configurations (e.g. robustness tests). If you have one
  217. // thread doing HTTP socket write and another thread trying to
  218. // remove same application here. Possible if you got e.g. session
  219. // lifetime 1 min but socket write may take longer than 1 min.
  220. // FIXME: Handle exception
  221. System.err.println("Could not remove application, leaking memory.");
  222. e.printStackTrace();
  223. }
  224. }
  225. /**
  226. * Get the web browser associated with this application context.
  227. *
  228. * Because application context is related to the http session and server
  229. * maintains one session per browser-instance, each context has exactly one
  230. * web browser associated with it.
  231. *
  232. * @return
  233. */
  234. public WebBrowser getBrowser() {
  235. return browser;
  236. }
  237. }