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