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.cache.MultiVMPoolUtil;
23  import com.liferay.portal.kernel.cache.PortalCache;
24  import com.liferay.portal.kernel.util.InitialThreadLocal;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.util.PropsValues;
27  
28  import java.util.Map;
29  
30  import org.apache.commons.collections.map.LRUMap;
31  
32  /**
33   * <a href="PermissionCacheUtil.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Charles May
36   * @author Michael Young
37   *
38   */
39  public class PermissionCacheUtil {
40  
41      public static final String CACHE_NAME = PermissionCacheUtil.class.getName();
42  
43      public static void clearCache() {
44          clearLocalCache();
45  
46          _cache.removeAll();
47      }
48  
49      public static void clearLocalCache() {
50          if (_localCacheEnabled.get().booleanValue()) {
51              Map<String, Object> localCache = _localCache.get();
52  
53              localCache.clear();
54          }
55      }
56  
57      public static PermissionCheckerBag getBag(long userId, long groupId) {
58          PermissionCheckerBag bag = null;
59  
60          String key = _encodeKey(userId, groupId);
61  
62          if (_localCacheEnabled.get().booleanValue()) {
63              Map<String, Object> localCache = _localCache.get();
64  
65              bag = (PermissionCheckerBag)localCache.get(key);
66          }
67  
68          if (bag == null) {
69              bag = (PermissionCheckerBag)MultiVMPoolUtil.get(_cache, key);
70          }
71  
72          return bag;
73      }
74  
75      public static Boolean getPermission(
76          long userId, long groupId, String name, String primKey,
77          String actionId) {
78  
79          Boolean value = null;
80  
81          String key = _encodeKey(userId, groupId, name, primKey, actionId);
82  
83          if (_localCacheEnabled.get().booleanValue()) {
84              Map<String, Object> localCache = _localCache.get();
85  
86              value = (Boolean)localCache.get(key);
87          }
88  
89          if (value == null) {
90              value = (Boolean)MultiVMPoolUtil.get(_cache, key);
91          }
92  
93          return value;
94      }
95  
96      public static PermissionCheckerBag putBag(
97          long userId, long groupId, PermissionCheckerBag bag) {
98  
99          if (bag != null) {
100             String key = _encodeKey(userId, groupId);
101 
102             if (_localCacheEnabled.get().booleanValue()) {
103                 Map<String, Object> localCache = _localCache.get();
104 
105                 localCache.put(key, bag);
106             }
107 
108             MultiVMPoolUtil.put(_cache, key, bag);
109         }
110 
111         return bag;
112     }
113 
114     public static Boolean putPermission(
115         long userId, long groupId, String name, String primKey, String actionId,
116         Boolean value) {
117 
118         if (value != null) {
119             String key = _encodeKey(userId, groupId, name, primKey, actionId);
120 
121             if (_localCacheEnabled.get().booleanValue()) {
122                 Map<String, Object> localCache = _localCache.get();
123 
124                 localCache.put(key, value);
125             }
126 
127             MultiVMPoolUtil.put(_cache, key, value);
128         }
129 
130         return value;
131     }
132 
133     public static void setLocalCacheEnabled(boolean localCacheEnabled) {
134         if (_localCacheAvailable) {
135             _localCacheEnabled.set(Boolean.valueOf(localCacheEnabled));
136         }
137     }
138 
139     private static String _encodeKey(long userId, long groupId) {
140         StringBuilder sb = new StringBuilder();
141 
142         sb.append(CACHE_NAME);
143         sb.append(StringPool.POUND);
144         sb.append(userId);
145         sb.append(StringPool.POUND);
146         sb.append(groupId);
147 
148         return sb.toString();
149     }
150 
151     private static String _encodeKey(
152         long userId, long groupId, String name, String primKey,
153         String actionId) {
154 
155         StringBuilder sb = new StringBuilder();
156 
157         sb.append(CACHE_NAME);
158         sb.append(StringPool.POUND);
159         sb.append(userId);
160         sb.append(StringPool.POUND);
161         sb.append(groupId);
162         sb.append(StringPool.POUND);
163         sb.append(name);
164         sb.append(StringPool.POUND);
165         sb.append(primKey);
166         sb.append(StringPool.POUND);
167         sb.append(actionId);
168 
169         return sb.toString();
170     }
171 
172     private static PortalCache _cache = MultiVMPoolUtil.getCache(
173         CACHE_NAME, true);
174 
175     private static ThreadLocal<Map> _localCache;
176     private static boolean _localCacheAvailable;
177     private static ThreadLocal<Boolean> _localCacheEnabled =
178         new InitialThreadLocal<Boolean>(Boolean.FALSE);
179 
180     static {
181         if (PropsValues.PERMISSIONS_THREAD_LOCAL_CACHE_MAX_SIZE > 0) {
182             _localCache = new InitialThreadLocal<Map>(new LRUMap(
183                 PropsValues.PERMISSIONS_THREAD_LOCAL_CACHE_MAX_SIZE));
184             _localCacheAvailable = true;
185         }
186     }
187 
188 }