001: /*
002: * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a>
003: * @version $Id: PortletInvoker.java 6408 2008-01-25 13:38:39Z docentt $
004: */
005: package org.gridsphere.portletcontainer.impl;
006:
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009: import org.gridsphere.layout.event.PortletWindowEvent;
010: import org.gridsphere.portlet.service.PortletServiceException;
011: import org.gridsphere.portlet.service.spi.PortletServiceFactory;
012: import org.gridsphere.portletcontainer.*;
013: import org.gridsphere.services.core.registry.PortletRegistryService;
014:
015: import javax.servlet.http.HttpServletRequest;
016: import javax.servlet.http.HttpServletResponse;
017: import java.io.IOException;
018:
019: /**
020: * The <code>PortletInvoker</code> provides static lifecycle routines for performing portlet operations on
021: * concrete portlets.
022: *
023: * @see org.gridsphere.portletcontainer.impl.PortletDispatcherImpl
024: */
025: public class PortletInvoker {
026:
027: private Log log = LogFactory.getLog(PortletInvoker.class);
028: private PortletRegistryService registry = null;
029:
030: public PortletInvoker() {
031: try {
032: registry = (PortletRegistryService) PortletServiceFactory
033: .createPortletService(PortletRegistryService.class,
034: true);
035: } catch (PortletServiceException e) {
036: log.error("Unable to init services! ", e);
037: }
038: }
039:
040: /**
041: * Performs service method on a concrete portlet instance
042: *
043: * @param concretePortletID the concrete portlet id
044: * @param render the default portlet render
045: * @param req the <code>HttpServletRequest</code>
046: * @param res the <code>HttpServletResponse</code>
047: * @throws PortletDispatcherException if a dispatching error occurs
048: * @throws IOException if an I/O error occurs
049: */
050: public void service(String concretePortletID,
051: DefaultPortletRender render, HttpServletRequest req,
052: HttpServletResponse res) throws IOException,
053: PortletDispatcherException {
054: log.debug("in service " + concretePortletID);
055: String appID = registry
056: .getApplicationPortletID(concretePortletID);
057: ApplicationPortlet appPortlet = registry
058: .getApplicationPortlet(appID);
059: if (appPortlet != null) {
060: PortletDispatcher dispatcher = appPortlet
061: .getPortletDispatcher(req, res);
062: dispatcher.service(render, req, res);
063: } else {
064: log.info("in service: Unable to find portlet in registry: "
065: + concretePortletID);
066: }
067: }
068:
069: /**
070: * Performs action performed method on a concrete portlet instance
071: *
072: * @param concretePortletID the concrete portlet id
073: * @param action the default portlet action
074: * @param req the <code>HttpServletRequest</code>
075: * @param res the <code>HttpServletResponse</code>
076: * @throws PortletDispatcherException if a dispatching error occurs
077: */
078: public void actionPerformed(String concretePortletID,
079: DefaultPortletAction action, HttpServletRequest req,
080: HttpServletResponse res) throws PortletDispatcherException {
081: log.debug("in actionPerformed " + concretePortletID);
082: String appID = registry
083: .getApplicationPortletID(concretePortletID);
084: ApplicationPortlet appPortlet = registry
085: .getApplicationPortlet(appID);
086: if (appPortlet != null) {
087: PortletDispatcher dispatcher = appPortlet
088: .getPortletDispatcher(req, res);
089: dispatcher.actionPerformed(action, req, res);
090: } else {
091: log
092: .info("in actionPerformed: Unable to find portlet in registry: "
093: + concretePortletID);
094: }
095: }
096:
097: /**
098: * Performs doTitle method on a concrete portlet instance
099: *
100: * @param concretePortletID the concrete portlet id
101: * @param req the <code>HttpServletRequest</code>
102: * @param res the <code>HttpServletResponse</code>
103: * @throws PortletDispatcherException if a dispatching error occurs
104: */
105: public void doTitle(String concretePortletID,
106: HttpServletRequest req, HttpServletResponse res)
107: throws PortletDispatcherException {
108: log.debug("in doTitle " + concretePortletID);
109: String appID = registry
110: .getApplicationPortletID(concretePortletID);
111: ApplicationPortlet appPortlet = registry
112: .getApplicationPortlet(appID);
113: if (appPortlet != null) {
114: PortletDispatcher dispatcher = appPortlet
115: .getPortletDispatcher(req, res);
116: dispatcher.doTitle(req, res);
117: } else {
118: log
119: .info("in actionPerformed: Unable to find portlet in registry: "
120: + concretePortletID);
121: }
122: }
123:
124: /**
125: * Performs window event method on a concrete portlet instance
126: *
127: * @param concretePortletID the concrete portlet id
128: * @param winEvent the window event
129: * @param req the <code>HttpServletRequest</code>
130: * @param res the <code>HttpServletResponse</code>
131: * @throws PortletDispatcherException if a dispatching error occurs
132: */
133: public void windowEvent(String concretePortletID,
134: PortletWindowEvent winEvent, HttpServletRequest req,
135: HttpServletResponse res) throws PortletDispatcherException {
136: log.debug("in windowEvent " + concretePortletID);
137: String appID = registry
138: .getApplicationPortletID(concretePortletID);
139: ApplicationPortlet appPortlet = registry
140: .getApplicationPortlet(appID);
141: if (appPortlet != null) {
142: PortletDispatcher dispatcher = appPortlet
143: .getPortletDispatcher(req, res);
144: dispatcher.windowEvent(winEvent, req, res);
145: } else {
146: log
147: .info("in windowEvent: Unable to find portlet in registry: "
148: + concretePortletID);
149: }
150: }
151:
152: public void initPortletWebApp(
153: PortletWebApplicationLoader appLoader,
154: HttpServletRequest req, HttpServletResponse res)
155: throws PortletDispatcherException {
156: // Initialize all concrete portlets for each application portlet
157: PortletDispatcher portletDispatcher = appLoader
158: .getPortletDispatcher();
159: // initialize the application portlet
160: log.debug("initializing portlet web app "
161: + appLoader.getWebApplicationName());
162: portletDispatcher.init(req, res);
163:
164: }
165:
166: public void loadPortletWebApp(
167: PortletWebApplicationLoader appLoader,
168: HttpServletRequest req, HttpServletResponse res)
169: throws PortletDispatcherException {
170: // Initialize all concrete portlets for each application portlet
171: PortletDispatcher portletDispatcher = appLoader
172: .getPortletDispatcher();
173: // initialize the application portlet
174: log.debug("loading portlet web app "
175: + appLoader.getWebApplicationName());
176: portletDispatcher.load(req, res);
177:
178: }
179:
180: /**
181: * Lohgout a portlet web application
182: *
183: * @param appLoader the web application loader
184: * @param req the <code>HttpServletRequest</code>
185: * @param res the <code>HttpServletResponse</code>
186: * @throws PortletDispatcherException if a dispatching error occurs
187: */
188: public void logoutPortletWebApp(
189: PortletWebApplicationLoader appLoader,
190: HttpServletRequest req, HttpServletResponse res)
191: throws PortletDispatcherException {
192: PortletDispatcher portletDispatcher = appLoader
193: .getPortletDispatcher();
194: portletDispatcher.logout(req, res);
195: }
196:
197: /**
198: * Shuts down a portlet web application
199: *
200: * @param webAppLoader the name of the JSR portlet web application loader
201: * @param req the <code>HttpServletRequest</code>
202: * @param res the <code>HttpServletResponse</code>
203: * @throws PortletDispatcherException if a dispatching error occurs
204: */
205: public void destroyPortletWebApp(
206: PortletWebApplicationLoader webAppLoader,
207: HttpServletRequest req, HttpServletResponse res)
208: throws PortletDispatcherException {
209: // First destroy all concrete portlets for each application portlet
210: log.debug("in destroyPortletWebApp "
211: + webAppLoader.getWebApplicationName());
212: // destroy the application portlet
213: webAppLoader.getPortletDispatcher().destroy(req, res);
214: }
215:
216: /**
217: * Returns the application portlet id from the supplied concrete portlet id
218: *
219: * @param concretePortletID the concrete portlet id
220: * @return the application portlet id
221: */
222: protected static String getApplicationPortletID(
223: String concretePortletID) {
224: int i = concretePortletID.lastIndexOf(".");
225: if (i < 0)
226: return "";
227: return concretePortletID.substring(0, i);
228: }
229:
230: }
|