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.model.Portlet;
23  import com.liferay.portal.util.PropsValues;
24  
25  import javax.portlet.PortletContext;
26  import javax.portlet.PortletMode;
27  import javax.portlet.PortletPreferences;
28  import javax.portlet.WindowState;
29  
30  import javax.servlet.http.HttpServletRequest;
31  
32  import org.apache.commons.pool.BasePoolableObjectFactory;
33  import org.apache.commons.pool.ObjectPool;
34  import org.apache.commons.pool.impl.StackObjectPool;
35  
36  /**
37   * <a href="ResourceRequestFactory.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class ResourceRequestFactory {
43  
44      public static ResourceRequestImpl create(
45              HttpServletRequest request, Portlet portlet,
46              InvokerPortlet invokerPortlet, PortletContext portletContext,
47              WindowState windowState, PortletMode portletMode,
48              PortletPreferences prefs, long plid)
49          throws Exception {
50  
51          ResourceRequestImpl resourceRequestImpl = null;
52  
53          if (PropsValues.COMMONS_POOL_ENABLED) {
54              resourceRequestImpl =
55                  (ResourceRequestImpl)_instance._pool.borrowObject();
56          }
57          else {
58              resourceRequestImpl = new ResourceRequestImpl();
59          }
60  
61          resourceRequestImpl.init(
62              request, portlet, invokerPortlet, portletContext, windowState,
63              portletMode, prefs, plid);
64  
65          return resourceRequestImpl;
66      }
67  
68      public static void recycle(ResourceRequestImpl resourceRequestImpl)
69          throws Exception {
70  
71          if (PropsValues.COMMONS_POOL_ENABLED) {
72              _instance._pool.returnObject(resourceRequestImpl);
73          }
74          else if (resourceRequestImpl != null) {
75              resourceRequestImpl.recycle();
76          }
77      }
78  
79      private ResourceRequestFactory() {
80          _pool = new StackObjectPool(new Factory());
81      }
82  
83      private static ResourceRequestFactory _instance =
84          new ResourceRequestFactory();
85  
86      private ObjectPool _pool;
87  
88      private class Factory extends BasePoolableObjectFactory {
89  
90          public Object makeObject() {
91              return new ResourceRequestImpl();
92          }
93  
94          public void passivateObject(Object obj) {
95              ResourceRequestImpl resourceRequestImpl = (ResourceRequestImpl)obj;
96  
97              resourceRequestImpl.recycle();
98          }
99  
100     }
101 
102 }