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