1
22
23 package com.liferay.portal.security.permission;
24
25 import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
26 import com.liferay.portal.kernel.cache.PortalCache;
27 import com.liferay.portal.kernel.util.InitialThreadLocal;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.util.PropsValues;
30
31 import java.util.Map;
32
33 import org.apache.commons.collections.map.LRUMap;
34
35
41 public class PermissionCacheUtil {
42
43 public static final String CACHE_NAME = PermissionCacheUtil.class.getName();
44
45 public static void clearCache() {
46 clearLocalCache();
47
48 _cache.removeAll();
49 }
50
51 public static void clearLocalCache() {
52 if (_localCacheEnabled.get().booleanValue()) {
53 Map<String, Object> localCache = _localCache.get();
54
55 localCache.clear();
56 }
57 }
58
59 public static PermissionCheckerBag getBag(long userId, long groupId) {
60 PermissionCheckerBag bag = null;
61
62 String key = _encodeKey(userId, groupId);
63
64 if (_localCacheEnabled.get().booleanValue()) {
65 Map<String, Object> localCache = _localCache.get();
66
67 bag = (PermissionCheckerBag)localCache.get(key);
68 }
69
70 if (bag == null) {
71 bag = (PermissionCheckerBag)MultiVMPoolUtil.get(_cache, 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 (_localCacheEnabled.get().booleanValue()) {
86 Map<String, Object> localCache = _localCache.get();
87
88 value = (Boolean)localCache.get(key);
89 }
90
91 if (value == null) {
92 value = (Boolean)MultiVMPoolUtil.get(_cache, 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 (_localCacheEnabled.get().booleanValue()) {
105 Map<String, Object> localCache = _localCache.get();
106
107 localCache.put(key, bag);
108 }
109
110 MultiVMPoolUtil.put(_cache, 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 (_localCacheEnabled.get().booleanValue()) {
124 Map<String, Object> localCache = _localCache.get();
125
126 localCache.put(key, value);
127 }
128
129 MultiVMPoolUtil.put(_cache, key, value);
130 }
131
132 return value;
133 }
134
135 public static void setLocalCacheEnabled(boolean localCacheEnabled) {
136 if (_localCacheAvailable) {
137 _localCacheEnabled.set(Boolean.valueOf(localCacheEnabled));
138 }
139 }
140
141 private static String _encodeKey(long userId, long groupId) {
142 StringBuilder sb = new StringBuilder();
143
144 sb.append(CACHE_NAME);
145 sb.append(StringPool.POUND);
146 sb.append(userId);
147 sb.append(StringPool.POUND);
148 sb.append(groupId);
149
150 return sb.toString();
151 }
152
153 private static String _encodeKey(
154 long userId, long groupId, String name, String primKey,
155 String actionId) {
156
157 StringBuilder sb = new StringBuilder();
158
159 sb.append(CACHE_NAME);
160 sb.append(StringPool.POUND);
161 sb.append(userId);
162 sb.append(StringPool.POUND);
163 sb.append(groupId);
164 sb.append(StringPool.POUND);
165 sb.append(name);
166 sb.append(StringPool.POUND);
167 sb.append(primKey);
168 sb.append(StringPool.POUND);
169 sb.append(actionId);
170
171 return sb.toString();
172 }
173
174 private static PortalCache _cache = MultiVMPoolUtil.getCache(
175 CACHE_NAME, PropsValues.PERMISSIONS_OBJECT_BLOCKING_CACHE);
176
177 private static ThreadLocal<Map> _localCache;
178 private static boolean _localCacheAvailable;
179 private static ThreadLocal<Boolean> _localCacheEnabled =
180 new InitialThreadLocal<Boolean>(Boolean.FALSE);
181
182 static {
183 if (PropsValues.PERMISSIONS_THREAD_LOCAL_CACHE_MAX_SIZE > 0) {
184 _localCache = new InitialThreadLocal<Map>(new LRUMap(
185 PropsValues.PERMISSIONS_THREAD_LOCAL_CACHE_MAX_SIZE));
186 _localCacheAvailable = true;
187 }
188 }
189
190 }