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.kernel.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.io.Serializer;
020    import com.liferay.portal.kernel.nio.intraband.Datagram;
021    import com.liferay.portal.kernel.nio.intraband.Intraband;
022    import com.liferay.portal.kernel.nio.intraband.RegistrationReference;
023    import com.liferay.portal.kernel.nio.intraband.SystemDataType;
024    
025    import java.io.Serializable;
026    
027    import java.net.URL;
028    
029    import java.util.Map;
030    import java.util.concurrent.ConcurrentHashMap;
031    
032    /**
033     * @author Shuyang Zhou
034     */
035    public class IntrabandPortalCacheManager
036                    <K extends Serializable, V extends Serializable>
037            implements PortalCacheManager<K, V> {
038    
039            public static <K extends Serializable, V extends Serializable>
040                    PortalCacheManager<K, V> getPortalCacheManager() {
041    
042                    return (PortalCacheManager<K, V>)_portalCacheManager;
043            }
044    
045            public static void setPortalCacheManager(
046                    PortalCacheManager<? extends Serializable, ? extends Serializable>
047                    portalCacheManager) {
048    
049                    _portalCacheManager = portalCacheManager;
050            }
051    
052            public IntrabandPortalCacheManager(
053                    RegistrationReference registrationReference) {
054    
055                    _intraband = registrationReference.getIntraband();
056                    _registrationReference = registrationReference;
057            }
058    
059            @Override
060            public void clearAll() {
061                    _portalCaches.clear();
062            }
063    
064            @Override
065            public PortalCache<K, V> getCache(String name) {
066                    return getCache(name, false);
067            }
068    
069            @Override
070            public PortalCache<K, V> getCache(String name, boolean blocking) {
071                    PortalCache<K, V> portalCache = _portalCaches.get(name);
072    
073                    if (portalCache == null) {
074                            portalCache = new IntrabandPortalCache<K, V>(
075                                    name, _registrationReference);
076    
077                            _portalCaches.put(name, portalCache);
078                    }
079    
080                    return portalCache;
081            }
082    
083            @Override
084            public void reconfigureCaches(URL configurationURL) {
085                    Serializer serializer = new Serializer();
086    
087                    serializer.writeInt(PortalCacheActionType.RECONFIGURE.ordinal());
088                    serializer.writeString(configurationURL.toExternalForm());
089    
090                    SystemDataType systemDataType = SystemDataType.PORTAL_CACHE;
091    
092                    _intraband.sendDatagram(
093                            _registrationReference,
094                            Datagram.createRequestDatagram(
095                                    systemDataType.getValue(), serializer.toByteBuffer()));
096            }
097    
098            @Override
099            public void removeCache(String name) {
100                    _portalCaches.remove(name);
101            }
102    
103            private static PortalCacheManager
104                    <? extends Serializable, ? extends Serializable> _portalCacheManager;
105    
106            private final Intraband _intraband;
107            private final Map<String, PortalCache<K, V>> _portalCaches =
108                    new ConcurrentHashMap<String, PortalCache<K, V>>();
109            private final RegistrationReference _registrationReference;
110    
111    }