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.model.Portlet;
28  import com.liferay.portal.util.PropsValues;
29  
30  import javax.portlet.PortletContext;
31  import javax.portlet.PortletMode;
32  import javax.portlet.PortletPreferences;
33  import javax.portlet.WindowState;
34  
35  import javax.servlet.http.HttpServletRequest;
36  
37  import org.apache.commons.pool.BasePoolableObjectFactory;
38  import org.apache.commons.pool.ObjectPool;
39  import org.apache.commons.pool.impl.StackObjectPool;
40  
41  /**
42   * <a href="RenderRequestFactory.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class RenderRequestFactory {
47  
48      public static RenderRequestImpl create(
49              HttpServletRequest request, Portlet portlet,
50              InvokerPortlet invokerPortlet, PortletContext portletContext,
51              WindowState windowState, PortletMode portletMode,
52              PortletPreferences prefs)
53          throws Exception {
54  
55          return create(
56              request, portlet, invokerPortlet, portletContext, windowState,
57              portletMode, prefs, 0);
58      }
59  
60      public static RenderRequestImpl create(
61              HttpServletRequest request, Portlet portlet,
62              InvokerPortlet invokerPortlet, PortletContext portletContext,
63              WindowState windowState, PortletMode portletMode,
64              PortletPreferences prefs, long plid)
65          throws Exception {
66  
67          if (PropsValues.COMMONS_POOL_ENABLED) {
68              if (_log.isDebugEnabled()) {
69                  _log.debug(
70                      "Borrowing:\t" + _instance._pool.getNumIdle() + "\t" +
71                          _instance._pool.getNumActive());
72              }
73          }
74  
75          RenderRequestImpl renderRequestImpl = null;
76  
77          if (PropsValues.COMMONS_POOL_ENABLED) {
78              renderRequestImpl =
79                  (RenderRequestImpl)_instance._pool.borrowObject();
80          }
81          else {
82              renderRequestImpl = new RenderRequestImpl();
83          }
84  
85          renderRequestImpl.init(
86              request, portlet, invokerPortlet, portletContext, windowState,
87              portletMode, prefs, plid);
88  
89          return renderRequestImpl;
90      }
91  
92      public static void recycle(RenderRequestImpl renderRequestImpl)
93          throws Exception {
94  
95          if (PropsValues.COMMONS_POOL_ENABLED) {
96              if (_log.isDebugEnabled()) {
97                  _log.debug(
98                      "Recycling:\t" + _instance._pool.getNumIdle() + "\t" +
99                          _instance._pool.getNumActive());
100             }
101 
102             _instance._pool.returnObject(renderRequestImpl);
103         }
104         else if (renderRequestImpl != null) {
105             renderRequestImpl.recycle();
106         }
107     }
108 
109     private RenderRequestFactory() {
110         _pool = new StackObjectPool(new Factory());
111     }
112 
113     private static Log _log =
114         LogFactoryUtil.getLog(RenderRequestFactory.class);
115 
116     private static RenderRequestFactory _instance = new RenderRequestFactory();
117 
118     private ObjectPool _pool;
119 
120     private class Factory extends BasePoolableObjectFactory {
121 
122         public Object makeObject() {
123             return new RenderRequestImpl();
124         }
125 
126         public void passivateObject(Object obj) {
127             RenderRequestImpl renderRequestImpl = (RenderRequestImpl)obj;
128 
129             renderRequestImpl.recycle();
130         }
131 
132     }
133 
134 }