1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.StringBundler;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.util.PropsValues;
31  
32  import java.util.Map;
33  
34  import org.apache.commons.collections.map.LRUMap;
35  
36  /**
37   * <a href="PermissionCacheUtil.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Charles May
40   * @author Michael Young
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)_cache.get(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)_cache.get(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             _cache.put(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             _cache.put(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         StringBundler sb = new StringBundler(5);
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         StringBundler sb = new StringBundler(11);
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, PropsValues.PERMISSIONS_OBJECT_BLOCKING_CACHE);
177 
178     private static ThreadLocal<LRUMap> _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<LRUMap>(new LRUMap(
186                 PropsValues.PERMISSIONS_THREAD_LOCAL_CACHE_MAX_SIZE));
187             _localCacheAvailable = true;
188         }
189     }
190 
191 }