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