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