1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.security.permission;
21  
22  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
23  import com.liferay.portal.kernel.cache.PortalCache;
24  import com.liferay.portal.kernel.util.StringPool;
25  
26  /**
27   * <a href="PermissionCacheUtil.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Charles May
30   * @author Michael Young
31   *
32   */
33  public class PermissionCacheUtil {
34  
35      public static final String CACHE_NAME = PermissionCacheUtil.class.getName();
36  
37      public static void clearCache() {
38          _cache.removeAll();
39      }
40  
41      public static PermissionCheckerBag getBag(long userId, long groupId) {
42          String key = _encodeKey(userId, groupId);
43  
44          return (PermissionCheckerBag)MultiVMPoolUtil.get(_cache, key);
45      }
46  
47      public static Boolean getPermission(
48          long userId, long groupId, String name, String primKey,
49          String actionId) {
50  
51          String key = _encodeKey(userId, groupId, name, primKey, actionId);
52  
53          return (Boolean)MultiVMPoolUtil.get(_cache, key);
54      }
55  
56      public static PermissionCheckerBag putBag(
57          long userId, long groupId, PermissionCheckerBag bag) {
58  
59          if (bag != null) {
60              String key = _encodeKey(userId, groupId);
61  
62              MultiVMPoolUtil.put(_cache, key, bag);
63          }
64  
65          return bag;
66      }
67  
68      public static Boolean putPermission(
69          long userId, long groupId, String name, String primKey, String actionId,
70          Boolean value) {
71  
72          if (value != null) {
73              String key = _encodeKey(userId, groupId, name, primKey, actionId);
74  
75              MultiVMPoolUtil.put(_cache, key, value);
76          }
77  
78          return value;
79      }
80  
81      private static String _encodeKey(long userId, long groupId) {
82          StringBuilder sb = new StringBuilder();
83  
84          sb.append(CACHE_NAME);
85          sb.append(StringPool.POUND);
86          sb.append(userId);
87          sb.append(StringPool.POUND);
88          sb.append(groupId);
89  
90          return sb.toString();
91      }
92  
93      private static String _encodeKey(
94          long userId, long groupId, String name, String primKey,
95          String actionId) {
96  
97          StringBuilder sb = new StringBuilder();
98  
99          sb.append(CACHE_NAME);
100         sb.append(StringPool.POUND);
101         sb.append(userId);
102         sb.append(StringPool.POUND);
103         sb.append(groupId);
104         sb.append(StringPool.POUND);
105         sb.append(name);
106         sb.append(StringPool.POUND);
107         sb.append(primKey);
108         sb.append(StringPool.POUND);
109         sb.append(actionId);
110 
111         return sb.toString();
112     }
113 
114     private static PortalCache _cache = MultiVMPoolUtil.getCache(CACHE_NAME);
115 
116 }