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.cache.PortalCacheException;
020    
021    import java.io.Serializable;
022    
023    import net.sf.ehcache.Element;
024    import net.sf.ehcache.event.CacheEventListener;
025    
026    /**
027     * @author Tina Tian
028     */
029    public class EhcacheCacheListenerAdapter<K extends Serializable, V>
030            implements CacheListener<K, V> {
031    
032            public EhcacheCacheListenerAdapter(CacheEventListener cacheEventListener) {
033                    _cacheEventListener = cacheEventListener;
034            }
035    
036            @Override
037            public void notifyEntryEvicted(
038                            PortalCache<K, V> portalCache, K key, V value, int timeToLive)
039                    throws PortalCacheException {
040    
041                    Element element = new Element(key, value);
042    
043                    if (timeToLive != PortalCache.DEFAULT_TIME_TO_LIVE) {
044                            element.setTimeToLive(timeToLive);
045                    }
046    
047                    _cacheEventListener.notifyElementEvicted(
048                            EhcacheUnwrapUtil.getEhcache(portalCache), element);
049            }
050    
051            @Override
052            public void notifyEntryExpired(
053                            PortalCache<K, V> portalCache, K key, V value, int timeToLive)
054                    throws PortalCacheException {
055    
056                    Element element = new Element(key, value);
057    
058                    if (timeToLive != PortalCache.DEFAULT_TIME_TO_LIVE) {
059                            element.setTimeToLive(timeToLive);
060                    }
061    
062                    _cacheEventListener.notifyElementExpired(
063                            EhcacheUnwrapUtil.getEhcache(portalCache), element);
064            }
065    
066            @Override
067            public void notifyEntryPut(
068                            PortalCache<K, V> portalCache, K key, V value, int timeToLive)
069                    throws PortalCacheException {
070    
071                    Element element = new Element(key, value);
072    
073                    if (timeToLive != PortalCache.DEFAULT_TIME_TO_LIVE) {
074                            element.setTimeToLive(timeToLive);
075                    }
076    
077                    _cacheEventListener.notifyElementPut(
078                            EhcacheUnwrapUtil.getEhcache(portalCache), element);
079            }
080    
081            @Override
082            public void notifyEntryRemoved(
083                            PortalCache<K, V> portalCache, K key, V value, int timeToLive)
084                    throws PortalCacheException {
085    
086                    Element element = new Element(key, value);
087    
088                    if (timeToLive != PortalCache.DEFAULT_TIME_TO_LIVE) {
089                            element.setTimeToLive(timeToLive);
090                    }
091    
092                    _cacheEventListener.notifyElementRemoved(
093                            EhcacheUnwrapUtil.getEhcache(portalCache), element);
094            }
095    
096            @Override
097            public void notifyEntryUpdated(
098                            PortalCache<K, V> portalCache, K key, V value, int timeToLive)
099                    throws PortalCacheException {
100    
101                    Element element = new Element(key, value);
102    
103                    if (timeToLive != PortalCache.DEFAULT_TIME_TO_LIVE) {
104                            element.setTimeToLive(timeToLive);
105                    }
106    
107                    _cacheEventListener.notifyElementUpdated(
108                            EhcacheUnwrapUtil.getEhcache(portalCache), element);
109            }
110    
111            @Override
112            public void notifyRemoveAll(PortalCache<K, V> portalCache)
113                    throws PortalCacheException {
114    
115                    _cacheEventListener.notifyRemoveAll(
116                            EhcacheUnwrapUtil.getEhcache(portalCache));
117            }
118    
119            private final CacheEventListener _cacheEventListener;
120    
121    }