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.cache.cluster;
016    
017    import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterEvent;
018    import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterEventType;
019    import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterLinkUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    
022    import java.util.Properties;
023    
024    import net.sf.ehcache.CacheException;
025    import net.sf.ehcache.Ehcache;
026    import net.sf.ehcache.Element;
027    import net.sf.ehcache.distribution.CacheReplicator;
028    
029    /**
030     * @author Shuyang Zhou
031     */
032    public class EhcachePortalCacheClusterReplicator implements CacheReplicator {
033    
034            public EhcachePortalCacheClusterReplicator(Properties properties) {
035                    if (properties != null) {
036                            _replicatePuts = GetterUtil.getBoolean(
037                                    properties.getProperty(_REPLICATE_PUTS));
038                            _replicatePutsViaCopy = GetterUtil.getBoolean(
039                                    properties.getProperty(_REPLICATE_PUTS_VIA_COPY));
040                            _replicateRemovals = GetterUtil.getBoolean(
041                                    properties.getProperty(_REPLICATE_REMOVALS), true);
042                            _replicateUpdates = GetterUtil.getBoolean(
043                                    properties.getProperty(_REPLICATE_UPDATES), true);
044                            _replicateUpdatesViaCopy = GetterUtil.getBoolean(
045                                    properties.getProperty(_REPLICATE_UPDATES_VIA_COPY));
046                    }
047            }
048    
049            @Override
050            public boolean alive() {
051                    return true;
052            }
053    
054            @Override
055            public Object clone() throws CloneNotSupportedException {
056                    return super.clone();
057            }
058    
059            @Override
060            public void dispose() {
061            }
062    
063            @Override
064            public boolean isReplicateUpdatesViaCopy() {
065                    return false;
066            }
067    
068            @Override
069            public boolean notAlive() {
070                    return false;
071            }
072    
073            @Override
074            public void notifyElementEvicted(Ehcache ehcache, Element element) {
075            }
076    
077            @Override
078            public void notifyElementExpired(Ehcache ehcache, Element element) {
079            }
080    
081            @Override
082            public void notifyElementPut(Ehcache ehcache, Element element)
083                    throws CacheException {
084    
085                    if (!_replicatePuts) {
086                            return;
087                    }
088    
089                    PortalCacheClusterEvent portalCacheClusterEvent =
090                            new PortalCacheClusterEvent(
091                                    ehcache.getName(), element.getKey(),
092                                    PortalCacheClusterEventType.PUT);
093    
094                    if (_replicatePutsViaCopy) {
095                            portalCacheClusterEvent.setElementValue(element.getValue());
096                    }
097    
098                    PortalCacheClusterLinkUtil.sendEvent(portalCacheClusterEvent);
099            }
100    
101            @Override
102            public void notifyElementRemoved(Ehcache ehcache, Element element)
103                    throws CacheException {
104    
105                    if (!_replicateRemovals) {
106                            return;
107                    }
108    
109                    PortalCacheClusterEvent portalCacheClusterEvent =
110                            new PortalCacheClusterEvent(
111                                    ehcache.getName(), element.getKey(),
112                                    PortalCacheClusterEventType.REMOVE);
113    
114                    PortalCacheClusterLinkUtil.sendEvent(portalCacheClusterEvent);
115            }
116    
117            @Override
118            public void notifyElementUpdated(Ehcache ehcache, Element element)
119                    throws CacheException {
120    
121                    if (!_replicateUpdates) {
122                            return;
123                    }
124    
125                    PortalCacheClusterEvent portalCacheClusterEvent =
126                            new PortalCacheClusterEvent(
127                                    ehcache.getName(), element.getKey(),
128                                    PortalCacheClusterEventType.UPDATE);
129    
130                    if (_replicateUpdatesViaCopy) {
131                            portalCacheClusterEvent.setElementValue(element.getValue());
132                    }
133    
134                    PortalCacheClusterLinkUtil.sendEvent(portalCacheClusterEvent);
135            }
136    
137            @Override
138            public void notifyRemoveAll(Ehcache ehcache) {
139                    if (!_replicateRemovals) {
140                            return;
141                    }
142    
143                    PortalCacheClusterEvent portalCacheClusterEvent =
144                            new PortalCacheClusterEvent(
145                                    ehcache.getName(), null,
146                                    PortalCacheClusterEventType.REMOVE_ALL);
147    
148                    PortalCacheClusterLinkUtil.sendEvent(portalCacheClusterEvent);
149            }
150    
151            private static final String _REPLICATE_PUTS = "replicatePuts";
152    
153            private static final String _REPLICATE_PUTS_VIA_COPY =
154                    "replicatePutsViaCopy";
155    
156            private static final String _REPLICATE_REMOVALS = "replicateRemovals";
157    
158            private static final String _REPLICATE_UPDATES = "replicateUpdates";
159    
160            private static final String _REPLICATE_UPDATES_VIA_COPY =
161                    "replicateUpdatesViaCopy";
162    
163            private boolean _replicatePuts;
164            private boolean _replicatePutsViaCopy;
165            private boolean _replicateRemovals = true;
166            private boolean _replicateUpdates = true;
167            private boolean _replicateUpdatesViaCopy;
168    
169    }