1   /**
2    * Copyright (c) 2000-2007 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.portal.security.permission;
24  
25  import com.liferay.portal.kernel.security.permission.PermissionChecker;
26  import com.liferay.portal.model.User;
27  import com.liferay.portal.util.CachePropsUtil;
28  import com.liferay.portal.util.PropsUtil;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.commons.pool.BasePoolableObjectFactory;
33  import org.apache.commons.pool.ObjectPool;
34  import org.apache.commons.pool.impl.StackObjectPool;
35  
36  /**
37   * <a href="PermissionCheckerFactory.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Charles May
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class PermissionCheckerFactory {
44  
45      public static PermissionCheckerImpl create(User user, boolean checkGuest)
46          throws Exception {
47  
48          if (CachePropsUtil.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          PermissionCheckerImpl permissionChecker = null;
57  
58          if (CachePropsUtil.COMMONS_POOL_ENABLED) {
59              permissionChecker =
60                  (PermissionCheckerImpl)_instance._pool.borrowObject();
61          }
62          else {
63              permissionChecker = (PermissionCheckerImpl)Class.forName(
64                  PropsUtil.get(PropsUtil.PERMISSIONS_CHECKER)).newInstance();
65          }
66  
67          permissionChecker.init(user, checkGuest);
68  
69          return permissionChecker;
70      }
71  
72      public static void recycle(PermissionChecker permissionChecker)
73          throws Exception {
74  
75          if (CachePropsUtil.COMMONS_POOL_ENABLED) {
76              if (permissionChecker == null) {
77                  return;
78              }
79  
80              if (_log.isDebugEnabled()) {
81                  _log.debug(
82                      "Recycling:\t" + _instance._pool.getNumIdle() + "\t" +
83                          _instance._pool.getNumActive());
84              }
85  
86              _instance._pool.returnObject(permissionChecker);
87          }
88      }
89  
90      private PermissionCheckerFactory() {
91          _pool = new StackObjectPool(new Factory());
92      }
93  
94      private static Log _log = LogFactory.getLog(PermissionCheckerFactory.class);
95  
96      private static PermissionCheckerFactory _instance =
97          new PermissionCheckerFactory();
98  
99      private ObjectPool _pool;
100 
101     private class Factory extends BasePoolableObjectFactory {
102 
103         public Object makeObject() {
104             try {
105                 return Class.forName(
106                     PropsUtil.get(PropsUtil.PERMISSIONS_CHECKER)).newInstance();
107             }
108             catch (Exception e) {
109                 _log.error(e);
110 
111                 return null;
112             }
113         }
114 
115         public void passivateObject(Object obj) {
116             PermissionCheckerImpl permissionChecker =
117                 (PermissionCheckerImpl)obj;
118 
119             permissionChecker.recycle();
120         }
121 
122     }
123 
124 }