1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.util.CachePropsUtil;
26  
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.commons.pool.BasePoolableObjectFactory;
32  import org.apache.commons.pool.ObjectPool;
33  import org.apache.commons.pool.impl.StackObjectPool;
34  
35  /**
36   * <a href="RenderResponseFactory.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class RenderResponseFactory {
42  
43      public static RenderResponseImpl create(
44              RenderRequestImpl req, HttpServletResponse res, String portletName,
45              long companyId)
46          throws Exception {
47  
48          return create(req, res, portletName, companyId, 0);
49      }
50  
51      public static RenderResponseImpl create(
52              RenderRequestImpl req, HttpServletResponse res, String portletName,
53              long companyId, long plid)
54          throws Exception {
55  
56          if (CachePropsUtil.COMMONS_POOL_ENABLED) {
57              if (_log.isDebugEnabled()) {
58                  _log.debug(
59                      "Borrowing:\t" + _instance._pool.getNumIdle() + "\t" +
60                          _instance._pool.getNumActive());
61              }
62          }
63  
64          RenderResponseImpl renderResponseImpl = null;
65  
66          if (CachePropsUtil.COMMONS_POOL_ENABLED) {
67              renderResponseImpl =
68                  (RenderResponseImpl)_instance._pool.borrowObject();
69          }
70          else {
71              renderResponseImpl = new RenderResponseImpl();
72          }
73  
74          renderResponseImpl.init(req, res, portletName, companyId, plid);
75  
76          return renderResponseImpl;
77      }
78  
79      public static void recycle(RenderResponseImpl renderResponseImpl)
80          throws Exception {
81  
82          if (CachePropsUtil.COMMONS_POOL_ENABLED) {
83              if (_log.isDebugEnabled()) {
84                  _log.debug(
85                      "Recycling:\t" + _instance._pool.getNumIdle() + "\t" +
86                          _instance._pool.getNumActive());
87              }
88  
89              _instance._pool.returnObject(renderResponseImpl);
90          }
91      }
92  
93      private RenderResponseFactory() {
94          _pool = new StackObjectPool(new Factory());
95      }
96  
97      private static Log _log = LogFactory.getLog(RenderResponseFactory.class);
98  
99      private static RenderResponseFactory _instance =
100         new RenderResponseFactory();
101 
102     private ObjectPool _pool;
103 
104     private class Factory extends BasePoolableObjectFactory {
105 
106         public Object makeObject() {
107             return new RenderResponseImpl();
108         }
109 
110         public void passivateObject(Object obj) {
111             RenderResponseImpl renderResponseImpl = (RenderResponseImpl)obj;
112 
113             renderResponseImpl.recycle();
114         }
115 
116     }
117 
118 }