001    /**
002     * Copyright (c) 2000-2012 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.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 java.io.Serializable;
029    
030    import net.sf.ehcache.CacheManager;
031    import net.sf.ehcache.Ehcache;
032    import net.sf.ehcache.Element;
033    
034    /**
035     * @author Shuyang Zhou
036     */
037    public class ClusterLinkPortalCacheClusterListener
038            extends BaseMessageListener {
039    
040            public ClusterLinkPortalCacheClusterListener() {
041                    LiferayEhcacheRegionFactory liferayEhcacheRegionFactory =
042                            SingletonLiferayEhcacheRegionFactory.getInstance();
043    
044                    _hibernateCacheManager = liferayEhcacheRegionFactory.getCacheManager();
045    
046                    EhcachePortalCacheManager ehcachePortalCacheManager =
047                            (EhcachePortalCacheManager)PortalBeanLocatorUtil.locate(
048                                    _MULTI_VM_PORTAL_CACHE_MANAGER_BEAN_NAME);
049    
050                    _portalCacheManager = ehcachePortalCacheManager.getEhcacheManager();
051            }
052    
053            @Override
054            protected void doReceive(Message message) throws Exception {
055                    PortalCacheClusterEvent portalCacheClusterEvent =
056                            (PortalCacheClusterEvent)message.getPayload();
057    
058                    if (portalCacheClusterEvent == null) {
059                            if (_log.isWarnEnabled()) {
060                                    _log.warn("Payload is null");
061                            }
062    
063                            return;
064                    }
065    
066                    String cacheName = portalCacheClusterEvent.getCacheName();
067    
068                    Ehcache ehcache = _portalCacheManager.getEhcache(cacheName);
069    
070                    if (ehcache == null) {
071                            ehcache = _hibernateCacheManager.getEhcache(cacheName);
072                    }
073    
074                    if (ehcache != null) {
075                            PortalCacheClusterEventType portalCacheClusterEventType =
076                                    portalCacheClusterEvent.getEventType();
077    
078                            if (portalCacheClusterEventType.equals(
079                                            PortalCacheClusterEventType.REMOVEALL)) {
080    
081                                    ehcache.removeAll(true);
082                            }
083                            else if (portalCacheClusterEventType.equals(
084                                                    PortalCacheClusterEventType.PUT) ||
085                                            portalCacheClusterEventType.equals(
086                                                    PortalCacheClusterEventType.UPDATE)) {
087    
088                                    Serializable elementKey =
089                                            portalCacheClusterEvent.getElementKey();
090                                    Serializable elementValue =
091                                            portalCacheClusterEvent.getElementValue();
092    
093                                    if (elementValue == null) {
094                                            ehcache.remove(
095                                                    portalCacheClusterEvent.getElementKey(), true);
096                                    }
097                                    else {
098                                            Element oldElement = ehcache.get(elementKey);
099                                            Element newElement = new Element(elementKey, elementValue);
100    
101                                            if (oldElement != null) {
102                                                    ehcache.replace(newElement);
103                                            }
104                                            else {
105                                                    ehcache.put(newElement);
106                                            }
107                                    }
108                            }
109                            else {
110                                    ehcache.remove(portalCacheClusterEvent.getElementKey(), true);
111                            }
112                    }
113            }
114    
115            private static final String _MULTI_VM_PORTAL_CACHE_MANAGER_BEAN_NAME =
116                    "com.liferay.portal.kernel.cache.MultiVMPortalCacheManager";
117    
118            private static Log _log = LogFactoryUtil.getLog(
119                    ClusterLinkPortalCacheClusterListener.class);
120    
121            private CacheManager _hibernateCacheManager;
122            private CacheManager _portalCacheManager;
123    
124    }