1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.model.Portlet;
20  import com.liferay.portal.util.PropsValues;
21  
22  import javax.portlet.PortletContext;
23  import javax.portlet.PortletMode;
24  import javax.portlet.PortletPreferences;
25  import javax.portlet.WindowState;
26  
27  import javax.servlet.http.HttpServletRequest;
28  
29  import org.apache.commons.pool.BasePoolableObjectFactory;
30  import org.apache.commons.pool.ObjectPool;
31  import org.apache.commons.pool.impl.StackObjectPool;
32  
33  /**
34   * <a href="RenderRequestFactory.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class RenderRequestFactory {
39  
40      public static RenderRequestImpl create(
41              HttpServletRequest request, Portlet portlet,
42              InvokerPortlet invokerPortlet, PortletContext portletContext,
43              WindowState windowState, PortletMode portletMode,
44              PortletPreferences prefs)
45          throws Exception {
46  
47          return create(
48              request, portlet, invokerPortlet, portletContext, windowState,
49              portletMode, prefs, 0);
50      }
51  
52      public static RenderRequestImpl create(
53              HttpServletRequest request, Portlet portlet,
54              InvokerPortlet invokerPortlet, PortletContext portletContext,
55              WindowState windowState, PortletMode portletMode,
56              PortletPreferences prefs, long plid)
57          throws Exception {
58  
59          if (PropsValues.COMMONS_POOL_ENABLED) {
60              if (_log.isDebugEnabled()) {
61                  _log.debug(
62                      "Borrowing:\t" + _instance._pool.getNumIdle() + "\t" +
63                          _instance._pool.getNumActive());
64              }
65          }
66  
67          RenderRequestImpl renderRequestImpl = null;
68  
69          if (PropsValues.COMMONS_POOL_ENABLED) {
70              renderRequestImpl =
71                  (RenderRequestImpl)_instance._pool.borrowObject();
72          }
73          else {
74              renderRequestImpl = new RenderRequestImpl();
75          }
76  
77          renderRequestImpl.init(
78              request, portlet, invokerPortlet, portletContext, windowState,
79              portletMode, prefs, plid);
80  
81          return renderRequestImpl;
82      }
83  
84      public static void recycle(RenderRequestImpl renderRequestImpl)
85          throws Exception {
86  
87          if (PropsValues.COMMONS_POOL_ENABLED) {
88              if (_log.isDebugEnabled()) {
89                  _log.debug(
90                      "Recycling:\t" + _instance._pool.getNumIdle() + "\t" +
91                          _instance._pool.getNumActive());
92              }
93  
94              _instance._pool.returnObject(renderRequestImpl);
95          }
96          else if (renderRequestImpl != null) {
97              renderRequestImpl.recycle();
98          }
99      }
100 
101     private RenderRequestFactory() {
102         _pool = new StackObjectPool(new Factory());
103     }
104 
105     private static Log _log = LogFactoryUtil.getLog(
106         RenderRequestFactory.class);
107 
108     private static RenderRequestFactory _instance = new RenderRequestFactory();
109 
110     private ObjectPool _pool;
111 
112     private class Factory extends BasePoolableObjectFactory {
113 
114         public Object makeObject() {
115             return new RenderRequestImpl();
116         }
117 
118         public void passivateObject(Object obj) {
119             RenderRequestImpl renderRequestImpl = (RenderRequestImpl)obj;
120 
121             renderRequestImpl.recycle();
122         }
123 
124     }
125 
126 }