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