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.clusterlink.messaging;
016    
017    import com.liferay.portal.kernel.cache.PortalCache;
018    import com.liferay.portal.kernel.cache.PortalCacheHelperUtil;
019    import com.liferay.portal.kernel.cache.PortalCacheManager;
020    import com.liferay.portal.kernel.cache.PortalCacheProvider;
021    import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterEvent;
022    import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterEventType;
023    import com.liferay.portal.kernel.io.Deserializer;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.messaging.BaseMessageListener;
027    import com.liferay.portal.kernel.messaging.Message;
028    
029    import java.io.Serializable;
030    
031    import java.nio.ByteBuffer;
032    
033    /**
034     * @author Shuyang Zhou
035     */
036    public class ClusterLinkPortalCacheClusterListener extends BaseMessageListener {
037    
038            @Override
039            protected void doReceive(Message message) throws Exception {
040                    byte[] data = (byte[])message.getPayload();
041    
042                    Deserializer deserializer = new Deserializer(ByteBuffer.wrap(data));
043    
044                    PortalCacheClusterEvent portalCacheClusterEvent =
045                            (PortalCacheClusterEvent)deserializer.readObject();
046    
047                    if (portalCacheClusterEvent == null) {
048                            if (_log.isWarnEnabled()) {
049                                    _log.warn("Payload is null");
050                            }
051    
052                            return;
053                    }
054    
055                    handlePortalCacheClusterEvent(portalCacheClusterEvent);
056            }
057    
058            protected void handlePortalCacheClusterEvent(
059                    PortalCacheClusterEvent portalCacheClusterEvent) {
060    
061                    PortalCacheManager<? extends Serializable, ?> portalCacheManager =
062                            PortalCacheProvider.getPortalCacheManager(
063                                    portalCacheClusterEvent.getPortalCacheManagerName());
064    
065                    PortalCache<Serializable, Serializable> portalCache =
066                            (PortalCache<Serializable, Serializable>)
067                                    portalCacheManager.getCache(
068                                            portalCacheClusterEvent.getPortalCacheName());
069    
070                    if (portalCache == null) {
071                            return;
072                    }
073    
074                    PortalCacheClusterEventType portalCacheClusterEventType =
075                            portalCacheClusterEvent.getEventType();
076    
077                    if (portalCacheClusterEventType.equals(
078                                    PortalCacheClusterEventType.REMOVE_ALL)) {
079    
080                            PortalCacheHelperUtil.removeAllWithoutReplicator(portalCache);
081                    }
082                    else if (portalCacheClusterEventType.equals(
083                                            PortalCacheClusterEventType.PUT) ||
084                                     portalCacheClusterEventType.equals(
085                                            PortalCacheClusterEventType.UPDATE)) {
086    
087                            Serializable key = portalCacheClusterEvent.getElementKey();
088                            Serializable value = portalCacheClusterEvent.getElementValue();
089    
090                            if (value == null) {
091                                    PortalCacheHelperUtil.removeWithoutReplicator(portalCache, key);
092                            }
093                            else {
094                                    PortalCacheHelperUtil.putWithoutReplicator(
095                                            portalCache, key, value,
096                                            portalCacheClusterEvent.getTimeToLive());
097                            }
098                    }
099                    else {
100                            PortalCacheHelperUtil.removeWithoutReplicator(
101                                    portalCache, portalCacheClusterEvent.getElementKey());
102                    }
103            }
104    
105            private static final Log _log = LogFactoryUtil.getLog(
106                    ClusterLinkPortalCacheClusterListener.class);
107    
108    }