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 request, Portlet portlet,
51              InvokerPortlet invokerPortlet, PortletContext portletContext,
52              WindowState windowState, PortletMode portletMode,
53              PortletPreferences prefs)
54          throws Exception {
55  
56          return create(
57              request, portlet, invokerPortlet, portletContext, windowState,
58              portletMode, prefs, 0);
59      }
60  
61      public static RenderRequestImpl create(
62              HttpServletRequest request, Portlet portlet,
63              InvokerPortlet invokerPortlet, PortletContext portletContext,
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 renderRequestImpl = null;
77  
78          if (PropsValues.COMMONS_POOL_ENABLED) {
79              renderRequestImpl =
80                  (RenderRequestImpl)_instance._pool.borrowObject();
81          }
82          else {
83              renderRequestImpl = new RenderRequestImpl();
84          }
85  
86          renderRequestImpl.init(
87              request, portlet, invokerPortlet, portletContext, windowState,
88              portletMode, prefs, plid);
89  
90          return renderRequestImpl;
91      }
92  
93      public static void recycle(RenderRequestImpl renderRequestImpl)
94          throws Exception {
95  
96          if (PropsValues.COMMONS_POOL_ENABLED) {
97              if (_log.isDebugEnabled()) {
98                  _log.debug(
99                      "Recycling:\t" + _instance._pool.getNumIdle() + "\t" +
100                         _instance._pool.getNumActive());
101             }
102 
103             _instance._pool.returnObject(renderRequestImpl);
104         }
105         else if (renderRequestImpl != null) {
106             renderRequestImpl.recycle();
107         }
108     }
109 
110     private RenderRequestFactory() {
111         _pool = new StackObjectPool(new Factory());
112     }
113 
114     private static Log _log =
115         LogFactory.getLog(RenderRequestFactory.class);
116 
117     private static RenderRequestFactory _instance = new RenderRequestFactory();
118 
119     private ObjectPool _pool;
120 
121     private class Factory extends BasePoolableObjectFactory {
122 
123         public Object makeObject() {
124             return new RenderRequestImpl();
125         }
126 
127         public void passivateObject(Object obj) {
128             RenderRequestImpl renderRequestImpl = (RenderRequestImpl)obj;
129 
130             renderRequestImpl.recycle();
131         }
132 
133     }
134 
135 }