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.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    }