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.AggregatedCacheListener;
018    import com.liferay.portal.kernel.cache.PortalCache;
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                    boolean remoteInvoke = AggregatedCacheListener.isRemoteInvoke();
078    
079                    AggregatedCacheListener.setRemoteInvoke(true);
080    
081                    try {
082                            if (portalCacheClusterEventType.equals(
083                                            PortalCacheClusterEventType.REMOVE_ALL)) {
084    
085                                    portalCache.removeAll();
086                            }
087                            else if (portalCacheClusterEventType.equals(
088                                                    PortalCacheClusterEventType.PUT) ||
089                                             portalCacheClusterEventType.equals(
090                                                    PortalCacheClusterEventType.UPDATE)) {
091    
092                                    Serializable key = portalCacheClusterEvent.getElementKey();
093                                    Serializable value = portalCacheClusterEvent.getElementValue();
094    
095                                    if (value == null) {
096                                            portalCache.remove(key);
097                                    }
098                                    else {
099                                            portalCache.put(
100                                                    key, value, portalCacheClusterEvent.getTimeToLive());
101                                    }
102                            }
103                            else {
104                                    portalCache.remove(portalCacheClusterEvent.getElementKey());
105                            }
106                    }
107                    finally {
108                            AggregatedCacheListener.setRemoteInvoke(remoteInvoke);
109                    }
110            }
111    
112            private static final Log _log = LogFactoryUtil.getLog(
113                    ClusterLinkPortalCacheClusterListener.class);
114    
115    }