001    /**
002     * Copyright (c) 2000-2013 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            @Override
035            public void clearAll() {
036                    for (MultiVMKeyPoolPortalCache multiVMKeyPoolPortalCache :
037                                    _multiVMKeyPoolPortalCaches.values()) {
038    
039                            multiVMKeyPoolPortalCache.removeAll();
040                    }
041            }
042    
043            @Override
044            public PortalCache getCache(String name) {
045                    return getCache(name, false);
046            }
047    
048            @Override
049            public PortalCache getCache(String name, boolean blocking) {
050                    MultiVMKeyPoolPortalCache multiVMKeyPoolPortalCache =
051                            _multiVMKeyPoolPortalCaches.get(name);
052    
053                    if (multiVMKeyPoolPortalCache != null) {
054                            return multiVMKeyPoolPortalCache;
055                    }
056    
057                    synchronized (_multiVMKeyPoolPortalCaches) {
058                            PortalCache clusterPortalCache = _multiVMPool.getCache(
059                                    name, blocking);
060                            PortalCache localPortalCache = _singleVMPool.getCache(
061                                    name, blocking);
062    
063                            multiVMKeyPoolPortalCache = new MultiVMKeyPoolPortalCache(
064                                    clusterPortalCache, localPortalCache);
065    
066                            multiVMKeyPoolPortalCache.registerCacheListener(
067                                    new MultiVMKeyPoolCacheListener(localPortalCache),
068                                    CacheListenerScope.REMOTE);
069    
070                            _multiVMKeyPoolPortalCaches.put(name, multiVMKeyPoolPortalCache);
071                    }
072    
073                    return multiVMKeyPoolPortalCache;
074            }
075    
076            @Override
077            public void reconfigureCaches(URL configurationURL) {
078            }
079    
080            @Override
081            public void removeCache(String name) {
082                    synchronized (_multiVMKeyPoolPortalCaches) {
083                            MultiVMKeyPoolPortalCache multiVMKeyPoolPortalCache =
084                                    _multiVMKeyPoolPortalCaches.get(name);
085    
086                            if (multiVMKeyPoolPortalCache != null) {
087                                    _multiVMPool.removeCache(name);
088                                    _singleVMPool.removeCache(name);
089    
090                                    _multiVMKeyPoolPortalCaches.remove(name);
091                            }
092                    }
093            }
094    
095            public void setMultiVMPool(MultiVMPool multiVMPool) {
096                    _multiVMPool = multiVMPool;
097            }
098    
099            public void setSingleVMPool(SingleVMPool singleVMPool) {
100                    _singleVMPool = singleVMPool;
101            }
102    
103            private Map<String, MultiVMKeyPoolPortalCache> _multiVMKeyPoolPortalCaches =
104                    new ConcurrentHashMap<String, MultiVMKeyPoolPortalCache>();
105            private MultiVMPool _multiVMPool;
106            private SingleVMPool _singleVMPool;
107    
108    }