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.memory;
016    
017    import com.liferay.portal.cache.AbstractPortalCacheManager;
018    import com.liferay.portal.cache.cluster.ClusterLinkCallbackFactory;
019    import com.liferay.portal.kernel.cache.CacheListenerScope;
020    import com.liferay.portal.kernel.cache.PortalCache;
021    import com.liferay.portal.kernel.cache.configuration.CallbackConfiguration;
022    import com.liferay.portal.kernel.cache.configuration.PortalCacheConfiguration;
023    import com.liferay.portal.kernel.cache.configuration.PortalCacheManagerConfiguration;
024    import com.liferay.portal.util.PropsValues;
025    
026    import java.io.Serializable;
027    
028    import java.net.URL;
029    
030    import java.util.HashMap;
031    import java.util.Map;
032    import java.util.Properties;
033    import java.util.concurrent.ConcurrentHashMap;
034    import java.util.concurrent.ConcurrentMap;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Tina Tian
039     */
040    public class MemoryPortalCacheManager<K extends Serializable, V>
041            extends AbstractPortalCacheManager<K, V> {
042    
043            @Override
044            public String getName() {
045                    return _name;
046            }
047    
048            @Override
049            public void reconfigureCaches(URL configurationURL) {
050                    throw new UnsupportedOperationException();
051            }
052    
053            public void setCacheInitialCapacity(int cacheInitialCapacity) {
054                    _cacheInitialCapacity = cacheInitialCapacity;
055            }
056    
057            public void setCacheManagerInitialCapacity(
058                    int cacheManagerInitialCapacity) {
059    
060                    _cacheManagerInitialCapacity = cacheManagerInitialCapacity;
061            }
062    
063            public void setName(String name) {
064                    _name = name;
065            }
066    
067            @Override
068            protected PortalCache<K, V> createPortalCache(String cacheName) {
069                    MemoryPortalCache<K, V> portalCache = _memoryPortalCaches.get(
070                            cacheName);
071    
072                    if (portalCache != null) {
073                            return portalCache;
074                    }
075    
076                    portalCache = new MemoryPortalCache<K, V>(
077                            this, cacheName, _cacheInitialCapacity);
078    
079                    MemoryPortalCache<K, V> previousPortalCache =
080                            _memoryPortalCaches.putIfAbsent(cacheName, portalCache);
081    
082                    if (previousPortalCache == null) {
083                            aggregatedCacheManagerListener.notifyCacheAdded(cacheName);
084                    }
085                    else {
086                            portalCache = previousPortalCache;
087                    }
088    
089                    return portalCache;
090            }
091    
092            @Override
093            protected void doClearAll() {
094                    for (MemoryPortalCache<K, V> memoryPortalCache :
095                                    _memoryPortalCaches.values()) {
096    
097                            memoryPortalCache.removeAll();
098                    }
099            }
100    
101            @Override
102            protected void doDestroy() {
103                    for (MemoryPortalCache<K, V> memoryPortalCache :
104                                    _memoryPortalCaches.values()) {
105    
106                            memoryPortalCache.destroy();
107                    }
108    
109                    aggregatedCacheManagerListener.dispose();
110            }
111    
112            @Override
113            protected void doRemoveCache(String cacheName) {
114                    MemoryPortalCache<K, V> memoryPortalCache = _memoryPortalCaches.remove(
115                            cacheName);
116    
117                    memoryPortalCache.destroy();
118    
119                    aggregatedCacheManagerListener.notifyCacheRemoved(cacheName);
120            }
121    
122            @Override
123            protected PortalCacheManagerConfiguration
124                    getPortalCacheManagerConfiguration() {
125    
126                    PortalCacheConfiguration defaultPortalCacheConfiguration = null;
127    
128                    if (clusterAware && PropsValues.CLUSTER_LINK_ENABLED) {
129                            CallbackConfiguration cacheListenerConfiguration =
130                                    new CallbackConfiguration(
131                                            ClusterLinkCallbackFactory.INSTANCE, new Properties());
132    
133                            Map<CallbackConfiguration, CacheListenerScope>
134                                    cacheListenerConfigurations =
135                                            new HashMap<CallbackConfiguration, CacheListenerScope>();
136    
137                            cacheListenerConfigurations.put(
138                                    cacheListenerConfiguration, CacheListenerScope.ALL);
139    
140                            CallbackConfiguration bootstrapLoaderConfiguration =
141                                    new CallbackConfiguration(
142                                            ClusterLinkCallbackFactory.INSTANCE, new Properties());
143    
144                            defaultPortalCacheConfiguration =
145                                    new PortalCacheConfiguration(
146                                            PortalCacheConfiguration.DEFAULT_PORTAL_CACHE_NAME,
147                                            cacheListenerConfigurations, bootstrapLoaderConfiguration);
148                    }
149    
150                    return new PortalCacheManagerConfiguration(
151                            null, defaultPortalCacheConfiguration, null);
152            }
153    
154            @Override
155            protected void initPortalCacheManager() {
156                    if (_name == null) {
157                            throw new NullPointerException("Name is null");
158                    }
159    
160                    _memoryPortalCaches =
161                            new ConcurrentHashMap<String, MemoryPortalCache<K, V>>(
162                                    _cacheManagerInitialCapacity);
163    
164                    aggregatedCacheManagerListener.init();
165            }
166    
167            private int _cacheInitialCapacity = 10000;
168            private int _cacheManagerInitialCapacity = 10000;
169            private ConcurrentMap<String, MemoryPortalCache<K, V>> _memoryPortalCaches;
170            private String _name;
171    
172    }