1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.model.Portlet;
25  import com.liferay.portal.util.PropsValues;
26  
27  import javax.portlet.PortletContext;
28  import javax.portlet.PortletMode;
29  import javax.portlet.PortletPreferences;
30  import javax.portlet.WindowState;
31  
32  import javax.servlet.http.HttpServletRequest;
33  
34  import org.apache.commons.pool.BasePoolableObjectFactory;
35  import org.apache.commons.pool.ObjectPool;
36  import org.apache.commons.pool.impl.StackObjectPool;
37  
38  /**
39   * <a href="RenderRequestFactory.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class RenderRequestFactory {
45  
46      public static RenderRequestImpl create(
47              HttpServletRequest request, Portlet portlet,
48              InvokerPortlet invokerPortlet, PortletContext portletContext,
49              WindowState windowState, PortletMode portletMode,
50              PortletPreferences prefs)
51          throws Exception {
52  
53          return create(
54              request, portlet, invokerPortlet, portletContext, windowState,
55              portletMode, prefs, 0);
56      }
57  
58      public static RenderRequestImpl create(
59              HttpServletRequest request, Portlet portlet,
60              InvokerPortlet invokerPortlet, PortletContext portletContext,
61              WindowState windowState, PortletMode portletMode,
62              PortletPreferences prefs, long plid)
63          throws Exception {
64  
65          if (PropsValues.COMMONS_POOL_ENABLED) {
66              if (_log.isDebugEnabled()) {
67                  _log.debug(
68                      "Borrowing:\t" + _instance._pool.getNumIdle() + "\t" +
69                          _instance._pool.getNumActive());
70              }
71          }
72  
73          RenderRequestImpl renderRequestImpl = null;
74  
75          if (PropsValues.COMMONS_POOL_ENABLED) {
76              renderRequestImpl =
77                  (RenderRequestImpl)_instance._pool.borrowObject();
78          }
79          else {
80              renderRequestImpl = new RenderRequestImpl();
81          }
82  
83          renderRequestImpl.init(
84              request, portlet, invokerPortlet, portletContext, windowState,
85              portletMode, prefs, plid);
86  
87          return renderRequestImpl;
88      }
89  
90      public static void recycle(RenderRequestImpl renderRequestImpl)
91          throws Exception {
92  
93          if (PropsValues.COMMONS_POOL_ENABLED) {
94              if (_log.isDebugEnabled()) {
95                  _log.debug(
96                      "Recycling:\t" + _instance._pool.getNumIdle() + "\t" +
97                          _instance._pool.getNumActive());
98              }
99  
100             _instance._pool.returnObject(renderRequestImpl);
101         }
102         else if (renderRequestImpl != null) {
103             renderRequestImpl.recycle();
104         }
105     }
106 
107     private RenderRequestFactory() {
108         _pool = new StackObjectPool(new Factory());
109     }
110 
111     private static Log _log =
112         LogFactoryUtil.getLog(RenderRequestFactory.class);
113 
114     private static RenderRequestFactory _instance = new RenderRequestFactory();
115 
116     private ObjectPool _pool;
117 
118     private class Factory extends BasePoolableObjectFactory {
119 
120         public Object makeObject() {
121             return new RenderRequestImpl();
122         }
123 
124         public void passivateObject(Object obj) {
125             RenderRequestImpl renderRequestImpl = (RenderRequestImpl)obj;
126 
127             renderRequestImpl.recycle();
128         }
129 
130     }
131 
132 }