001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet;
022:
023: import com.liferay.portal.kernel.language.LanguageUtil;
024: import com.liferay.portal.kernel.servlet.PortletServlet;
025: import com.liferay.portal.kernel.servlet.StringServletResponse;
026: import com.liferay.portal.kernel.util.ClassUtil;
027: import com.liferay.portal.kernel.util.GetterUtil;
028: import com.liferay.portal.kernel.util.JavaConstants;
029: import com.liferay.portal.kernel.util.StringMaker;
030: import com.liferay.portal.kernel.util.StringPool;
031: import com.liferay.portal.model.Layout;
032: import com.liferay.portal.tools.PortletDeployer;
033: import com.liferay.portal.util.WebKeys;
034: import com.liferay.util.CollectionFactory;
035: import com.liferay.util.Time;
036:
037: import java.io.IOException;
038:
039: import java.util.Map;
040:
041: import javax.portlet.ActionRequest;
042: import javax.portlet.ActionResponse;
043: import javax.portlet.Portlet;
044: import javax.portlet.PortletConfig;
045: import javax.portlet.PortletContext;
046: import javax.portlet.PortletException;
047: import javax.portlet.PortletRequest;
048: import javax.portlet.PortletResponse;
049: import javax.portlet.PortletSession;
050: import javax.portlet.RenderRequest;
051: import javax.portlet.RenderResponse;
052:
053: import javax.servlet.RequestDispatcher;
054: import javax.servlet.ServletException;
055: import javax.servlet.http.HttpServletRequest;
056: import javax.servlet.http.HttpServletResponse;
057: import javax.servlet.http.HttpSession;
058:
059: import org.apache.commons.lang.time.StopWatch;
060: import org.apache.commons.logging.Log;
061: import org.apache.commons.logging.LogFactory;
062:
063: /**
064: * <a href="CachePortlet.java.html"><b><i>View Source</i></b></a>
065: *
066: * @author Brian Wing Shun Chan
067: * @author Brian Myunghun Kim
068: *
069: */
070: public class CachePortlet implements Portlet {
071:
072: public static void clearResponse(HttpSession ses, long plid,
073: String portletId, String languageId) {
074:
075: String sesResponseId = encodeResponseKey(plid, portletId,
076: languageId);
077:
078: getResponses(ses).remove(sesResponseId);
079: }
080:
081: public static void clearResponses(HttpSession ses) {
082: getResponses(ses).clear();
083: }
084:
085: public static void clearResponses(PortletSession ses) {
086: getResponses(ses).clear();
087: }
088:
089: public static String encodeResponseKey(long plid, String portletId,
090: String languageId) {
091:
092: StringMaker sm = new StringMaker();
093:
094: sm.append(plid);
095: sm.append(StringPool.UNDERLINE);
096: sm.append(portletId);
097: sm.append(StringPool.UNDERLINE);
098: sm.append(languageId);
099:
100: return sm.toString();
101: }
102:
103: public static Map getResponses(HttpSession ses) {
104: Map responses = (Map) ses
105: .getAttribute(WebKeys.CACHE_PORTLET_RESPONSES);
106:
107: if (responses == null) {
108: responses = CollectionFactory.getHashMap();
109:
110: ses
111: .setAttribute(WebKeys.CACHE_PORTLET_RESPONSES,
112: responses);
113: }
114:
115: return responses;
116: }
117:
118: public static Map getResponses(PortletSession ses) {
119: return getResponses(((PortletSessionImpl) ses).getHttpSession());
120: }
121:
122: public CachePortlet(Portlet portlet, PortletContext portletCtx,
123: Integer expCache) {
124:
125: _portlet = portlet;
126: _portletCtx = (PortletContextImpl) portletCtx;
127: _expCache = expCache;
128:
129: if (_log.isDebugEnabled()) {
130: _log.debug("Create root cache wrapper for "
131: + _portletCtx.getPortlet().getPortletId());
132: }
133:
134: if (ClassUtil.isSubclass(_portlet.getClass(),
135: PortletDeployer.JSF_MYFACES)
136: || ClassUtil.isSubclass(_portlet.getClass(),
137: PortletDeployer.JSF_SUN)) {
138:
139: _facesPortlet = true;
140: }
141:
142: _strutsPortlet = ClassUtil.isSubclass(portlet.getClass(),
143: StrutsPortlet.class);
144: _strutsBridgePortlet = ClassUtil.isSubclass(portlet.getClass(),
145: "org.apache.portals.bridges.struts.StrutsPortlet");
146: }
147:
148: public CachePortlet(Portlet portlet, PortletConfig portletConfig,
149: PortletContext portletCtx, Integer expCache,
150: boolean facesPortlet, boolean strutsPortlet,
151: boolean strutsBridgePortlet) {
152:
153: // From constructor
154:
155: _portlet = portlet;
156: _portletCtx = (PortletContextImpl) portletCtx;
157: _expCache = expCache;
158: _facesPortlet = facesPortlet;
159: _strutsPortlet = strutsPortlet;
160: _strutsBridgePortlet = strutsBridgePortlet;
161:
162: if (_log.isDebugEnabled()) {
163: _log.debug("Create instance cache wrapper for "
164: + _portletCtx.getPortlet().getPortletId());
165: }
166:
167: // From init
168:
169: _portletConfig = (PortletConfigImpl) portletConfig;
170:
171: _portletId = _portletConfig.getPortletId();
172: }
173:
174: public void init(PortletConfig portletConfig)
175: throws PortletException {
176: _portletConfig = (PortletConfigImpl) portletConfig;
177:
178: _portletId = _portletConfig.getPortletId();
179:
180: ClassLoader contextClassLoader = Thread.currentThread()
181: .getContextClassLoader();
182:
183: ClassLoader portletClassLoader = _getPortletClassLoader();
184:
185: try {
186: if (portletClassLoader != null) {
187: Thread.currentThread().setContextClassLoader(
188: portletClassLoader);
189: }
190:
191: _portlet.init(portletConfig);
192: } finally {
193: if (portletClassLoader != null) {
194: Thread.currentThread().setContextClassLoader(
195: contextClassLoader);
196: }
197: }
198:
199: _destroyable = true;
200: }
201:
202: public void processAction(ActionRequest req, ActionResponse res)
203: throws IOException, PortletException {
204:
205: StopWatch stopWatch = null;
206:
207: if (_log.isDebugEnabled()) {
208: stopWatch = new StopWatch();
209:
210: stopWatch.start();
211: }
212:
213: try {
214: _invoke(req, res, true);
215: } catch (PortletException pe) {
216: req.setAttribute(_portletId
217: + PortletException.class.getName(), pe);
218: }
219:
220: if (_log.isDebugEnabled()) {
221: _log.debug("processAction for " + _portletId + " takes "
222: + stopWatch.getTime() + " ms");
223: }
224: }
225:
226: public void render(RenderRequest req, RenderResponse res)
227: throws IOException, PortletException {
228:
229: PortletException portletException = (PortletException) req
230: .getAttribute(_portletId
231: + PortletException.class.getName());
232:
233: if (portletException != null) {
234: throw portletException;
235: }
236:
237: StopWatch stopWatch = null;
238:
239: if (_log.isDebugEnabled()) {
240: stopWatch = new StopWatch();
241:
242: stopWatch.start();
243: }
244:
245: String remoteUser = req.getRemoteUser();
246:
247: if ((remoteUser == null) || (_expCache == null)
248: || (_expCache.intValue() == 0)) {
249:
250: _invoke(req, res, false);
251: } else {
252: RenderResponseImpl resImpl = (RenderResponseImpl) res;
253:
254: StringServletResponse stringServletRes = (StringServletResponse) resImpl
255: .getHttpServletResponse();
256:
257: PortletSession ses = req.getPortletSession();
258:
259: long now = System.currentTimeMillis();
260:
261: Layout layout = (Layout) req.getAttribute(WebKeys.LAYOUT);
262:
263: Map sesResponses = getResponses(ses);
264:
265: String sesResponseId = encodeResponseKey(layout.getPlid(),
266: _portletId, LanguageUtil.getLanguageId(req));
267:
268: CachePortletResponse response = (CachePortletResponse) sesResponses
269: .get(sesResponseId);
270:
271: if (response == null) {
272: _invoke(req, res, false);
273:
274: response = new CachePortletResponse(resImpl.getTitle(),
275: stringServletRes.getString(), now + Time.SECOND
276: * _expCache.intValue());
277:
278: sesResponses.put(sesResponseId, response);
279: } else if ((response.getTime() < now)
280: && (_expCache.intValue() > 0)) {
281:
282: _invoke(req, res, false);
283:
284: response.setTitle(resImpl.getTitle());
285: response.setContent(stringServletRes.getString());
286: response.setTime(now + Time.SECOND
287: * _expCache.intValue());
288: } else {
289: resImpl.setTitle(response.getTitle());
290: stringServletRes.getWriter().print(
291: response.getContent());
292: }
293: }
294:
295: if (_log.isDebugEnabled()) {
296: _log.debug("render for " + _portletId + " takes "
297: + stopWatch.getTime() + " ms");
298: }
299: }
300:
301: public void destroy() {
302: if (_destroyable) {
303: ClassLoader contextClassLoader = Thread.currentThread()
304: .getContextClassLoader();
305:
306: ClassLoader portletClassLoader = _getPortletClassLoader();
307:
308: try {
309: if (portletClassLoader != null) {
310: Thread.currentThread().setContextClassLoader(
311: portletClassLoader);
312: }
313:
314: _portlet.destroy();
315: } finally {
316: if (portletClassLoader != null) {
317: Thread.currentThread().setContextClassLoader(
318: contextClassLoader);
319: }
320: }
321: }
322:
323: _destroyable = false;
324: }
325:
326: public Portlet getPortletInstance() {
327: return _portlet;
328: }
329:
330: public PortletConfigImpl getPortletConfig() {
331: return _portletConfig;
332: }
333:
334: public PortletContextImpl getPortletContext() {
335: return _portletCtx;
336: }
337:
338: public Integer getExpCache() {
339: return _expCache;
340: }
341:
342: public boolean isDestroyable() {
343: return _destroyable;
344: }
345:
346: public boolean isFacesPortlet() {
347: return _facesPortlet;
348: }
349:
350: public boolean isStrutsPortlet() {
351: return _strutsPortlet;
352: }
353:
354: public boolean isStrutsBridgePortlet() {
355: return _strutsBridgePortlet;
356: }
357:
358: private ClassLoader _getPortletClassLoader() {
359: return (ClassLoader) _portletCtx
360: .getAttribute(PortletServlet.PORTLET_CLASS_LOADER);
361: }
362:
363: private void _invoke(PortletRequest req, PortletResponse res,
364: boolean action) throws IOException, PortletException {
365:
366: Map properties = null;
367:
368: if (_portletConfig.isWARFile()) {
369: String path = StringPool.SLASH
370: + _portletConfig.getPortletName() + "/invoke";
371:
372: RequestDispatcher rd = _portletCtx.getServletContext()
373: .getRequestDispatcher(path);
374:
375: HttpServletRequest httpReq = null;
376: HttpServletResponse httpRes = null;
377:
378: ActionRequestImpl actionReqImpl = null;
379: ActionResponseImpl actionResImpl = null;
380:
381: RenderRequestImpl renderReqImpl = null;
382: RenderResponseImpl renderResImpl = null;
383:
384: if (action) {
385: actionReqImpl = (ActionRequestImpl) req;
386: actionResImpl = (ActionResponseImpl) res;
387:
388: httpReq = actionReqImpl.getHttpServletRequest();
389: httpRes = actionResImpl.getHttpServletResponse();
390: } else {
391: renderReqImpl = (RenderRequestImpl) req;
392: renderResImpl = (RenderResponseImpl) res;
393:
394: httpReq = renderReqImpl.getHttpServletRequest();
395: httpRes = renderResImpl.getHttpServletResponse();
396: }
397:
398: httpReq.setAttribute(JavaConstants.JAVAX_PORTLET_PORTLET,
399: _portlet);
400:
401: try {
402: rd.include(httpReq, httpRes);
403: } catch (ServletException se) {
404: Throwable cause = se.getRootCause();
405:
406: if (cause instanceof PortletException) {
407: throw (PortletException) cause;
408: }
409:
410: throw new PortletException(cause);
411: }
412:
413: if (action) {
414: properties = actionResImpl.getProperties();
415: } else {
416: properties = renderResImpl.getProperties();
417: }
418: } else {
419: if (action) {
420: ActionRequestImpl actionReqImpl = (ActionRequestImpl) req;
421: ActionResponseImpl actionResImpl = (ActionResponseImpl) res;
422:
423: _portlet.processAction(actionReqImpl, actionResImpl);
424:
425: properties = actionResImpl.getProperties();
426: } else {
427: RenderRequestImpl renderReqImpl = (RenderRequestImpl) req;
428: RenderResponseImpl renderResImpl = (RenderResponseImpl) res;
429:
430: _portlet.render(renderReqImpl, renderResImpl);
431:
432: properties = renderResImpl.getProperties();
433: }
434: }
435:
436: if ((properties != null) && (properties.size() > 0)) {
437: if (_expCache != null) {
438: String[] expCache = (String[]) properties
439: .get(RenderResponse.EXPIRATION_CACHE);
440:
441: if ((expCache != null) && (expCache.length > 0)
442: && (expCache[0] != null)) {
443:
444: _expCache = new Integer(GetterUtil
445: .getInteger(expCache[0]));
446: }
447: }
448: }
449: }
450:
451: private static Log _log = LogFactory.getLog(CachePortlet.class);
452:
453: private String _portletId;
454: private Portlet _portlet;
455: private PortletConfigImpl _portletConfig;
456: private PortletContextImpl _portletCtx;
457: private Integer _expCache;
458: private boolean _destroyable;
459: private boolean _facesPortlet;
460: private boolean _strutsPortlet;
461: private boolean _strutsBridgePortlet;
462:
463: }
|