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            public void clearAll() {
060                    _portalCaches.clear();
061            }
062    
063            public PortalCache<K, V> getCache(String name) {
064                    return getCache(name, false);
065            }
066    
067            public PortalCache<K, V> getCache(String name, boolean blocking) {
068                    PortalCache<K, V> portalCache = _portalCaches.get(name);
069    
070                    if (portalCache == null) {
071                            portalCache = new IntraBandPortalCache<K, V>(
072                                    name, _registrationReference);
073    
074                            _portalCaches.put(name, portalCache);
075                    }
076    
077                    return portalCache;
078            }
079    
080            public void reconfigureCaches(URL configurationURL) {
081                    Serializer serializer = new Serializer();
082    
083                    serializer.writeInt(PortalCacheActionType.RECONFIGURE.ordinal());
084                    serializer.writeString(configurationURL.toExternalForm());
085    
086                    SystemDataType systemDataType = SystemDataType.PORTAL_CACHE;
087    
088                    _intraBand.sendDatagram(
089                            _registrationReference,
090                            Datagram.createRequestDatagram(
091                                    systemDataType.getValue(), serializer.toByteBuffer()));
092            }
093    
094            public void removeCache(String name) {
095                    _portalCaches.remove(name);
096            }
097    
098            private static PortalCacheManager
099                    <? extends Serializable, ? extends Serializable> _portalCacheManager;
100    
101            private final IntraBand _intraBand;
102            private final Map<String, PortalCache<K, V>> _portalCaches =
103                    new ConcurrentHashMap<String, PortalCache<K, V>>();
104            private final RegistrationReference _registrationReference;
105    
106    }