1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.util.PropsValues;
28  
29  import javax.servlet.http.HttpServletResponse;
30  
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  public class RenderResponseFactory {
41  
42      public static RenderResponseImpl create(
43              RenderRequestImpl renderRequestImpl, HttpServletResponse response,
44              String portletName, long companyId)
45          throws Exception {
46  
47          return create(renderRequestImpl, response, portletName, companyId, 0);
48      }
49  
50      public static RenderResponseImpl create(
51              RenderRequestImpl renderRequestImpl, HttpServletResponse response,
52              String portletName, long companyId, long plid)
53          throws Exception {
54  
55          if (PropsValues.COMMONS_POOL_ENABLED) {
56              if (_log.isDebugEnabled()) {
57                  _log.debug(
58                      "Borrowing:\t" + _instance._pool.getNumIdle() + "\t" +
59                          _instance._pool.getNumActive());
60              }
61          }
62  
63          RenderResponseImpl renderResponseImpl = null;
64  
65          if (PropsValues.COMMONS_POOL_ENABLED) {
66              renderResponseImpl =
67                  (RenderResponseImpl)_instance._pool.borrowObject();
68          }
69          else {
70              renderResponseImpl = new RenderResponseImpl();
71          }
72  
73          renderResponseImpl.init(
74              renderRequestImpl, response, portletName, companyId, plid);
75  
76          return renderResponseImpl;
77      }
78  
79      public static void recycle(RenderResponseImpl renderResponseImpl)
80          throws Exception {
81  
82          if (PropsValues.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          else if (renderResponseImpl != null) {
92              renderResponseImpl.recycle();
93          }
94      }
95  
96      private RenderResponseFactory() {
97          _pool = new StackObjectPool(new Factory());
98      }
99  
100     private static Log _log =
101         LogFactoryUtil.getLog(RenderResponseFactory.class);
102 
103     private static RenderResponseFactory _instance =
104         new RenderResponseFactory();
105 
106     private ObjectPool _pool;
107 
108     private class Factory extends BasePoolableObjectFactory {
109 
110         public Object makeObject() {
111             return new RenderResponseImpl();
112         }
113 
114         public void passivateObject(Object obj) {
115             RenderResponseImpl renderResponseImpl = (RenderResponseImpl)obj;
116 
117             renderResponseImpl.recycle();
118         }
119 
120     }
121 
122 }