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