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.

AbstractWebApplicationContext.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.server;
  5. import java.io.PrintWriter;
  6. import java.io.Serializable;
  7. import java.io.StringWriter;
  8. import java.net.URL;
  9. import java.util.Collection;
  10. import java.util.Collections;
  11. import java.util.HashMap;
  12. import java.util.HashSet;
  13. import java.util.LinkedList;
  14. import javax.servlet.http.HttpSessionBindingEvent;
  15. import javax.servlet.http.HttpSessionBindingListener;
  16. import com.vaadin.Application;
  17. import com.vaadin.service.ApplicationContext;
  18. import com.vaadin.terminal.ApplicationResource;
  19. /**
  20. * Base class for web application contexts (including portlet contexts) that
  21. * handles the common tasks.
  22. */
  23. public abstract class AbstractWebApplicationContext implements
  24. ApplicationContext, HttpSessionBindingListener, Serializable {
  25. protected Collection<TransactionListener> listeners = Collections
  26. .synchronizedList(new LinkedList<TransactionListener>());
  27. protected final HashSet<Application> applications = new HashSet<Application>();
  28. protected WebBrowser browser = new WebBrowser();
  29. protected HashMap<Application, AbstractCommunicationManager> applicationToAjaxAppMgrMap = new HashMap<Application, AbstractCommunicationManager>();
  30. public void addTransactionListener(TransactionListener listener) {
  31. if (listener != null) {
  32. listeners.add(listener);
  33. }
  34. }
  35. public void removeTransactionListener(TransactionListener listener) {
  36. listeners.remove(listener);
  37. }
  38. /**
  39. * Sends a notification that a transaction is starting.
  40. *
  41. * @param application
  42. * The application associated with the transaction.
  43. * @param request
  44. * the HTTP or portlet request that triggered the transaction.
  45. */
  46. protected void startTransaction(Application application, Object request) {
  47. synchronized (listeners) {
  48. for (TransactionListener listener : listeners) {
  49. listener.transactionStart(application, request);
  50. }
  51. }
  52. }
  53. /**
  54. * Sends a notification that a transaction has ended.
  55. *
  56. * @param application
  57. * The application associated with the transaction.
  58. * @param request
  59. * the HTTP or portlet request that triggered the transaction.
  60. */
  61. protected void endTransaction(Application application, Object request) {
  62. LinkedList<Exception> exceptions = null;
  63. synchronized (listeners) {
  64. for (TransactionListener listener : listeners) {
  65. try {
  66. listener.transactionEnd(application, request);
  67. } catch (final RuntimeException t) {
  68. if (exceptions == null) {
  69. exceptions = new LinkedList<Exception>();
  70. }
  71. exceptions.add(t);
  72. }
  73. }
  74. }
  75. // If any runtime exceptions occurred, throw a combined exception
  76. if (exceptions != null) {
  77. final StringBuffer msg = new StringBuffer();
  78. for (Exception e : exceptions) {
  79. if (msg.length() == 0) {
  80. msg.append("\n\n--------------------------\n\n");
  81. }
  82. msg.append(e.getMessage() + "\n");
  83. final StringWriter trace = new StringWriter();
  84. e.printStackTrace(new PrintWriter(trace, true));
  85. msg.append(trace.toString());
  86. }
  87. throw new RuntimeException(msg.toString());
  88. }
  89. }
  90. /**
  91. * @see javax.servlet.http.HttpSessionBindingListener#valueBound(HttpSessionBindingEvent)
  92. */
  93. public void valueBound(HttpSessionBindingEvent arg0) {
  94. // We are not interested in bindings
  95. }
  96. /**
  97. * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent)
  98. */
  99. public void valueUnbound(HttpSessionBindingEvent event) {
  100. // If we are going to be unbound from the session, the session must be
  101. // closing
  102. try {
  103. while (!applications.isEmpty()) {
  104. final Application app = applications.iterator().next();
  105. app.close();
  106. removeApplication(app);
  107. }
  108. } catch (Exception e) {
  109. // This should never happen but is possible with rare
  110. // configurations (e.g. robustness tests). If you have one
  111. // thread doing HTTP socket write and another thread trying to
  112. // remove same application here. Possible if you got e.g. session
  113. // lifetime 1 min but socket write may take longer than 1 min.
  114. // FIXME: Handle exception
  115. System.err.println("Could not remove application, leaking memory.");
  116. e.printStackTrace();
  117. }
  118. }
  119. /**
  120. * Get the web browser associated with this application context.
  121. *
  122. * Because application context is related to the http session and server
  123. * maintains one session per browser-instance, each context has exactly one
  124. * web browser associated with it.
  125. *
  126. * @return
  127. */
  128. public WebBrowser getBrowser() {
  129. return browser;
  130. }
  131. public Collection<Application> getApplications() {
  132. return Collections.unmodifiableCollection(applications);
  133. }
  134. protected void removeApplication(Application application) {
  135. applications.remove(application);
  136. applicationToAjaxAppMgrMap.remove(application);
  137. }
  138. public String generateApplicationResourceURL(ApplicationResource resource,
  139. String mapKey) {
  140. final String filename = resource.getFilename();
  141. if (filename == null) {
  142. return "APP/" + mapKey + "/";
  143. } else {
  144. return "APP/" + mapKey + "/" + filename;
  145. }
  146. }
  147. public boolean isApplicationResourceURL(URL context, String relativeUri) {
  148. // If the relative uri is null, we are ready
  149. if (relativeUri == null) {
  150. return false;
  151. }
  152. // Resolves the prefix
  153. String prefix = relativeUri;
  154. final int index = relativeUri.indexOf('/');
  155. if (index >= 0) {
  156. prefix = relativeUri.substring(0, index);
  157. }
  158. // Handles the resource requests
  159. return (prefix.equals("APP"));
  160. }
  161. public String getURLKey(URL context, String relativeUri) {
  162. final int index = relativeUri.indexOf('/');
  163. final int next = relativeUri.indexOf('/', index + 1);
  164. if (next < 0) {
  165. return null;
  166. }
  167. return relativeUri.substring(index + 1, next);
  168. }
  169. }