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