1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
18  import com.liferay.portal.kernel.cache.PortalCache;
19  import com.liferay.portal.kernel.util.StringBundler;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portlet.PortletPreferencesImpl;
23  
24  import java.util.Map;
25  import java.util.concurrent.ConcurrentHashMap;
26  
27  /**
28   * <a href="PortletPreferencesLocalUtil.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   * @author Michael Young
32   */
33  public class PortletPreferencesLocalUtil {
34  
35      public static final String CACHE_NAME =
36          PortletPreferencesLocalUtil.class.getName();
37  
38      protected static void clearPreferencesPool() {
39          _portalCache.removeAll();
40      }
41  
42      protected static void clearPreferencesPool(long ownerId, int ownerType) {
43          String key = _encodeKey(ownerId, ownerType);
44  
45          _portalCache.remove(key);
46      }
47  
48      protected static Map<String, PortletPreferencesImpl> getPreferencesPool(
49              long ownerId, int ownerType) {
50          String key = _encodeKey(ownerId, ownerType);
51  
52          Map<String, PortletPreferencesImpl> preferencesPool =
53              (Map<String, PortletPreferencesImpl>)_portalCache.get(key);
54  
55          if (preferencesPool == null) {
56              preferencesPool =
57                  new ConcurrentHashMap<String, PortletPreferencesImpl>();
58  
59              _portalCache.put(key, preferencesPool);
60          }
61  
62          return preferencesPool;
63      }
64  
65      private static String _encodeKey(long ownerId, int ownerType) {
66          StringBundler sb = new StringBundler(5);
67  
68          sb.append(CACHE_NAME);
69          sb.append(StringPool.POUND);
70          sb.append(StringUtil.toHexString(ownerId));
71          sb.append(StringPool.POUND);
72          sb.append(StringUtil.toHexString(ownerType));
73  
74          return sb.toString();
75      }
76  
77      private static PortalCache _portalCache = MultiVMPoolUtil.getCache(
78          CACHE_NAME);
79  
80  }