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.bean.PortalBeanLocatorUtil;
21  import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterEvent;
22  import com.liferay.portal.kernel.cache.cluster.PortalCacheClusterEventType;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.messaging.Message;
26  import com.liferay.portal.kernel.messaging.MessageListener;
27  
28  import net.sf.ehcache.Cache;
29  import net.sf.ehcache.CacheManager;
30  
31  /**
32   * <a href="ClusterLinkPortalCacheClusterRemoveListener.java.html"><b><i>View
33   * Source</i></b></a>
34   *
35   * @author Shuyang Zhou
36   */
37  public class ClusterLinkPortalCacheClusterRemoveListener
38      implements MessageListener {
39  
40      public ClusterLinkPortalCacheClusterRemoveListener()
41          throws SystemException {
42  
43          _hibernateCacheManager = EhCacheProvider.getCacheManager();
44  
45          EhcachePortalCacheManager ehcachePortalCacheManager =
46              (EhcachePortalCacheManager)PortalBeanLocatorUtil.locate(
47                  _MULTI_VM_PORTAL_CACHE_MANAGER_BEAN_NAME);
48  
49          _portalCacheManager = ehcachePortalCacheManager.getEhcacheManager();
50      }
51  
52      public void receive(Message message) {
53          PortalCacheClusterEvent portalCacheClusterEvent =
54              (PortalCacheClusterEvent)message.getPayload();
55  
56          if (portalCacheClusterEvent == null) {
57              if (_log.isWarnEnabled()) {
58                  _log.warn("Payload is null");
59              }
60  
61              return;
62          }
63  
64          String cacheName = portalCacheClusterEvent.getCacheName();
65  
66          Cache cache = _portalCacheManager.getCache(cacheName);
67  
68          if (cache == null) {
69              cache = _hibernateCacheManager.getCache(cacheName);
70          }
71  
72          if (cache != null) {
73              PortalCacheClusterEventType portalCacheClusterEventType =
74                  portalCacheClusterEvent.getEventType();
75  
76              if (portalCacheClusterEventType.equals(
77                      PortalCacheClusterEventType.REMOVEALL)) {
78  
79                  cache.removeAll(true);
80              }
81              else {
82                  cache.remove(portalCacheClusterEvent.getElementKey(), true);
83              }
84          }
85      }
86  
87      private static final String _MULTI_VM_PORTAL_CACHE_MANAGER_BEAN_NAME =
88          "com.liferay.portal.kernel.cache.MultiVMPortalCacheManager";
89  
90      private static Log _log = LogFactoryUtil.getLog(
91          ClusterLinkPortalCacheClusterRemoveListener.class);
92  
93      private CacheManager _hibernateCacheManager;
94      private CacheManager _portalCacheManager;
95  
96  }