1   /**
2    * Copyright (c) 2000-2008 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.model.User;
26  import com.liferay.portal.util.PropsValues;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
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   */
41  public class PermissionCheckerFactory {
42  
43      public static PermissionChecker create(User user, boolean checkGuest)
44          throws Exception {
45  
46          if (PropsValues.COMMONS_POOL_ENABLED) {
47              if (_log.isDebugEnabled()) {
48                  _log.debug(
49                      "Borrowing:\t" + _instance._pool.getNumIdle() + "\t" +
50                          _instance._pool.getNumActive());
51              }
52          }
53  
54          PermissionChecker permissionChecker = null;
55  
56          if (PropsValues.COMMONS_POOL_ENABLED) {
57              permissionChecker =
58                  (PermissionChecker)_instance._pool.borrowObject();
59          }
60          else {
61              permissionChecker = (PermissionChecker)Class.forName(
62                  PropsValues.PERMISSIONS_CHECKER).newInstance();
63          }
64  
65          permissionChecker.init(user, checkGuest);
66  
67          return permissionChecker;
68      }
69  
70      public static void recycle(PermissionChecker permissionChecker)
71          throws Exception {
72  
73          if (PropsValues.COMMONS_POOL_ENABLED) {
74              if (permissionChecker == null) {
75                  return;
76              }
77  
78              if (_log.isDebugEnabled()) {
79                  _log.debug(
80                      "Recycling:\t" + _instance._pool.getNumIdle() + "\t" +
81                          _instance._pool.getNumActive());
82              }
83  
84              _instance._pool.returnObject(permissionChecker);
85          }
86          else if (permissionChecker != null) {
87              permissionChecker.recycle();
88          }
89      }
90  
91      private PermissionCheckerFactory() {
92          _pool = new StackObjectPool(new Factory());
93      }
94  
95      private static Log _log = LogFactory.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 }