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.kernel.cache.cluster;
016    
017    import com.liferay.portal.kernel.cache.CacheListener;
018    import com.liferay.portal.kernel.cache.CacheReplicator;
019    import com.liferay.portal.kernel.cache.PortalCache;
020    import com.liferay.portal.kernel.cache.PortalCacheException;
021    import com.liferay.portal.kernel.cache.PortalCacheManager;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    
024    import java.io.Serializable;
025    
026    import java.util.Properties;
027    
028    /**
029     * @author Tina Tian
030     */
031    public class ClusterLinkCacheReplicator
032            <K extends Serializable, V extends Serializable>
033                    implements CacheListener<K, V>, CacheReplicator {
034    
035            public ClusterLinkCacheReplicator(Properties properties) {
036                    _replicatePuts = GetterUtil.getBoolean(
037                            properties.getProperty(CacheReplicator.REPLICATE_PUTS),
038                            CacheReplicator.DEFAULT_REPLICATE_PUTS);
039                    _replicatePutsViaCopy = GetterUtil.getBoolean(
040                            properties.getProperty(CacheReplicator.REPLICATE_PUTS_VIA_COPY),
041                            CacheReplicator.DEFAULT_REPLICATE_PUTS_VIA_COPY);
042                    _replicateRemovals = GetterUtil.getBoolean(
043                            properties.getProperty(CacheReplicator.REPLICATE_REMOVALS),
044                            CacheReplicator.DEFAULT_REPLICATE_REMOVALS);
045                    _replicateUpdates = GetterUtil.getBoolean(
046                            properties.getProperty(CacheReplicator.REPLICATE_UPDATES),
047                            CacheReplicator.DEFAULT_REPLICATE_UPDATES);
048                    _replicateUpdatesViaCopy = GetterUtil.getBoolean(
049                            properties.getProperty(CacheReplicator.REPLICATE_UPDATES_VIA_COPY),
050                            CacheReplicator.DEFAULT_REPLICATE_UPDATES_VIA_COPY);
051            }
052    
053            @Override
054            public void dispose() {
055            }
056    
057            @Override
058            public void notifyEntryEvicted(
059                    PortalCache<K, V> portalCache, K key, V value, int timeToLive) {
060            }
061    
062            @Override
063            public void notifyEntryExpired(
064                    PortalCache<K, V> portalCache, K key, V value, int timeToLive) {
065            }
066    
067            @Override
068            public void notifyEntryPut(
069                            PortalCache<K, V> portalCache, K key, V value, int timeToLive)
070                    throws PortalCacheException {
071    
072                    if (!_replicatePuts) {
073                            return;
074                    }
075    
076                    PortalCacheManager<K, V> portalCacheManager =
077                            portalCache.getPortalCacheManager();
078    
079                    PortalCacheClusterEvent portalCacheClusterEvent =
080                            new PortalCacheClusterEvent(
081                                    portalCacheManager.getName(), portalCache.getName(), key,
082                                    PortalCacheClusterEventType.PUT);
083    
084                    if (_replicatePutsViaCopy) {
085                            portalCacheClusterEvent.setElementValue(value);
086                            portalCacheClusterEvent.setTimeToLive(timeToLive);
087                    }
088    
089                    PortalCacheClusterLinkUtil.sendEvent(portalCacheClusterEvent);
090            }
091    
092            @Override
093            public void notifyEntryRemoved(
094                            PortalCache<K, V> portalCache, K key, V value, int timeToLive)
095                    throws PortalCacheException {
096    
097                    if (!_replicateRemovals) {
098                            return;
099                    }
100    
101                    PortalCacheManager<K, V> portalCacheManager =
102                            portalCache.getPortalCacheManager();
103    
104                    PortalCacheClusterEvent portalCacheClusterEvent =
105                            new PortalCacheClusterEvent(
106                                    portalCacheManager.getName(), portalCache.getName(), key,
107                                    PortalCacheClusterEventType.REMOVE);
108    
109                    PortalCacheClusterLinkUtil.sendEvent(portalCacheClusterEvent);
110            }
111    
112            @Override
113            public void notifyEntryUpdated(
114                            PortalCache<K, V> portalCache, K key, V value, int timeToLive)
115                    throws PortalCacheException {
116    
117                    if (!_replicateUpdates) {
118                            return;
119                    }
120    
121                    PortalCacheManager<K, V> portalCacheManager =
122                            portalCache.getPortalCacheManager();
123    
124                    PortalCacheClusterEvent portalCacheClusterEvent =
125                            new PortalCacheClusterEvent(
126                                    portalCacheManager.getName(), portalCache.getName(), key,
127                                    PortalCacheClusterEventType.UPDATE);
128    
129                    if (_replicateUpdatesViaCopy) {
130                            portalCacheClusterEvent.setElementValue(value);
131                            portalCacheClusterEvent.setTimeToLive(timeToLive);
132                    }
133    
134                    PortalCacheClusterLinkUtil.sendEvent(portalCacheClusterEvent);
135            }
136    
137            @Override
138            public void notifyRemoveAll(PortalCache<K, V> portalCache)
139                    throws PortalCacheException {
140    
141                    if (!_replicateRemovals) {
142                            return;
143                    }
144    
145                    PortalCacheManager<K, V> portalCacheManager =
146                            portalCache.getPortalCacheManager();
147    
148                    PortalCacheClusterEvent portalCacheClusterEvent =
149                            new PortalCacheClusterEvent(
150                                    portalCacheManager.getName(), portalCache.getName(), null,
151                                    PortalCacheClusterEventType.REMOVE_ALL);
152    
153                    PortalCacheClusterLinkUtil.sendEvent(portalCacheClusterEvent);
154            }
155    
156            private final boolean _replicatePuts;
157            private final boolean _replicatePutsViaCopy;
158            private final boolean _replicateRemovals;
159            private final boolean _replicateUpdates;
160            private final boolean _replicateUpdatesViaCopy;
161    
162    }