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.CacheListenerScope;
019    import com.liferay.portal.kernel.cache.PortalCache;
020    
021    import java.io.Serializable;
022    
023    import java.util.ArrayList;
024    import java.util.Collection;
025    import java.util.List;
026    import java.util.Map;
027    import java.util.concurrent.ConcurrentHashMap;
028    
029    import net.sf.ehcache.Ehcache;
030    import net.sf.ehcache.Element;
031    import net.sf.ehcache.event.CacheEventListener;
032    import net.sf.ehcache.event.NotificationScope;
033    import net.sf.ehcache.event.RegisteredEventListeners;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Edward Han
038     * @author Shuyang Zhou
039     */
040    public class EhcachePortalCache<K extends Serializable, V>
041            implements PortalCache<K, V> {
042    
043            public EhcachePortalCache(Ehcache ehcache) {
044                    _ehcache = ehcache;
045            }
046    
047            public void destroy() {
048            }
049    
050            public Collection<V> get(Collection<K> keys) {
051                    List<V> values = new ArrayList<V>(keys.size());
052    
053                    for (K key : keys) {
054                            values.add(get(key));
055                    }
056    
057                    return values;
058            }
059    
060            public V get(K key) {
061                    Element element = _ehcache.get(key);
062    
063                    if (element == null) {
064                            return null;
065                    }
066                    else {
067                            return (V)element.getObjectValue();
068                    }
069            }
070    
071            public String getName() {
072                    return _ehcache.getName();
073            }
074    
075            public void put(K key, V value) {
076                    Element element = new Element(key, value);
077    
078                    _ehcache.put(element);
079            }
080    
081            public void put(K key, V value, int timeToLive) {
082                    Element element = new Element(key, value);
083    
084                    element.setTimeToLive(timeToLive);
085    
086                    _ehcache.put(element);
087            }
088    
089            public void registerCacheListener(CacheListener<K, V> cacheListener) {
090                    registerCacheListener(cacheListener, CacheListenerScope.ALL);
091            }
092    
093            public void registerCacheListener(
094                    CacheListener<K, V> cacheListener,
095                    CacheListenerScope cacheListenerScope) {
096    
097                    if (_cacheEventListeners.containsKey(cacheListener)) {
098                            return;
099                    }
100    
101                    CacheEventListener cacheEventListener =
102                            new PortalCacheCacheEventListener<K, V>(cacheListener, this);
103    
104                    _cacheEventListeners.put(cacheListener, cacheEventListener);
105    
106                    NotificationScope notificationScope = getNotificationScope(
107                            cacheListenerScope);
108    
109                    RegisteredEventListeners registeredEventListeners =
110                            _ehcache.getCacheEventNotificationService();
111    
112                    registeredEventListeners.registerListener(
113                            cacheEventListener, notificationScope);
114            }
115    
116            public void remove(K key) {
117                    _ehcache.remove(key);
118            }
119    
120            public void removeAll() {
121                    _ehcache.removeAll();
122            }
123    
124            public void setEhcache(Ehcache ehcache) {
125                    _ehcache = ehcache;
126            }
127    
128            public void unregisterCacheListener(CacheListener<K, V> cacheListener) {
129                    CacheEventListener cacheEventListener = _cacheEventListeners.get(
130                            cacheListener);
131    
132                    if (cacheEventListener != null) {
133                            RegisteredEventListeners registeredEventListeners =
134                                    _ehcache.getCacheEventNotificationService();
135    
136                            registeredEventListeners.unregisterListener(cacheEventListener);
137                    }
138    
139                    _cacheEventListeners.remove(cacheListener);
140            }
141    
142            public void unregisterCacheListeners() {
143                    RegisteredEventListeners registeredEventListeners =
144                            _ehcache.getCacheEventNotificationService();
145    
146                    for (CacheEventListener cacheEventListener :
147                                    _cacheEventListeners.values()) {
148    
149                            registeredEventListeners.unregisterListener(cacheEventListener);
150                    }
151    
152                    _cacheEventListeners.clear();
153            }
154    
155            protected NotificationScope getNotificationScope(
156                    CacheListenerScope cacheListenerScope) {
157    
158                    if (cacheListenerScope.equals(CacheListenerScope.ALL)) {
159                            return NotificationScope.ALL;
160                    }
161                    else if (cacheListenerScope.equals(CacheListenerScope.LOCAL)) {
162                            return NotificationScope.LOCAL;
163                    }
164                    else {
165                            return NotificationScope.REMOTE;
166                    }
167            }
168    
169            private Map<CacheListener<K, V>, CacheEventListener> _cacheEventListeners =
170                    new ConcurrentHashMap<CacheListener<K, V>, CacheEventListener>();
171            private Ehcache _ehcache;
172    
173    }