1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.util.PropsValues;
25  
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.apache.commons.pool.BasePoolableObjectFactory;
29  import org.apache.commons.pool.ObjectPool;
30  import org.apache.commons.pool.impl.StackObjectPool;
31  
32  /**
33   * <a href="ResourceResponseFactory.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public class ResourceResponseFactory {
39  
40      public static ResourceResponseImpl create(
41              ResourceRequestImpl resourceRequestImpl,
42              HttpServletResponse response, String portletName, long companyId)
43          throws Exception {
44  
45          return create(resourceRequestImpl, response, portletName, companyId, 0);
46      }
47  
48      public static ResourceResponseImpl create(
49              ResourceRequestImpl resourceRequestImpl,
50              HttpServletResponse response, String portletName, long companyId,
51              long plid)
52          throws Exception {
53  
54          if (PropsValues.COMMONS_POOL_ENABLED) {
55              if (_log.isDebugEnabled()) {
56                  _log.debug(
57                      "Borrowing:\t" + _instance._pool.getNumIdle() + "\t" +
58                          _instance._pool.getNumActive());
59              }
60          }
61  
62          ResourceResponseImpl resourceResponseImpl = null;
63  
64          if (PropsValues.COMMONS_POOL_ENABLED) {
65              resourceResponseImpl =
66                  (ResourceResponseImpl)_instance._pool.borrowObject();
67          }
68          else {
69              resourceResponseImpl = new ResourceResponseImpl();
70          }
71  
72          resourceResponseImpl.init(
73              resourceRequestImpl, response, portletName, companyId, plid);
74  
75          return resourceResponseImpl;
76      }
77  
78      public static void recycle(ResourceResponseImpl resourceResponseImpl)
79          throws Exception {
80  
81          if (PropsValues.COMMONS_POOL_ENABLED) {
82              if (_log.isDebugEnabled()) {
83                  _log.debug(
84                      "Recycling:\t" + _instance._pool.getNumIdle() + "\t" +
85                          _instance._pool.getNumActive());
86              }
87  
88              _instance._pool.returnObject(resourceResponseImpl);
89          }
90          else if (resourceResponseImpl != null) {
91              resourceResponseImpl.recycle();
92          }
93      }
94  
95      private ResourceResponseFactory() {
96          _pool = new StackObjectPool(new Factory());
97      }
98  
99      private static Log _log =
100          LogFactoryUtil.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 resourceResponseImpl =
115                 (ResourceResponseImpl)obj;
116 
117             resourceResponseImpl.recycle();
118         }
119 
120     }
121 
122 }