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.nio.intraband.cache;
016    
017    import com.liferay.portal.kernel.cache.PortalCache;
018    import com.liferay.portal.kernel.cache.PortalCacheManager;
019    import com.liferay.portal.kernel.nio.intraband.RegistrationReference;
020    import com.liferay.portal.nio.intraband.proxy.IntrabandProxyUtil;
021    import com.liferay.portal.nio.intraband.proxy.WarnLogExceptionHandler;
022    
023    import java.io.Serializable;
024    
025    import java.util.Map;
026    import java.util.concurrent.ConcurrentHashMap;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public abstract class BaseIntrabandPortalCacheManager
032                    <K extends Serializable, V extends Serializable>
033            implements PortalCacheManager<K, V> {
034    
035            public static Class<? extends PortalCache<?, ?>> getPortalCacheStubClass() {
036                    return _STUB_CLASS;
037            }
038    
039            @Override
040            public void destroy() {
041                    _portalCaches.clear();
042            }
043    
044            @Override
045            public PortalCache<K, V> getCache(String name) {
046                    return getCache(name, false);
047            }
048    
049            @Override
050            public PortalCache<K, V> getCache(String name, boolean blocking) {
051                    PortalCache<K, V> portalCache = _portalCaches.get(name);
052    
053                    if (portalCache == null) {
054                            portalCache = (PortalCache<K, V>)IntrabandProxyUtil.newStubInstance(
055                                    _STUB_CLASS, name, _registrationReference,
056                                    WarnLogExceptionHandler.INSTANCE);
057    
058                            _portalCaches.put(name, portalCache);
059                    }
060    
061                    return portalCache;
062            }
063    
064            @Override
065            public void removeCache(String name) {
066                    _portalCaches.remove(name);
067            }
068    
069            private static final Class<? extends PortalCache<?, ?>> _STUB_CLASS =
070                    (Class<? extends PortalCache<?, ?>>)
071                            IntrabandProxyUtil.getStubClass(
072                                    PortalCache.class, PortalCache.class.getName());
073    
074            private final Map<String, PortalCache<K, V>> _portalCaches =
075                    new ConcurrentHashMap<String, PortalCache<K, V>>();
076            private final RegistrationReference _registrationReference = null;
077    
078    }