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