001    /**
002     * Copyright (c) 2000-2012 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.portlet.BasePreferencesImpl;
020    
021    import java.io.Serializable;
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     * @author Michael Young
029     * @author Shuyang Zhou
030     */
031    public class PortletPreferencesLocalUtil {
032    
033            public static Map<Serializable, BasePreferencesImpl> getPreferencesPool(
034                    long ownerId, int ownerType) {
035    
036                    Serializable key = new PreferencesPoolKey(ownerId, ownerType);
037    
038                    Map<Serializable, BasePreferencesImpl> preferencesPool =
039                            (Map<Serializable, BasePreferencesImpl>)_portalCache.get(key);
040    
041                    if (preferencesPool == null) {
042                            preferencesPool = new HashMap<Serializable, BasePreferencesImpl>();
043    
044                            _portalCache.put(key, preferencesPool);
045                    }
046    
047                    return preferencesPool;
048            }
049    
050            protected static void clearPreferencesPool() {
051                    _portalCache.removeAll();
052            }
053    
054            protected static void clearPreferencesPool(long ownerId, int ownerType) {
055                    Serializable key = new PreferencesPoolKey(ownerId, ownerType);
056    
057                    _portalCache.remove(key);
058            }
059    
060            private static final String _CACHE_NAME =
061                    PortletPreferencesLocalUtil.class.getName();
062    
063            private static PortalCache _portalCache = MultiVMPoolUtil.getCache(
064                    _CACHE_NAME);
065    
066            private static class PreferencesPoolKey implements Serializable {
067    
068                    public PreferencesPoolKey(long ownerId, int ownerType) {
069                            _ownerId = ownerId;
070                            _ownerType = ownerType;
071                    }
072    
073                    @Override
074                    public boolean equals(Object obj) {
075                            PreferencesPoolKey preferencesPoolKey = (PreferencesPoolKey)obj;
076    
077                            if ((preferencesPoolKey._ownerId == _ownerId) &&
078                                    (preferencesPoolKey._ownerType == _ownerType)) {
079    
080                                    return true;
081                            }
082                            else {
083                                    return false;
084                            }
085                    }
086    
087                    @Override
088                    public int hashCode() {
089                            return (int)(_ownerId * 11 + _ownerType);
090                    }
091    
092                    private static final long serialVersionUID = 1L;
093    
094                    private final long _ownerId;
095                    private final int _ownerType;
096    
097            }
098    
099    }