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.util.PropsValues;
26  
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.commons.pool.BasePoolableObjectFactory;
32  import org.apache.commons.pool.ObjectPool;
33  import org.apache.commons.pool.impl.StackObjectPool;
34  
35  /**
36   * <a href="ResourceResponseFactory.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class ResourceResponseFactory {
42  
43      public static ResourceResponseImpl create(
44              ResourceRequestImpl req, HttpServletResponse res,
45              String portletName, long companyId)
46          throws Exception {
47  
48          return create(req, res, portletName, companyId, 0);
49      }
50  
51      public static ResourceResponseImpl create(
52              ResourceRequestImpl req, HttpServletResponse res,
53              String portletName, long companyId, 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          ResourceResponseImpl resourceResImpl = null;
65  
66          if (PropsValues.COMMONS_POOL_ENABLED) {
67              resourceResImpl =
68                  (ResourceResponseImpl)_instance._pool.borrowObject();
69          }
70          else {
71              resourceResImpl = new ResourceResponseImpl();
72          }
73  
74          resourceResImpl.init(req, res, portletName, companyId, plid);
75  
76          return resourceResImpl;
77      }
78  
79      public static void recycle(ResourceResponseImpl resourceResImpl)
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(resourceResImpl);
90          }
91          else if (resourceResImpl != null) {
92              resourceResImpl.recycle();
93          }
94      }
95  
96      private ResourceResponseFactory() {
97          _pool = new StackObjectPool(new Factory());
98      }
99  
100     private static Log _log = LogFactory.getLog(ResourceResponseFactory.class);
101 
102     private static ResourceResponseFactory _instance =
103         new ResourceResponseFactory();
104 
105     private ObjectPool _pool;
106 
107     private class Factory extends BasePoolableObjectFactory {
108 
109         public Object makeObject() {
110             return new ResourceResponseImpl();
111         }
112 
113         public void passivateObject(Object obj) {
114             ResourceResponseImpl resourceResImpl = (ResourceResponseImpl)obj;
115 
116             resourceResImpl.recycle();
117         }
118 
119     }
120 
121 }