001: /**
002: * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a>
003: * @version $Id: PortletDispatcherImpl.java 6408 2008-01-25 13:38:39Z docentt $
004: */package org.gridsphere.portletcontainer.impl;
005:
006: import org.apache.commons.logging.Log;
007: import org.apache.commons.logging.LogFactory;
008: import org.gridsphere.layout.event.PortletWindowEvent;
009: import org.gridsphere.portlet.impl.SportletProperties;
010: import org.gridsphere.portletcontainer.DefaultPortletAction;
011: import org.gridsphere.portletcontainer.DefaultPortletRender;
012: import org.gridsphere.portletcontainer.PortletDispatcher;
013: import org.gridsphere.portletcontainer.PortletDispatcherException;
014:
015: import javax.servlet.RequestDispatcher;
016: import javax.servlet.ServletException;
017: import javax.servlet.http.HttpServletRequest;
018: import javax.servlet.http.HttpServletResponse;
019: import java.io.IOException;
020:
021: /**
022: * The <code>PortletDispatcher</code> provides a mechanism for invoking portlet lifecycle methods by using a
023: * <code>RequestDispatcher</code> from the Servlet API to forward requests to other portlets (servlets).
024: *
025: * @see <code>PortletInvoker</code>
026: */
027: public class PortletDispatcherImpl implements PortletDispatcher {
028:
029: public static Log log = LogFactory
030: .getLog(PortletDispatcherImpl.class);
031:
032: private RequestDispatcher rd;
033:
034: /**
035: * Default constructor is kept private
036: */
037: private PortletDispatcherImpl() {
038: }
039:
040: /**
041: * Constructs a PortletDispatcher from a <code>RequestDispatcher</code> and an <code>ApplicationPortletConfig</code>
042: *
043: * @param rd the <code>RequestDispatcher</code>
044: */
045: public PortletDispatcherImpl(RequestDispatcher rd) {
046: this .rd = rd;
047: }
048:
049: public void init(HttpServletRequest req, HttpServletResponse res)
050: throws PortletDispatcherException {
051: req.setAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD,
052: SportletProperties.INIT);
053: try {
054: include(req, res);
055: } catch (ServletException e) {
056: throw new PortletDispatcherException(
057: "Unable to initialize servlet", e.getRootCause());
058: } catch (Exception e) {
059: throw new PortletDispatcherException(
060: "Unable to initialize portlet: ", e);
061: } finally {
062: req
063: .removeAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD);
064: }
065: }
066:
067: public void load(HttpServletRequest req, HttpServletResponse res)
068: throws PortletDispatcherException {
069: req.setAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD,
070: SportletProperties.LOAD);
071: try {
072: include(req, res);
073: } catch (ServletException e) {
074: throw new PortletDispatcherException(
075: "Unable to initialize servlet", e.getRootCause());
076: } catch (Exception e) {
077: throw new PortletDispatcherException(
078: "Unable to load portlet: ", e);
079: } finally {
080: req
081: .removeAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD);
082: }
083: }
084:
085: public void destroy(HttpServletRequest req, HttpServletResponse res)
086: throws PortletDispatcherException {
087: req.setAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD,
088: SportletProperties.DESTROY);
089: try {
090: include(req, res);
091: } catch (ServletException e) {
092: throw new PortletDispatcherException(
093: "Unable to perform destroy", e.getRootCause());
094: } catch (Exception e) {
095: throw new PortletDispatcherException(
096: "Unable to perform destroy: ", e);
097: } finally {
098: req
099: .removeAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD);
100: }
101: }
102:
103: public void service(DefaultPortletRender render,
104: HttpServletRequest req, HttpServletResponse res)
105: throws PortletDispatcherException {
106: req.setAttribute(SportletProperties.RENDER_EVENT, render);
107: req.setAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD,
108: SportletProperties.SERVICE);
109: try {
110: include(req, res);
111: } catch (ServletException e) {
112: throw new PortletDispatcherException(
113: "Unable to perform service", e.getRootCause());
114: } catch (Exception e) {
115: throw new PortletDispatcherException(
116: "Unable to perform service", e);
117: } finally {
118: req
119: .removeAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD);
120: }
121: }
122:
123: public void login(HttpServletRequest req, HttpServletResponse res)
124: throws PortletDispatcherException {
125: req.setAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD,
126: SportletProperties.LOGIN);
127: try {
128: include(req, res);
129: } catch (ServletException e) {
130: throw new PortletDispatcherException(
131: "Unable to perform login", e.getRootCause());
132: } catch (Exception e) {
133: throw new PortletDispatcherException(
134: "Unable to perform login", e);
135: } finally {
136: req
137: .removeAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD);
138: }
139: }
140:
141: public void logout(HttpServletRequest req, HttpServletResponse res)
142: throws PortletDispatcherException {
143: req.setAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD,
144: SportletProperties.LOGOUT);
145: try {
146: include(req, res);
147: } catch (ServletException e) {
148: throw new PortletDispatcherException(
149: "Unable to perform logout", e.getRootCause());
150: } catch (Exception e) {
151: throw new PortletDispatcherException(
152: "Unable to perform logout", e);
153: } finally {
154: req
155: .removeAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD);
156: }
157: }
158:
159: public void actionPerformed(DefaultPortletAction action,
160: HttpServletRequest req, HttpServletResponse res)
161: throws PortletDispatcherException {
162: req.setAttribute(SportletProperties.ACTION_EVENT, action);
163: req.setAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD,
164: SportletProperties.SERVICE);
165: req.setAttribute(SportletProperties.PORTLET_ACTION_METHOD,
166: SportletProperties.ACTION_PERFORMED);
167: try {
168: include(req, res);
169: } catch (ServletException e) {
170: throw new PortletDispatcherException(
171: "Unable to perform actionPerformed", e
172: .getRootCause());
173: } catch (Exception e) {
174: throw new PortletDispatcherException(
175: "Unable to perform actionPerformed", e);
176: } finally {
177: req.removeAttribute(SportletProperties.ACTION_EVENT);
178: req
179: .removeAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD);
180: req
181: .removeAttribute(SportletProperties.PORTLET_ACTION_METHOD);
182: }
183: }
184:
185: public void doTitle(HttpServletRequest req, HttpServletResponse res)
186: throws PortletDispatcherException {
187: req.setAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD,
188: SportletProperties.SERVICE);
189: req.setAttribute(SportletProperties.PORTLET_ACTION_METHOD,
190: SportletProperties.DO_TITLE);
191: try {
192: include(req, res);
193: } catch (ServletException e) {
194: throw new PortletDispatcherException(
195: "Unable to perform doTitle", e.getRootCause());
196: } catch (Exception e) {
197: throw new PortletDispatcherException(
198: "Unable to perform doTitle", e);
199: } finally {
200: req
201: .removeAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD);
202: req
203: .removeAttribute(SportletProperties.PORTLET_ACTION_METHOD);
204: }
205: }
206:
207: public void windowEvent(PortletWindowEvent event,
208: HttpServletRequest req, HttpServletResponse res)
209: throws PortletDispatcherException {
210: req.setAttribute(SportletProperties.WINDOW_EVENT, event);
211: req.setAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD,
212: SportletProperties.SERVICE);
213: req.setAttribute(SportletProperties.PORTLET_ACTION_METHOD,
214: SportletProperties.WINDOW_EVENT);
215: try {
216: include(req, res);
217: } catch (ServletException e) {
218: throw new PortletDispatcherException(
219: "Unable to perform windowEvent", e.getRootCause());
220: } catch (Exception e) {
221: throw new PortletDispatcherException(
222: "Unable to perform windowEvent", e);
223: } finally {
224: req.removeAttribute(SportletProperties.WINDOW_EVENT);
225: req
226: .removeAttribute(SportletProperties.PORTLET_LIFECYCLE_METHOD);
227: req
228: .removeAttribute(SportletProperties.PORTLET_ACTION_METHOD);
229: }
230: }
231:
232: /**
233: * Underlying method used by all other dispatcher methods to invoke the appropriate portlet lifecycle method
234: *
235: * @param req a <code>HttpServletRequest</code>
236: * @param res a <code>HttpServletResponse</code>
237: * @throws IOException if an I/O error occurs
238: * @throws ServletException if a servlet exception occurs
239: */
240: protected synchronized void include(HttpServletRequest req,
241: HttpServletResponse res) throws IOException,
242: ServletException {
243: rd.include(req, res);
244: }
245:
246: }
|