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.clusterlink.messaging;
016    
017    import com.liferay.portal.cache.ehcache.EhcachePortalCacheManager;
018    import com.liferay.portal.dao.orm.hibernate.region.LiferayEhcacheRegionFactory;
019    import com.liferay.portal.dao.orm.hibernate.region.SingletonLiferayEhcacheRegionFactory;
020    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
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    import net.sf.ehcache.CacheManager;
034    import net.sf.ehcache.Ehcache;
035    import net.sf.ehcache.Element;
036    
037    /**
038     * @author Shuyang Zhou
039     */
040    public class ClusterLinkPortalCacheClusterListener extends BaseMessageListener {
041    
042            public ClusterLinkPortalCacheClusterListener() {
043                    LiferayEhcacheRegionFactory liferayEhcacheRegionFactory =
044                            SingletonLiferayEhcacheRegionFactory.getInstance();
045    
046                    _hibernateCacheManager = liferayEhcacheRegionFactory.getCacheManager();
047    
048                    EhcachePortalCacheManager<?, ?> ehcachePortalCacheManager =
049                            (EhcachePortalCacheManager<?, ?>)PortalBeanLocatorUtil.locate(
050                                    _MULTI_VM_PORTAL_CACHE_MANAGER_BEAN_NAME);
051    
052                    _portalCacheManager = ehcachePortalCacheManager.getEhcacheManager();
053            }
054    
055            @Override
056            protected void doReceive(Message message) throws Exception {
057                    byte[] data = (byte[])message.getPayload();
058    
059                    Deserializer deserializer = new Deserializer(ByteBuffer.wrap(data));
060    
061                    PortalCacheClusterEvent portalCacheClusterEvent =
062                            (PortalCacheClusterEvent)deserializer.readObject();
063    
064                    if (portalCacheClusterEvent == null) {
065                            if (_log.isWarnEnabled()) {
066                                    _log.warn("Payload is null");
067                            }
068    
069                            return;
070                    }
071    
072                    String cacheName = portalCacheClusterEvent.getCacheName();
073    
074                    Ehcache ehcache = _portalCacheManager.getEhcache(cacheName);
075    
076                    if ((ehcache == null) && (_hibernateCacheManager != null)) {
077                            ehcache = _hibernateCacheManager.getEhcache(cacheName);
078                    }
079    
080                    if (ehcache != null) {
081                            PortalCacheClusterEventType portalCacheClusterEventType =
082                                    portalCacheClusterEvent.getEventType();
083    
084                            if (portalCacheClusterEventType.equals(
085                                            PortalCacheClusterEventType.REMOVE_ALL)) {
086    
087                                    ehcache.removeAll(true);
088                            }
089                            else if (portalCacheClusterEventType.equals(
090                                                    PortalCacheClusterEventType.PUT) ||
091                                             portalCacheClusterEventType.equals(
092                                                    PortalCacheClusterEventType.UPDATE)) {
093    
094                                    Serializable elementKey =
095                                            portalCacheClusterEvent.getElementKey();
096                                    Serializable elementValue =
097                                            portalCacheClusterEvent.getElementValue();
098    
099                                    if (elementValue == null) {
100                                            ehcache.remove(elementKey, true);
101                                    }
102                                    else {
103                                            ehcache.put(new Element(elementKey, elementValue), true);
104                                    }
105                            }
106                            else {
107                                    ehcache.remove(portalCacheClusterEvent.getElementKey(), true);
108                            }
109                    }
110            }
111    
112            private static final String _MULTI_VM_PORTAL_CACHE_MANAGER_BEAN_NAME =
113                    "com.liferay.portal.kernel.cache.MultiVMPortalCacheManager";
114    
115            private static Log _log = LogFactoryUtil.getLog(
116                    ClusterLinkPortalCacheClusterListener.class);
117    
118            private CacheManager _hibernateCacheManager;
119            private CacheManager _portalCacheManager;
120    
121    }