001    /**
002     * Copyright (c) 2000-2013 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.ehcache;
016    
017    import com.liferay.portal.kernel.cache.CacheListener;
018    import com.liferay.portal.kernel.cache.PortalCache;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    
022    import java.io.Serializable;
023    
024    import net.sf.ehcache.CacheException;
025    import net.sf.ehcache.Ehcache;
026    import net.sf.ehcache.Element;
027    import net.sf.ehcache.event.CacheEventListener;
028    
029    /**
030     * @author Edward C. Han
031     * @author Shuyang Zhou
032     */
033    public class PortalCacheCacheEventListener<K extends Serializable, V>
034            implements CacheEventListener {
035    
036            public PortalCacheCacheEventListener(
037                    CacheListener<K, V> cacheListener, PortalCache<K, V> portalCache) {
038    
039                    _cacheListener = cacheListener;
040                    _portalCache = portalCache;
041            }
042    
043            @Override
044            public Object clone() {
045                    return new PortalCacheCacheEventListener<K, V>(
046                            _cacheListener, _portalCache);
047            }
048    
049            public void dispose() {
050            }
051    
052            public void notifyElementEvicted(Ehcache ehcache, Element element) {
053                    K key = (K)element.getKey();
054                    V value = (V)element.getObjectValue();
055    
056                    _cacheListener.notifyEntryEvicted(_portalCache, key, value);
057    
058                    if (_log.isDebugEnabled()) {
059                            _log.debug("Evicted " + key + " from " + ehcache.getName());
060                    }
061            }
062    
063            public void notifyElementExpired(Ehcache ehcache, Element element) {
064                    K key = (K)element.getKey();
065                    V value = (V)element.getObjectValue();
066    
067                    _cacheListener.notifyEntryExpired(_portalCache, key, value);
068    
069                    if (_log.isDebugEnabled()) {
070                            _log.debug("Expired " + key + " from " + ehcache.getName());
071                    }
072            }
073    
074            public void notifyElementPut(Ehcache ehcache, Element element)
075                    throws CacheException {
076    
077                    K key = (K)element.getKey();
078                    V value = (V)element.getObjectValue();
079    
080                    _cacheListener.notifyEntryPut(_portalCache, key, value);
081    
082                    if (_log.isDebugEnabled()) {
083                            _log.debug("Inserted " + key + " into " + ehcache.getName());
084                    }
085            }
086    
087            public void notifyElementRemoved(Ehcache ehcache, Element element)
088                    throws CacheException {
089    
090                    K key = (K)element.getKey();
091                    V value = (V)element.getObjectValue();
092    
093                    _cacheListener.notifyEntryRemoved(_portalCache, key, value);
094    
095                    if (_log.isDebugEnabled()) {
096                            _log.debug("Removed " + key + " from " + ehcache.getName());
097                    }
098            }
099    
100            public void notifyElementUpdated(Ehcache ehcache, Element element)
101                    throws CacheException {
102    
103                    K key = (K)element.getKey();
104                    V value = (V)element.getObjectValue();
105    
106                    _cacheListener.notifyEntryUpdated(_portalCache, key, value);
107    
108                    if (_log.isDebugEnabled()) {
109                            _log.debug("Updated " + key + " in " + ehcache.getName());
110                    }
111            }
112    
113            public void notifyRemoveAll(Ehcache ehcache) {
114                    _cacheListener.notifyRemoveAll(_portalCache);
115    
116                    if (_log.isDebugEnabled()) {
117                            _log.debug("Cleared " + ehcache.getName());
118                    }
119            }
120    
121            private static Log _log = LogFactoryUtil.getLog(
122                    PortalCacheCacheEventListener.class);
123    
124            private CacheListener<K, V> _cacheListener;
125            private PortalCache<K, V> _portalCache;
126    
127    }