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