1   /**
2    * Copyright (c) 2000-2008 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.PropsValues;
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="EventRequestFactory.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class EventRequestFactory {
48  
49      public static EventRequestImpl create(
50              HttpServletRequest req, Portlet portlet,
51              InvokerPortlet invokerPortlet, PortletContext portletCtx,
52              WindowState windowState, PortletMode portletMode,
53              PortletPreferences prefs, long plid)
54          throws Exception {
55  
56          if (PropsValues.COMMONS_POOL_ENABLED) {
57              if (_log.isDebugEnabled()) {
58                  _log.debug(
59                      "Borrowing:\t" + _instance._pool.getNumIdle() + "\t" +
60                          _instance._pool.getNumActive());
61              }
62          }
63  
64          EventRequestImpl eventReqImpl = null;
65  
66          if (PropsValues.COMMONS_POOL_ENABLED) {
67              eventReqImpl = (EventRequestImpl)_instance._pool.borrowObject();
68          }
69          else {
70              eventReqImpl = new EventRequestImpl();
71          }
72  
73          eventReqImpl.init(
74              req, portlet, invokerPortlet, portletCtx, windowState, portletMode,
75              prefs, plid);
76  
77          return eventReqImpl;
78      }
79  
80      public static void recycle(EventRequestImpl eventReqImpl)
81          throws Exception {
82  
83          if (PropsValues.COMMONS_POOL_ENABLED) {
84              if (_log.isDebugEnabled()) {
85                  _log.debug(
86                      "Recycling:\t" + _instance._pool.getNumIdle() + "\t" +
87                          _instance._pool.getNumActive());
88              }
89  
90              _instance._pool.returnObject(eventReqImpl);
91          }
92          else if (eventReqImpl != null) {
93              eventReqImpl.recycle();
94          }
95      }
96  
97      private EventRequestFactory() {
98          _pool = new StackObjectPool(new Factory());
99      }
100 
101     private static Log _log =
102         LogFactory.getLog(EventRequestFactory.class);
103 
104     private static EventRequestFactory _instance = new EventRequestFactory();
105 
106     private ObjectPool _pool;
107 
108     private class Factory extends BasePoolableObjectFactory {
109 
110         public Object makeObject() {
111             return new EventRequestImpl();
112         }
113 
114         public void passivateObject(Object obj) {
115             EventRequestImpl eventReqImpl = (EventRequestImpl)obj;
116 
117             eventReqImpl.recycle();
118         }
119 
120     }
121 
122 }