001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.cache.keypool;
016    
017    import com.liferay.portal.kernel.cache.CacheListenerScope;
018    import com.liferay.portal.kernel.cache.MultiVMPool;
019    import com.liferay.portal.kernel.cache.PortalCache;
020    import com.liferay.portal.kernel.cache.PortalCacheManager;
021    import com.liferay.portal.kernel.cache.SingleVMPool;
022    
023    import java.net.URL;
024    
025    import java.util.Map;
026    import java.util.concurrent.ConcurrentHashMap;
027    
028    /**
029     * @author Edward Han
030     * @author Brian Wing Shun Chan
031     */
032    public class MultiVMKeyPoolPortalCacheManager implements PortalCacheManager {
033    
034            public void clearAll() {
035                    for (MultiVMKeyPoolPortalCache multiVMKeyPoolPortalCache :
036                                    _multiVMKeyPoolPortalCaches.values()) {
037    
038                            multiVMKeyPoolPortalCache.removeAll();
039                    }
040            }
041    
042            public PortalCache getCache(String name) {
043                    return getCache(name, false);
044            }
045    
046            public PortalCache getCache(String name, boolean blocking) {
047                    MultiVMKeyPoolPortalCache multiVMKeyPoolPortalCache =
048                            _multiVMKeyPoolPortalCaches.get(name);
049    
050                    if (multiVMKeyPoolPortalCache != null) {
051                            return multiVMKeyPoolPortalCache;
052                    }
053    
054                    synchronized (_multiVMKeyPoolPortalCaches) {
055                            PortalCache clusterPortalCache = _multiVMPool.getCache(
056                                    name, blocking);
057                            PortalCache localPortalCache = _singleVMPool.getCache(
058                                    name, blocking);
059    
060                            multiVMKeyPoolPortalCache = new MultiVMKeyPoolPortalCache(
061                                    clusterPortalCache, localPortalCache);
062    
063                            multiVMKeyPoolPortalCache.registerCacheListener(
064                                    new MultiVMKeyPoolCacheListener(localPortalCache),
065                                    CacheListenerScope.REMOTE);
066    
067                            _multiVMKeyPoolPortalCaches.put(name, multiVMKeyPoolPortalCache);
068                    }
069    
070                    return multiVMKeyPoolPortalCache;
071            }
072    
073            public void reconfigureCaches(URL configurationURL) {
074            }
075    
076            public void removeCache(String name) {
077                    synchronized (_multiVMKeyPoolPortalCaches) {
078                            MultiVMKeyPoolPortalCache multiVMKeyPoolPortalCache =
079                                    _multiVMKeyPoolPortalCaches.get(name);
080    
081                            if (multiVMKeyPoolPortalCache != null) {
082                                    _multiVMPool.removeCache(name);
083                                    _singleVMPool.removeCache(name);
084    
085                                    _multiVMKeyPoolPortalCaches.remove(name);
086                            }
087                    }
088            }
089    
090            public void setMultiVMPool(MultiVMPool multiVMPool) {
091                    _multiVMPool = multiVMPool;
092            }
093    
094            public void setSingleVMPool(SingleVMPool singleVMPool) {
095                    _singleVMPool = singleVMPool;
096            }
097    
098            private Map<String, MultiVMKeyPoolPortalCache> _multiVMKeyPoolPortalCaches =
099                    new ConcurrentHashMap<String, MultiVMKeyPoolPortalCache>();
100            private MultiVMPool _multiVMPool;
101            private SingleVMPool _singleVMPool;
102    
103    }