1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.cache.cluster.clusterlink.messaging;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.cache.ehcache.EhcachePortalCacheManager;
19  import com.liferay.portal.dao.orm.hibernate.EhCacheProvider;
20  import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterEvent;
21  import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterEventType;
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.messaging.Message;
25  import com.liferay.portal.kernel.messaging.MessageListener;
26  
27  import net.sf.ehcache.Cache;
28  import net.sf.ehcache.CacheManager;
29  
30  /**
31   * <a href="ClusterLinkPortalCacheClusterRemoveListener.java.html"><b><i>View
32   * Source</i></b></a>
33   *
34   * @author Shuyang Zhou
35   */
36  public class ClusterLinkPortalCacheClusterRemoveListener
37      implements MessageListener {
38  
39      public ClusterLinkPortalCacheClusterRemoveListener(
40              EhcachePortalCacheManager ehcachePortalCacheManager)
41          throws SystemException {
42  
43          _hibernateCacheManager = EhCacheProvider.getCacheManager();
44          _portalCacheManager = ehcachePortalCacheManager.getEhcacheManager();
45      }
46  
47      public void receive(Message message) {
48          PortalCacheClusterEvent portalCacheClusterEvent =
49              (PortalCacheClusterEvent)message.getPayload();
50  
51          if (portalCacheClusterEvent == null) {
52              if (_log.isWarnEnabled()) {
53                  _log.warn("Payload is null");
54              }
55  
56              return;
57          }
58  
59          String cacheName = portalCacheClusterEvent.getCacheName();
60  
61          Cache cache = _portalCacheManager.getCache(cacheName);
62  
63          if (cache == null) {
64              cache = _hibernateCacheManager.getCache(cacheName);
65          }
66  
67          if (cache != null) {
68              PortalCacheClusterEventType portalCacheClusterEventType =
69                  portalCacheClusterEvent.getEventType();
70  
71              if (portalCacheClusterEventType.equals(
72                      PortalCacheClusterEventType.REMOVEALL)) {
73  
74                  cache.removeAll(true);
75              }
76              else {
77                  cache.remove(portalCacheClusterEvent.getElementKey(), true);
78              }
79          }
80      }
81  
82      private static Log _log = LogFactoryUtil.getLog(
83          ClusterLinkPortalCacheClusterRemoveListener.class);
84  
85      private CacheManager _hibernateCacheManager;
86      private CacheManager _portalCacheManager;
87  
88  }