001    /**
002     * Copyright (c) 2000-2010 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.EhCacheProvider;
019    import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterEvent;
020    import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterEventType;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.messaging.Message;
025    import com.liferay.portal.kernel.messaging.MessageListener;
026    
027    import net.sf.ehcache.Cache;
028    import net.sf.ehcache.CacheManager;
029    
030    /**
031     * @author Shuyang Zhou
032     */
033    public class ClusterLinkPortalCacheClusterRemoveListener
034            implements MessageListener {
035    
036            public ClusterLinkPortalCacheClusterRemoveListener(
037                            EhcachePortalCacheManager ehcachePortalCacheManager)
038                    throws SystemException {
039    
040                    _hibernateCacheManager = EhCacheProvider.getCacheManager();
041                    _portalCacheManager = ehcachePortalCacheManager.getEhcacheManager();
042            }
043    
044            public void receive(Message message) {
045                    PortalCacheClusterEvent portalCacheClusterEvent =
046                            (PortalCacheClusterEvent)message.getPayload();
047    
048                    if (portalCacheClusterEvent == null) {
049                            if (_log.isWarnEnabled()) {
050                                    _log.warn("Payload is null");
051                            }
052    
053                            return;
054                    }
055    
056                    String cacheName = portalCacheClusterEvent.getCacheName();
057    
058                    Cache cache = _portalCacheManager.getCache(cacheName);
059    
060                    if (cache == null) {
061                            cache = _hibernateCacheManager.getCache(cacheName);
062                    }
063    
064                    if (cache != null) {
065                            PortalCacheClusterEventType portalCacheClusterEventType =
066                                    portalCacheClusterEvent.getEventType();
067    
068                            if (portalCacheClusterEventType.equals(
069                                            PortalCacheClusterEventType.REMOVEALL)) {
070    
071                                    cache.removeAll(true);
072                            }
073                            else {
074                                    cache.remove(portalCacheClusterEvent.getElementKey(), true);
075                            }
076                    }
077            }
078    
079            private static Log _log = LogFactoryUtil.getLog(
080                    ClusterLinkPortalCacheClusterRemoveListener.class);
081    
082            private CacheManager _hibernateCacheManager;
083            private CacheManager _portalCacheManager;
084    
085    }