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