1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.model.Portlet;
28  import com.liferay.portal.util.PropsValues;
29  
30  import javax.portlet.PortletContext;
31  import javax.portlet.PortletMode;
32  import javax.portlet.PortletPreferences;
33  import javax.portlet.WindowState;
34  
35  import javax.servlet.http.HttpServletRequest;
36  
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="EventRequestFactory.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class EventRequestFactory {
47  
48      public static EventRequestImpl create(
49              HttpServletRequest request, Portlet portlet,
50              InvokerPortlet invokerPortlet, PortletContext portletContext,
51              WindowState windowState, PortletMode portletMode,
52              PortletPreferences prefs, long plid)
53          throws Exception {
54  
55          if (PropsValues.COMMONS_POOL_ENABLED) {
56              if (_log.isDebugEnabled()) {
57                  _log.debug(
58                      "Borrowing:\t" + _instance._pool.getNumIdle() + "\t" +
59                          _instance._pool.getNumActive());
60              }
61          }
62  
63          EventRequestImpl eventRequestImpl = null;
64  
65          if (PropsValues.COMMONS_POOL_ENABLED) {
66              eventRequestImpl = (EventRequestImpl)_instance._pool.borrowObject();
67          }
68          else {
69              eventRequestImpl = new EventRequestImpl();
70          }
71  
72          eventRequestImpl.init(
73              request, portlet, invokerPortlet, portletContext, windowState,
74              portletMode, prefs, plid);
75  
76          return eventRequestImpl;
77      }
78  
79      public static void recycle(EventRequestImpl eventRequestImpl)
80          throws Exception {
81  
82          if (PropsValues.COMMONS_POOL_ENABLED) {
83              if (_log.isDebugEnabled()) {
84                  _log.debug(
85                      "Recycling:\t" + _instance._pool.getNumIdle() + "\t" +
86                          _instance._pool.getNumActive());
87              }
88  
89              _instance._pool.returnObject(eventRequestImpl);
90          }
91          else if (eventRequestImpl != null) {
92              eventRequestImpl.recycle();
93          }
94      }
95  
96      private EventRequestFactory() {
97          _pool = new StackObjectPool(new Factory());
98      }
99  
100     private static Log _log =
101         LogFactoryUtil.getLog(EventRequestFactory.class);
102 
103     private static EventRequestFactory _instance = new EventRequestFactory();
104 
105     private ObjectPool _pool;
106 
107     private class Factory extends BasePoolableObjectFactory {
108 
109         public Object makeObject() {
110             return new EventRequestImpl();
111         }
112 
113         public void passivateObject(Object obj) {
114             EventRequestImpl eventRequestImpl = (EventRequestImpl)obj;
115 
116             eventRequestImpl.recycle();
117         }
118 
119     }
120 
121 }