1   /**
2    * Copyright (c) 2000-2008 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.PropsValues;
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 (PropsValues.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 renderResImpl = null;
65  
66          if (PropsValues.COMMONS_POOL_ENABLED) {
67              renderResImpl = (RenderResponseImpl)_instance._pool.borrowObject();
68          }
69          else {
70              renderResImpl = new RenderResponseImpl();
71          }
72  
73          renderResImpl.init(req, res, portletName, companyId, plid);
74  
75          return renderResImpl;
76      }
77  
78      public static void recycle(RenderResponseImpl renderResImpl)
79          throws Exception {
80  
81          if (PropsValues.COMMONS_POOL_ENABLED) {
82              if (_log.isDebugEnabled()) {
83                  _log.debug(
84                      "Recycling:\t" + _instance._pool.getNumIdle() + "\t" +
85                          _instance._pool.getNumActive());
86              }
87  
88              _instance._pool.returnObject(renderResImpl);
89          }
90          else if (renderResImpl != null) {
91              renderResImpl.recycle();
92          }
93      }
94  
95      private RenderResponseFactory() {
96          _pool = new StackObjectPool(new Factory());
97      }
98  
99      private static Log _log = LogFactory.getLog(RenderResponseFactory.class);
100 
101     private static RenderResponseFactory _instance =
102         new RenderResponseFactory();
103 
104     private ObjectPool _pool;
105 
106     private class Factory extends BasePoolableObjectFactory {
107 
108         public Object makeObject() {
109             return new RenderResponseImpl();
110         }
111 
112         public void passivateObject(Object obj) {
113             RenderResponseImpl renderResImpl = (RenderResponseImpl)obj;
114 
115             renderResImpl.recycle();
116         }
117 
118     }
119 
120 }