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.model.Portlet;
024: import com.liferay.portal.util.PropsValues;
025:
026: import javax.portlet.PortletContext;
027: import javax.portlet.PortletMode;
028: import javax.portlet.PortletPreferences;
029: import javax.portlet.WindowState;
030:
031: import javax.servlet.http.HttpServletRequest;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.apache.commons.pool.BasePoolableObjectFactory;
036: import org.apache.commons.pool.ObjectPool;
037: import org.apache.commons.pool.impl.StackObjectPool;
038:
039: /**
040: * <a href="RenderRequestFactory.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Brian Wing Shun Chan
043: *
044: */
045: public class RenderRequestFactory {
046:
047: public static RenderRequestImpl create(HttpServletRequest req,
048: Portlet portlet, CachePortlet cachePortlet,
049: PortletContext portletCtx, WindowState windowState,
050: PortletMode portletMode, PortletPreferences prefs)
051: throws Exception {
052:
053: return create(req, portlet, cachePortlet, portletCtx,
054: windowState, portletMode, prefs, 0);
055: }
056:
057: public static RenderRequestImpl create(HttpServletRequest req,
058: Portlet portlet, CachePortlet cachePortlet,
059: PortletContext portletCtx, WindowState windowState,
060: PortletMode portletMode, PortletPreferences prefs, long plid)
061: throws Exception {
062:
063: if (PropsValues.COMMONS_POOL_ENABLED) {
064: if (_log.isDebugEnabled()) {
065: _log.debug("Borrowing:\t"
066: + _instance._pool.getNumIdle() + "\t"
067: + _instance._pool.getNumActive());
068: }
069: }
070:
071: RenderRequestImpl renderRequestImpl = null;
072:
073: if (PropsValues.COMMONS_POOL_ENABLED) {
074: renderRequestImpl = (RenderRequestImpl) _instance._pool
075: .borrowObject();
076: } else {
077: renderRequestImpl = new RenderRequestImpl();
078: }
079:
080: renderRequestImpl.init(req, portlet, cachePortlet, portletCtx,
081: windowState, portletMode, prefs, plid);
082:
083: return renderRequestImpl;
084: }
085:
086: public static void recycle(RenderRequestImpl renderRequestImpl)
087: throws Exception {
088:
089: if (PropsValues.COMMONS_POOL_ENABLED) {
090: if (_log.isDebugEnabled()) {
091: _log.debug("Recycling:\t"
092: + _instance._pool.getNumIdle() + "\t"
093: + _instance._pool.getNumActive());
094: }
095:
096: _instance._pool.returnObject(renderRequestImpl);
097: } else if (renderRequestImpl != null) {
098: renderRequestImpl.recycle();
099: }
100: }
101:
102: private RenderRequestFactory() {
103: _pool = new StackObjectPool(new Factory());
104: }
105:
106: private static Log _log = LogFactory
107: .getLog(RenderRequestFactory.class);
108:
109: private static RenderRequestFactory _instance = new RenderRequestFactory();
110:
111: private ObjectPool _pool;
112:
113: private class Factory extends BasePoolableObjectFactory {
114:
115: public Object makeObject() {
116: return new RenderRequestImpl();
117: }
118:
119: public void passivateObject(Object obj) {
120: RenderRequestImpl renderRequestImpl = (RenderRequestImpl) obj;
121:
122: renderRequestImpl.recycle();
123: }
124:
125: }
126:
127: }
|