001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
018    import com.liferay.portal.kernel.cache.PortalCache;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portlet.PortletPreferencesImpl;
022    
023    import java.util.Map;
024    import java.util.concurrent.ConcurrentHashMap;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     * @author Michael Young
029     */
030    public class PortletPreferencesLocalUtil {
031    
032            public static final String CACHE_NAME =
033                    PortletPreferencesLocalUtil.class.getName();
034    
035            protected static void clearPreferencesPool() {
036                    _portalCache.removeAll();
037            }
038    
039            protected static void clearPreferencesPool(long ownerId, int ownerType) {
040                    String key = _encodeKey(ownerId, ownerType);
041    
042                    _portalCache.remove(key);
043            }
044    
045            protected static Map<String, PortletPreferencesImpl> getPreferencesPool(
046                            long ownerId, int ownerType) {
047                    String key = _encodeKey(ownerId, ownerType);
048    
049                    Map<String, PortletPreferencesImpl> preferencesPool =
050                            (Map<String, PortletPreferencesImpl>)_portalCache.get(key);
051    
052                    if (preferencesPool == null) {
053                            preferencesPool =
054                                    new ConcurrentHashMap<String, PortletPreferencesImpl>();
055    
056                            _portalCache.put(key, preferencesPool);
057                    }
058    
059                    return preferencesPool;
060            }
061    
062            private static String _encodeKey(long ownerId, int ownerType) {
063                    StringBundler sb = new StringBundler(5);
064    
065                    sb.append(CACHE_NAME);
066                    sb.append(StringPool.POUND);
067                    sb.append(ownerId);
068                    sb.append(StringPool.POUND);
069                    sb.append(ownerType);
070    
071                    return sb.toString();
072            }
073    
074            private static PortalCache _portalCache = MultiVMPoolUtil.getCache(
075                    CACHE_NAME);
076    
077    }