1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.portlet.PortletPreferencesImpl;
22  
23  import java.util.Map;
24  import java.util.concurrent.ConcurrentHashMap;
25  
26  /**
27   * <a href="PortletPreferencesLocalUtil.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   * @author Michael Young
31   */
32  public class PortletPreferencesLocalUtil {
33  
34      public static final String CACHE_NAME =
35          PortletPreferencesLocalUtil.class.getName();
36  
37      protected static void clearPreferencesPool() {
38          _cache.removeAll();
39      }
40  
41      protected static void clearPreferencesPool(long ownerId, int ownerType) {
42          String key = _encodeKey(ownerId, ownerType);
43  
44          _cache.remove(key);
45      }
46  
47      protected static Map<String, PortletPreferencesImpl> getPreferencesPool(
48              long ownerId, int ownerType) {
49          String key = _encodeKey(ownerId, ownerType);
50  
51          Map<String, PortletPreferencesImpl> preferencesPool =
52              (Map<String, PortletPreferencesImpl>)_cache.get(key);
53  
54          if (preferencesPool == null) {
55              preferencesPool =
56                  new ConcurrentHashMap<String, PortletPreferencesImpl>();
57  
58              _cache.put(key, preferencesPool);
59          }
60  
61          return preferencesPool;
62      }
63  
64      private static String _encodeKey(long ownerId, int ownerType) {
65          StringBundler sb = new StringBundler(5);
66  
67          sb.append(CACHE_NAME);
68          sb.append(StringPool.POUND);
69          sb.append(ownerId);
70          sb.append(StringPool.POUND);
71          sb.append(ownerType);
72  
73          return sb.toString();
74      }
75  
76      private static PortalCache _cache = MultiVMPoolUtil.getCache(CACHE_NAME);
77  
78  }