001    /**
002     * Copyright (c) 2000-present 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.test;
016    
017    import com.liferay.portal.kernel.cache.AbstractPortalCacheManager;
018    import com.liferay.portal.kernel.cache.PortalCache;
019    import com.liferay.portal.kernel.cache.configuration.PortalCacheConfiguration;
020    import com.liferay.portal.kernel.cache.configuration.PortalCacheManagerConfiguration;
021    
022    import java.io.Serializable;
023    
024    import java.net.URL;
025    
026    import java.util.concurrent.ConcurrentHashMap;
027    import java.util.concurrent.ConcurrentMap;
028    
029    /**
030     * @author Tina Tian
031     */
032    public class TestPortalCacheManager<K extends Serializable, V>
033            extends AbstractPortalCacheManager<K, V> {
034    
035            public static <K extends Serializable, V> TestPortalCacheManager<K, V>
036                    createTestPortalCacheManager(String portalCacheManagerName) {
037    
038                    TestPortalCacheManager<K, V> testPortalCacheManager =
039                            new TestPortalCacheManager<>();
040    
041                    testPortalCacheManager.setPortalCacheManagerName(
042                            portalCacheManagerName);
043    
044                    testPortalCacheManager.initialize();
045    
046                    return testPortalCacheManager;
047            }
048    
049            @Override
050            public void reconfigurePortalCaches(URL configurationURL) {
051                    throw new UnsupportedOperationException();
052            }
053    
054            @Override
055            protected PortalCache<K, V> createPortalCache(
056                    PortalCacheConfiguration portalCacheConfiguration) {
057    
058                    String portalCacheName = portalCacheConfiguration.getPortalCacheName();
059    
060                    TestPortalCache<K, V> portalCache = _testPortalCaches.get(
061                            portalCacheName);
062    
063                    if (portalCache != null) {
064                            return portalCache;
065                    }
066    
067                    portalCache = new TestPortalCache<>(this, portalCacheName);
068    
069                    TestPortalCache<K, V> previousPortalCache =
070                            _testPortalCaches.putIfAbsent(portalCacheName, portalCache);
071    
072                    if (previousPortalCache == null) {
073                            aggregatedPortalCacheManagerListener.notifyPortalCacheAdded(
074                                    portalCacheName);
075                    }
076                    else {
077                            portalCache = previousPortalCache;
078                    }
079    
080                    return portalCache;
081            }
082    
083            @Override
084            protected void doClearAll() {
085                    for (TestPortalCache<K, V> testPortalCache :
086                                    _testPortalCaches.values()) {
087    
088                            testPortalCache.removeAll();
089                    }
090            }
091    
092            @Override
093            protected void doDestroy() {
094                    for (TestPortalCache<K, V> testPortalCache :
095                                    _testPortalCaches.values()) {
096    
097                            testPortalCache.removeAll();
098                    }
099    
100                    aggregatedPortalCacheManagerListener.dispose();
101            }
102    
103            @Override
104            protected void doRemovePortalCache(String portalCacheName) {
105                    TestPortalCache<K, V> testPortalCache = _testPortalCaches.remove(
106                            portalCacheName);
107    
108                    testPortalCache.removeAll();
109    
110                    aggregatedPortalCacheManagerListener.notifyPortalCacheRemoved(
111                            portalCacheName);
112            }
113    
114            @Override
115            protected PortalCacheManagerConfiguration
116                    getPortalCacheManagerConfiguration() {
117    
118                    return new PortalCacheManagerConfiguration(
119                            null,
120                            new PortalCacheConfiguration(
121                                    PortalCacheConfiguration.DEFAULT_PORTAL_CACHE_NAME, null, null),
122                            null);
123            }
124    
125            @Override
126            protected void initPortalCacheManager() {
127                    _testPortalCaches = new ConcurrentHashMap<>();
128    
129                    aggregatedPortalCacheManagerListener.init();
130            }
131    
132            private ConcurrentMap<String, TestPortalCache<K, V>> _testPortalCaches;
133    
134    }