001    /**
002     * Copyright (c) 2000-2012 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            public boolean alive() {
050                    return true;
051            }
052    
053            @Override
054            public Object clone() throws CloneNotSupportedException {
055                    return super.clone();
056            }
057    
058            public void dispose() {
059            }
060    
061            public boolean isReplicateUpdatesViaCopy() {
062                    return false;
063            }
064    
065            public boolean notAlive() {
066                    return false;
067            }
068    
069            public void notifyElementEvicted(Ehcache ehcache, Element element) {
070            }
071    
072            public void notifyElementExpired(Ehcache ehcache, Element element) {
073            }
074    
075            public void notifyElementPut(Ehcache ehcache, Element element)
076                    throws CacheException {
077    
078                    if (!_replicatePuts) {
079                            return;
080                    }
081    
082                    PortalCacheClusterEvent portalCacheClusterEvent =
083                            new PortalCacheClusterEvent(
084                                    ehcache.getName(), element.getKey(),
085                                    PortalCacheClusterEventType.PUT);
086    
087                    if (_replicatePutsViaCopy) {
088                            portalCacheClusterEvent.setElementValue(element.getValue());
089                    }
090    
091                    PortalCacheClusterLinkUtil.sendEvent(portalCacheClusterEvent);
092            }
093    
094            public void notifyElementRemoved(Ehcache ehcache, Element element)
095                    throws CacheException {
096    
097                    if (!_replicateRemovals) {
098                            return;
099                    }
100    
101                    PortalCacheClusterEvent portalCacheClusterEvent =
102                            new PortalCacheClusterEvent(
103                                    ehcache.getName(), element.getKey(),
104                                    PortalCacheClusterEventType.REMOVE);
105    
106                    PortalCacheClusterLinkUtil.sendEvent(portalCacheClusterEvent);
107            }
108    
109            public void notifyElementUpdated(Ehcache ehcache, Element element)
110                    throws CacheException {
111    
112                    if (!_replicateUpdates) {
113                            return;
114                    }
115    
116                    PortalCacheClusterEvent portalCacheClusterEvent =
117                            new PortalCacheClusterEvent(
118                                    ehcache.getName(), element.getKey(),
119                                    PortalCacheClusterEventType.UPDATE);
120    
121                    if (_replicateUpdatesViaCopy) {
122                            portalCacheClusterEvent.setElementValue(element.getValue());
123                    }
124    
125                    PortalCacheClusterLinkUtil.sendEvent(portalCacheClusterEvent);
126            }
127    
128            public void notifyRemoveAll(Ehcache ehcache) {
129                    if (!_replicateRemovals) {
130                            return;
131                    }
132    
133                    PortalCacheClusterEvent portalCacheClusterEvent =
134                            new PortalCacheClusterEvent(
135                                    ehcache.getName(), null,
136                                    PortalCacheClusterEventType.REMOVE_ALL);
137    
138                    PortalCacheClusterLinkUtil.sendEvent(portalCacheClusterEvent);
139            }
140    
141            private static final String _REPLICATE_PUTS = "replicatePuts";
142    
143            private static final String _REPLICATE_PUTS_VIA_COPY =
144                    "replicatePutsViaCopy";
145    
146            private static final String _REPLICATE_REMOVALS = "replicateRemovals";
147    
148            private static final String _REPLICATE_UPDATES = "replicateUpdates";
149    
150            private static final String _REPLICATE_UPDATES_VIA_COPY =
151                    "replicateUpdatesViaCopy";
152    
153            private boolean _replicatePuts;
154            private boolean _replicatePutsViaCopy;
155            private boolean _replicateRemovals = true;
156            private boolean _replicateUpdates = true;
157            private boolean _replicateUpdatesViaCopy;
158    
159    }