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