1   /**
2    * Copyright (c) 2000-2007 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.CachePropsUtil;
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, CachePortlet cachePortlet,
51              PortletContext portletCtx, WindowState windowState,
52              PortletMode portletMode, PortletPreferences prefs)
53          throws Exception {
54  
55          return create(
56              req, portlet, cachePortlet, portletCtx, windowState, portletMode,
57              prefs, 0);
58      }
59  
60      public static RenderRequestImpl create(
61              HttpServletRequest req, Portlet portlet, CachePortlet cachePortlet,
62              PortletContext portletCtx, WindowState windowState,
63              PortletMode portletMode, PortletPreferences prefs, long plid)
64          throws Exception {
65  
66          if (CachePropsUtil.COMMONS_POOL_ENABLED) {
67              if (_log.isDebugEnabled()) {
68                  _log.debug(
69                      "Borrowing:\t" + _instance._pool.getNumIdle() + "\t" +
70                          _instance._pool.getNumActive());
71              }
72          }
73  
74          RenderRequestImpl renderRequestImpl = null;
75  
76          if (CachePropsUtil.COMMONS_POOL_ENABLED) {
77              renderRequestImpl =
78                  (RenderRequestImpl)_instance._pool.borrowObject();
79          }
80          else {
81              renderRequestImpl = new RenderRequestImpl();
82          }
83  
84          renderRequestImpl.init(
85              req, portlet, cachePortlet, portletCtx, windowState, portletMode,
86              prefs, plid);
87  
88          return renderRequestImpl;
89      }
90  
91      public static void recycle(RenderRequestImpl renderRequestImpl)
92          throws Exception {
93  
94          if (CachePropsUtil.COMMONS_POOL_ENABLED) {
95              if (_log.isDebugEnabled()) {
96                  _log.debug(
97                      "Recycling:\t" + _instance._pool.getNumIdle() + "\t" +
98                          _instance._pool.getNumActive());
99              }
100 
101             _instance._pool.returnObject(renderRequestImpl);
102         }
103     }
104 
105     private RenderRequestFactory() {
106         _pool = new StackObjectPool(new Factory());
107     }
108 
109     private static Log _log =
110         LogFactory.getLog(RenderRequestFactory.class);
111 
112     private static RenderRequestFactory _instance = new RenderRequestFactory();
113 
114     private ObjectPool _pool;
115 
116     private class Factory extends BasePoolableObjectFactory {
117 
118         public Object makeObject() {
119             return new RenderRequestImpl();
120         }
121 
122         public void passivateObject(Object obj) {
123             RenderRequestImpl renderRequestImpl = (RenderRequestImpl)obj;
124 
125             renderRequestImpl.recycle();
126         }
127 
128     }
129 
130 }