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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* *************************************************************************
  2. IT Mill Toolkit
  3. Development of Browser User Intarfaces 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/license.txt. Use of this product might
  8. require purchasing a commercial license from IT Mill Ltd. For guidelines
  9. on usage, see license/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.web;
  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.Iterator;
  25. import java.util.LinkedList;
  26. import java.util.List;
  27. import java.util.WeakHashMap;
  28. import javax.servlet.http.HttpServletRequest;
  29. import javax.servlet.http.HttpSession;
  30. import com.itmill.toolkit.Application;
  31. import com.itmill.toolkit.service.ApplicationContext;
  32. import com.itmill.toolkit.ui.Window;
  33. /**
  34. * Web application context for the IT Mill Toolkit applications.
  35. *
  36. * @author IT Mill Ltd.
  37. * @version
  38. * @VERSION@
  39. * @since 3.1
  40. */
  41. public class WebApplicationContext implements ApplicationContext {
  42. private List listeners;
  43. private HttpSession session;
  44. private WeakHashMap formActions = new WeakHashMap();
  45. /** Create a new Web Application Context. */
  46. WebApplicationContext(HttpSession session) {
  47. this.session = session;
  48. }
  49. /**
  50. * Get the form action for given window.
  51. *
  52. * By default, this action is "", which preserves the current url. Commonly
  53. * this is wanted to be set to <code>application.getUrl().toString()</code>
  54. * or <code>window.getUrl().toString()</code> in order to clean any local
  55. * links or parameters set from the action.
  56. *
  57. * @param window
  58. * Window for which the action is queried
  59. * @return Action to be set into Form action attribute
  60. */
  61. public String getWindowFormAction(Window window) {
  62. String action = (String) formActions.get(window);
  63. return action == null ? "" : action;
  64. }
  65. /**
  66. * Set the form action for given window.
  67. *
  68. * By default, this action is "", which preserves the current url. Commonly
  69. * this is wanted to be set to <code>application.getUrl().toString()</code>
  70. * or <code>window.getUrl().toString()</code> in order to clean any local
  71. * links or parameters set from the action.
  72. *
  73. * @param window
  74. * Window for which the action is set
  75. * @param action
  76. * New action for the window.
  77. */
  78. public void setWindowFormAction(Window window, String action) {
  79. if (action == null || action == "")
  80. formActions.remove(window);
  81. else
  82. formActions.put(window, action);
  83. }
  84. /*
  85. * (non-Javadoc)
  86. *
  87. * @see com.itmill.toolkit.service.ApplicationContext#getBaseDirectory()
  88. */
  89. public File getBaseDirectory() {
  90. String realPath = session.getServletContext().getRealPath("/");
  91. if (realPath == null)
  92. return null;
  93. return new File(realPath);
  94. }
  95. /**
  96. * Get the http-session application is running in.
  97. *
  98. * @return HttpSession this application context resides in
  99. */
  100. public HttpSession getHttpSession() {
  101. return session;
  102. }
  103. /*
  104. * (non-Javadoc)
  105. *
  106. * @see com.itmill.toolkit.service.ApplicationContext#getApplications()
  107. */
  108. public Collection getApplications() {
  109. LinkedList applications = (LinkedList) session
  110. .getAttribute(ApplicationServlet.SESSION_ATTR_APPS);
  111. return Collections
  112. .unmodifiableCollection(applications == null ? (new LinkedList())
  113. : applications);
  114. }
  115. /**
  116. * Get application context for HttpSession.
  117. *
  118. * @return application context for HttpSession.
  119. */
  120. static public WebApplicationContext getApplicationContext(
  121. HttpSession session) {
  122. return new WebApplicationContext(session);
  123. }
  124. /*
  125. * (non-Javadoc)
  126. *
  127. * @see java.lang.Object#equals(java.lang.Object)
  128. */
  129. public boolean equals(Object obj) {
  130. return session.equals(obj);
  131. }
  132. /*
  133. * (non-Javadoc)
  134. *
  135. * @see java.lang.Object#hashCode()
  136. */
  137. public int hashCode() {
  138. return session.hashCode();
  139. }
  140. /*
  141. * (non-Javadoc)
  142. *
  143. * @see com.itmill.toolkit.service.ApplicationContext#addTransactionListener(com.itmill.toolkit.service.ApplicationContext.TransactionListener)
  144. */
  145. public void addTransactionListener(TransactionListener listener) {
  146. if (this.listeners == null)
  147. this.listeners = new LinkedList();
  148. this.listeners.add(listener);
  149. }
  150. /*
  151. * (non-Javadoc)
  152. *
  153. * @see com.itmill.toolkit.service.ApplicationContext#removeTransactionListener(com.itmill.toolkit.service.ApplicationContext.TransactionListener)
  154. */
  155. public void removeTransactionListener(TransactionListener listener) {
  156. if (this.listeners != null)
  157. this.listeners.remove(listener);
  158. }
  159. /** Notify transaction start */
  160. protected void startTransaction(Application application,
  161. HttpServletRequest request) {
  162. if (this.listeners == null)
  163. return;
  164. for (Iterator i = this.listeners.iterator(); i.hasNext();) {
  165. ((ApplicationContext.TransactionListener) i.next())
  166. .transactionStart(application, request);
  167. }
  168. }
  169. /** Notify transaction end */
  170. protected void endTransaction(Application application,
  171. HttpServletRequest request) {
  172. if (this.listeners == null)
  173. return;
  174. LinkedList exceptions = null;
  175. for (Iterator i = this.listeners.iterator(); i.hasNext();)
  176. try {
  177. ((ApplicationContext.TransactionListener) i.next())
  178. .transactionEnd(application, request);
  179. } catch (RuntimeException t) {
  180. if (exceptions == null)
  181. exceptions = new LinkedList();
  182. exceptions.add(t);
  183. }
  184. // If any runtime exceptions occurred, throw a combined exception
  185. if (exceptions != null) {
  186. StringBuffer msg = new StringBuffer();
  187. for (Iterator i = listeners.iterator(); i.hasNext();) {
  188. RuntimeException e = (RuntimeException) i.next();
  189. if (msg.length() == 0)
  190. msg.append("\n\n--------------------------\n\n");
  191. msg.append(e.getMessage() + "\n");
  192. StringWriter trace = new StringWriter();
  193. e.printStackTrace(new PrintWriter(trace,true));
  194. msg.append(trace.toString());
  195. }
  196. throw new RuntimeException(msg.toString());
  197. }
  198. }
  199. }