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> getPortalCache(String portalCacheName) {
046                    return getPortalCache(portalCacheName, false);
047            }
048    
049            @Override
050            public PortalCache<K, V> getPortalCache(
051                    String portalCacheName, boolean blocking) {
052    
053                    PortalCache<K, V> portalCache = _portalCaches.get(portalCacheName);
054    
055                    if (portalCache == null) {
056                            portalCache = (PortalCache<K, V>)IntrabandProxyUtil.newStubInstance(
057                                    _STUB_CLASS, portalCacheName, _registrationReference,
058                                    WarnLogExceptionHandler.INSTANCE);
059    
060                            _portalCaches.put(portalCacheName, portalCache);
061                    }
062    
063                    return portalCache;
064            }
065    
066            @Override
067            public void removePortalCache(String portalCacheName) {
068                    _portalCaches.remove(portalCacheName);
069            }
070    
071            private static final Class<? extends PortalCache<?, ?>> _STUB_CLASS =
072                    (Class<? extends PortalCache<?, ?>>)
073                            IntrabandProxyUtil.getStubClass(
074                                    PortalCache.class, PortalCache.class.getName());
075    
076            private final Map<String, PortalCache<K, V>> _portalCaches =
077                    new ConcurrentHashMap<>();
078            private final RegistrationReference _registrationReference = null;
079    
080    }