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