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.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     */
032    public class PortalCacheCacheEventListener implements CacheEventListener {
033    
034            public PortalCacheCacheEventListener(
035                    CacheListener cacheListener, PortalCache portalCache) {
036    
037                    _cacheListener = cacheListener;
038                    _portalCache = portalCache;
039            }
040    
041            @Override
042            public Object clone() {
043                    return new PortalCacheCacheEventListener(_cacheListener, _portalCache);
044            }
045    
046            public void dispose() {
047            }
048    
049            public void notifyElementEvicted(Ehcache ehcache, Element element) {
050                    Serializable key = element.getKey();
051    
052                    _cacheListener.notifyEntryEvicted(
053                            _portalCache, String.valueOf(key), element.getObjectValue());
054    
055                    if (_log.isDebugEnabled()) {
056                            _log.debug("Evicted " + key + " from " + ehcache.getName());
057                    }
058            }
059    
060            public void notifyElementExpired(Ehcache ehcache, Element element) {
061                    Serializable key = element.getKey();
062    
063                    _cacheListener.notifyEntryExpired(
064                            _portalCache, String.valueOf(key), element.getObjectValue());
065    
066                    if (_log.isDebugEnabled()) {
067                            _log.debug("Expired " + key + " from " + ehcache.getName());
068                    }
069            }
070    
071            public void notifyElementPut(Ehcache ehcache, Element element)
072                    throws CacheException {
073    
074                    Serializable key = element.getKey();
075    
076                    _cacheListener.notifyEntryPut(
077                            _portalCache, String.valueOf(key), element.getObjectValue());
078    
079                    if (_log.isDebugEnabled()) {
080                            _log.debug("Inserted " + key + " into " + ehcache.getName());
081                    }
082            }
083    
084            public void notifyElementRemoved(Ehcache ehcache, Element element)
085                    throws CacheException {
086    
087                    Serializable key = element.getKey();
088    
089                    _cacheListener.notifyEntryRemoved(
090                            _portalCache, String.valueOf(key), element.getObjectValue());
091    
092                    if (_log.isDebugEnabled()) {
093                            _log.debug("Removed " + key + " from " + ehcache.getName());
094                    }
095            }
096    
097            public void notifyElementUpdated(Ehcache ehcache, Element element)
098                    throws CacheException {
099    
100                    Serializable key = element.getKey();
101    
102                    _cacheListener.notifyEntryUpdated(
103                            _portalCache, String.valueOf(key), element.getObjectValue());
104    
105                    if (_log.isDebugEnabled()) {
106                            _log.debug("Updated " + key + " in " + ehcache.getName());
107                    }
108            }
109    
110            public void notifyRemoveAll(Ehcache ehcache) {
111                    _cacheListener.notifyRemoveAll(_portalCache);
112    
113                    if (_log.isDebugEnabled()) {
114                            _log.debug("Cleared " + ehcache.getName());
115                    }
116            }
117    
118            private static Log _log = LogFactoryUtil.getLog(
119                    PortalCacheCacheEventListener.class);
120    
121            private CacheListener _cacheListener;
122            private PortalCache _portalCache;
123    
124    }