001    /**
002     * Copyright (c) 2000-present 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            @Override
050            public void dispose() {
051            }
052    
053            public CacheListener<K, V> getCacheListener() {
054                    return _cacheListener;
055            }
056    
057            public PortalCache<K, V> getPortalCache() {
058                    return _portalCache;
059            }
060    
061            @Override
062            public void notifyElementEvicted(Ehcache ehcache, Element element) {
063                    K key = (K)element.getObjectKey();
064                    V value = (V)element.getObjectValue();
065                    int timeToLive = element.getTimeToLive();
066    
067                    _cacheListener.notifyEntryEvicted(_portalCache, key, value, timeToLive);
068    
069                    if (_log.isDebugEnabled()) {
070                            _log.debug("Evicted " + key + " from " + ehcache.getName());
071                    }
072            }
073    
074            @Override
075            public void notifyElementExpired(Ehcache ehcache, Element element) {
076                    K key = (K)element.getObjectKey();
077                    V value = (V)element.getObjectValue();
078                    int timeToLive = element.getTimeToLive();
079    
080                    _cacheListener.notifyEntryExpired(_portalCache, key, value, timeToLive);
081    
082                    if (_log.isDebugEnabled()) {
083                            _log.debug("Expired " + key + " from " + ehcache.getName());
084                    }
085            }
086    
087            @Override
088            public void notifyElementPut(Ehcache ehcache, Element element)
089                    throws CacheException {
090    
091                    K key = (K)element.getObjectKey();
092                    V value = (V)element.getObjectValue();
093                    int timeToLive = element.getTimeToLive();
094    
095                    _cacheListener.notifyEntryPut(_portalCache, key, value, timeToLive);
096    
097                    if (_log.isDebugEnabled()) {
098                            _log.debug("Inserted " + key + " into " + ehcache.getName());
099                    }
100            }
101    
102            @Override
103            public void notifyElementRemoved(Ehcache ehcache, Element element)
104                    throws CacheException {
105    
106                    K key = (K)element.getObjectKey();
107                    V value = (V)element.getObjectValue();
108                    int timeToLive = element.getTimeToLive();
109    
110                    _cacheListener.notifyEntryRemoved(_portalCache, key, value, timeToLive);
111    
112                    if (_log.isDebugEnabled()) {
113                            _log.debug("Removed " + key + " from " + ehcache.getName());
114                    }
115            }
116    
117            @Override
118            public void notifyElementUpdated(Ehcache ehcache, Element element)
119                    throws CacheException {
120    
121                    K key = (K)element.getObjectKey();
122                    V value = (V)element.getObjectValue();
123                    int timeToLive = element.getTimeToLive();
124    
125                    _cacheListener.notifyEntryUpdated(_portalCache, key, value, timeToLive);
126    
127                    if (_log.isDebugEnabled()) {
128                            _log.debug("Updated " + key + " in " + ehcache.getName());
129                    }
130            }
131    
132            @Override
133            public void notifyRemoveAll(Ehcache ehcache) {
134                    _cacheListener.notifyRemoveAll(_portalCache);
135    
136                    if (_log.isDebugEnabled()) {
137                            _log.debug("Cleared " + ehcache.getName());
138                    }
139            }
140    
141            private static final Log _log = LogFactoryUtil.getLog(
142                    PortalCacheCacheEventListener.class);
143    
144            private final CacheListener<K, V> _cacheListener;
145            private final PortalCache<K, V> _portalCache;
146    
147    }