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