1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.cache.cluster.clusterlink.messaging;
16  
17  import com.liferay.portal.cache.ehcache.EhcachePortalCacheManager;
18  import com.liferay.portal.dao.orm.hibernate.EhCacheProvider;
19  import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterEvent;
20  import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterEventType;
21  import com.liferay.portal.kernel.exception.SystemException;
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  }