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.model.Portlet;
26  import com.liferay.portal.util.PropsValues;
27  
28  import javax.portlet.PortletContext;
29  import javax.portlet.PortletMode;
30  import javax.portlet.PortletPreferences;
31  import javax.portlet.WindowState;
32  
33  import javax.servlet.http.HttpServletRequest;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
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   */
47  public class RenderRequestFactory {
48  
49      public static RenderRequestImpl create(
50              HttpServletRequest req, Portlet portlet,
51              InvokerPortlet invokerPortlet, PortletContext portletCtx,
52              WindowState windowState, PortletMode portletMode,
53              PortletPreferences prefs)
54          throws Exception {
55  
56          return create(
57              req, portlet, invokerPortlet, portletCtx, windowState, portletMode,
58              prefs, 0);
59      }
60  
61      public static RenderRequestImpl create(
62              HttpServletRequest req, Portlet portlet,
63              InvokerPortlet invokerPortlet, PortletContext portletCtx,
64              WindowState windowState, PortletMode portletMode,
65              PortletPreferences prefs, long plid)
66          throws Exception {
67  
68          if (PropsValues.COMMONS_POOL_ENABLED) {
69              if (_log.isDebugEnabled()) {
70                  _log.debug(
71                      "Borrowing:\t" + _instance._pool.getNumIdle() + "\t" +
72                          _instance._pool.getNumActive());
73              }
74          }
75  
76          RenderRequestImpl renderReqImpl = null;
77  
78          if (PropsValues.COMMONS_POOL_ENABLED) {
79              renderReqImpl = (RenderRequestImpl)_instance._pool.borrowObject();
80          }
81          else {
82              renderReqImpl = new RenderRequestImpl();
83          }
84  
85          renderReqImpl.init(
86              req, portlet, invokerPortlet, portletCtx, windowState, portletMode,
87              prefs, plid);
88  
89          return renderReqImpl;
90      }
91  
92      public static void recycle(RenderRequestImpl renderReqImpl)
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(renderReqImpl);
103         }
104         else if (renderReqImpl != null) {
105             renderReqImpl.recycle();
106         }
107     }
108 
109     private RenderRequestFactory() {
110         _pool = new StackObjectPool(new Factory());
111     }
112 
113     private static Log _log =
114         LogFactory.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 renderReqImpl = (RenderRequestImpl)obj;
128 
129             renderReqImpl.recycle();
130         }
131 
132     }
133 
134 }