1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.util.PropsValues;
28  
29  import javax.servlet.http.HttpServletResponse;
30  
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  public class ResourceResponseFactory {
41  
42      public static ResourceResponseImpl create(
43              ResourceRequestImpl resourceRequestImpl,
44              HttpServletResponse response, String portletName, long companyId)
45          throws Exception {
46  
47          return create(resourceRequestImpl, response, portletName, companyId, 0);
48      }
49  
50      public static ResourceResponseImpl create(
51              ResourceRequestImpl resourceRequestImpl,
52              HttpServletResponse response, String portletName, long companyId,
53              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 resourceResponseImpl = null;
65  
66          if (PropsValues.COMMONS_POOL_ENABLED) {
67              resourceResponseImpl =
68                  (ResourceResponseImpl)_instance._pool.borrowObject();
69          }
70          else {
71              resourceResponseImpl = new ResourceResponseImpl();
72          }
73  
74          resourceResponseImpl.init(
75              resourceRequestImpl, response, portletName, companyId, plid);
76  
77          return resourceResponseImpl;
78      }
79  
80      public static void recycle(ResourceResponseImpl resourceResponseImpl)
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(resourceResponseImpl);
91          }
92          else if (resourceResponseImpl != null) {
93              resourceResponseImpl.recycle();
94          }
95      }
96  
97      private ResourceResponseFactory() {
98          _pool = new StackObjectPool(new Factory());
99      }
100 
101     private static Log _log =
102         LogFactoryUtil.getLog(ResourceResponseFactory.class);
103 
104     private static ResourceResponseFactory _instance =
105         new ResourceResponseFactory();
106 
107     private ObjectPool _pool;
108 
109     private class Factory extends BasePoolableObjectFactory {
110 
111         public Object makeObject() {
112             return new ResourceResponseImpl();
113         }
114 
115         public void passivateObject(Object obj) {
116             ResourceResponseImpl resourceResponseImpl =
117                 (ResourceResponseImpl)obj;
118 
119             resourceResponseImpl.recycle();
120         }
121 
122     }
123 
124 }