001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 net.sf.ehcache.CacheManager;
029    import net.sf.ehcache.Ehcache;
030    
031    /**
032     * @author Shuyang Zhou
033     */
034    public class ClusterLinkPortalCacheClusterRemoveListener
035            extends BaseMessageListener {
036    
037            public ClusterLinkPortalCacheClusterRemoveListener() {
038                    LiferayEhcacheRegionFactory liferayEhcacheRegionFactory =
039                            SingletonLiferayEhcacheRegionFactory.getInstance();
040    
041                    _hibernateCacheManager = liferayEhcacheRegionFactory.getCacheManager();
042    
043                    EhcachePortalCacheManager ehcachePortalCacheManager =
044                            (EhcachePortalCacheManager)PortalBeanLocatorUtil.locate(
045                                    _MULTI_VM_PORTAL_CACHE_MANAGER_BEAN_NAME);
046    
047                    _portalCacheManager = ehcachePortalCacheManager.getEhcacheManager();
048            }
049    
050            @Override
051            protected void doReceive(Message message) throws Exception {
052                    PortalCacheClusterEvent portalCacheClusterEvent =
053                            (PortalCacheClusterEvent)message.getPayload();
054    
055                    if (portalCacheClusterEvent == null) {
056                            if (_log.isWarnEnabled()) {
057                                    _log.warn("Payload is null");
058                            }
059    
060                            return;
061                    }
062    
063                    String cacheName = portalCacheClusterEvent.getCacheName();
064    
065                    Ehcache ehcache = _portalCacheManager.getEhcache(cacheName);
066    
067                    if (ehcache == null) {
068                            ehcache = _hibernateCacheManager.getEhcache(cacheName);
069                    }
070    
071                    if (ehcache != null) {
072                            PortalCacheClusterEventType portalCacheClusterEventType =
073                                    portalCacheClusterEvent.getEventType();
074    
075                            if (portalCacheClusterEventType.equals(
076                                            PortalCacheClusterEventType.REMOVEALL)) {
077    
078                                    ehcache.removeAll(true);
079                            }
080                            else {
081                                    ehcache.remove(portalCacheClusterEvent.getElementKey(), true);
082                            }
083                    }
084            }
085    
086            private static final String _MULTI_VM_PORTAL_CACHE_MANAGER_BEAN_NAME =
087                    "com.liferay.portal.kernel.cache.MultiVMPortalCacheManager";
088    
089            private static Log _log = LogFactoryUtil.getLog(
090                    ClusterLinkPortalCacheClusterRemoveListener.class);
091    
092            private CacheManager _hibernateCacheManager;
093            private CacheManager _portalCacheManager;
094    
095    }