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.

PortletApplicationContext.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. /**
  5. *
  6. */
  7. package com.vaadin.terminal.gwt.server;
  8. import java.io.Serializable;
  9. import java.util.HashMap;
  10. import java.util.Iterator;
  11. import java.util.LinkedHashSet;
  12. import java.util.Map;
  13. import java.util.Set;
  14. import javax.portlet.ActionRequest;
  15. import javax.portlet.ActionResponse;
  16. import javax.portlet.Portlet;
  17. import javax.portlet.PortletSession;
  18. import javax.portlet.RenderRequest;
  19. import javax.portlet.RenderResponse;
  20. import javax.servlet.http.HttpSession;
  21. import com.vaadin.Application;
  22. /**
  23. * @author marc
  24. */
  25. @SuppressWarnings({ "serial", "unchecked" })
  26. public class PortletApplicationContext extends WebApplicationContext implements
  27. Serializable {
  28. protected PortletSession portletSession;
  29. protected Map<Application, Set<PortletListener>> portletListeners = new HashMap<Application, Set<PortletListener>>();
  30. protected Map<Portlet, Application> portletToApplication = new HashMap<Portlet, Application>();
  31. PortletApplicationContext() {
  32. }
  33. static public PortletApplicationContext getApplicationContext(
  34. PortletSession session) {
  35. WebApplicationContext cx = (WebApplicationContext) session
  36. .getAttribute(WebApplicationContext.class.getName(),
  37. PortletSession.APPLICATION_SCOPE);
  38. if (cx == null) {
  39. cx = new PortletApplicationContext();
  40. }
  41. if (!(cx instanceof PortletApplicationContext)) {
  42. // TODO Should we even try this? And should we leave original as-is?
  43. PortletApplicationContext pcx = new PortletApplicationContext();
  44. pcx.applications.addAll(cx.applications);
  45. cx.applications.clear();
  46. pcx.browser = cx.browser;
  47. cx.browser = null;
  48. pcx.listeners = cx.listeners;
  49. cx.listeners = null;
  50. pcx.session = cx.session;
  51. cx = pcx;
  52. }
  53. if (((PortletApplicationContext) cx).portletSession == null) {
  54. ((PortletApplicationContext) cx).portletSession = session;
  55. }
  56. session.setAttribute(WebApplicationContext.class.getName(), cx,
  57. PortletSession.APPLICATION_SCOPE);
  58. return (PortletApplicationContext) cx;
  59. }
  60. static public WebApplicationContext getApplicationContext(
  61. HttpSession session) {
  62. WebApplicationContext cx = (WebApplicationContext) session
  63. .getAttribute(WebApplicationContext.class.getName());
  64. if (cx == null) {
  65. cx = new PortletApplicationContext();
  66. }
  67. if (cx.session == null) {
  68. cx.session = session;
  69. }
  70. session.setAttribute(WebApplicationContext.class.getName(), cx);
  71. return cx;
  72. }
  73. @Override
  74. protected void removeApplication(Application application) {
  75. portletListeners.remove(application);
  76. for (Iterator<Application> it = portletToApplication.values()
  77. .iterator(); it.hasNext();) {
  78. Application value = it.next();
  79. if (value == application) {
  80. // values().iterator() is backed by the map
  81. it.remove();
  82. }
  83. }
  84. super.removeApplication(application);
  85. }
  86. public void setPortletApplication(Portlet portlet, Application app) {
  87. portletToApplication.put(portlet, app);
  88. }
  89. public Application getPortletApplication(Portlet portlet) {
  90. return portletToApplication.get(portlet);
  91. }
  92. public PortletSession getPortletSession() {
  93. return portletSession;
  94. }
  95. public void addPortletListener(Application app, PortletListener listener) {
  96. Set<PortletListener> l = portletListeners.get(app);
  97. if (l == null) {
  98. l = new LinkedHashSet<PortletListener>();
  99. portletListeners.put(app, l);
  100. }
  101. l.add(listener);
  102. }
  103. public void removePortletListener(Application app, PortletListener listener) {
  104. Set<PortletListener> l = portletListeners.get(app);
  105. if (l != null) {
  106. l.remove(listener);
  107. }
  108. }
  109. public static void dispatchRequest(Portlet portlet, RenderRequest request,
  110. RenderResponse response) {
  111. PortletApplicationContext ctx = getApplicationContext(request
  112. .getPortletSession());
  113. if (ctx != null) {
  114. ctx.firePortletRenderRequest(portlet, request, response);
  115. }
  116. }
  117. public static void dispatchRequest(Portlet portlet, ActionRequest request,
  118. ActionResponse response) {
  119. PortletApplicationContext ctx = getApplicationContext(request
  120. .getPortletSession());
  121. if (ctx != null) {
  122. ctx.firePortletActionRequest(portlet, request, response);
  123. }
  124. }
  125. public void firePortletRenderRequest(Portlet portlet,
  126. RenderRequest request, RenderResponse response) {
  127. Application app = getPortletApplication(portlet);
  128. Set<PortletListener> listeners = portletListeners.get(app);
  129. if (listeners != null) {
  130. for (PortletListener l : listeners) {
  131. l.handleRenderRequest(request, new RestrictedRenderResponse(
  132. response));
  133. }
  134. }
  135. }
  136. public void firePortletActionRequest(Portlet portlet,
  137. ActionRequest request, ActionResponse response) {
  138. Application app = getPortletApplication(portlet);
  139. Set<PortletListener> listeners = portletListeners.get(app);
  140. if (listeners != null) {
  141. for (PortletListener l : listeners) {
  142. l.handleActionRequest(request, response);
  143. }
  144. }
  145. }
  146. public interface PortletListener extends Serializable {
  147. public void handleRenderRequest(RenderRequest request,
  148. RenderResponse response);
  149. public void handleActionRequest(ActionRequest request,
  150. ActionResponse response);
  151. }
  152. }