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