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