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