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