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.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 implements PortalCache {
041    
042            public EhcachePortalCache(Ehcache ehcache) {
043                    _ehcache = ehcache;
044            }
045    
046            public void destroy() {
047            }
048    
049            public Collection<Object> get(Collection<Serializable> keys) {
050                    List<Object> values = new ArrayList<Object>(keys.size());
051    
052                    for (Serializable key : keys) {
053                            values.add(get(key));
054                    }
055    
056                    return values;
057            }
058    
059            public Object get(Serializable key) {
060                    Element element = _ehcache.get(key);
061    
062                    if (element == null) {
063                            return null;
064                    }
065                    else {
066                            return element.getObjectValue();
067                    }
068            }
069    
070            public String getName() {
071                    return _ehcache.getName();
072            }
073    
074            public void put(Serializable key, Object value) {
075                    Element element = new Element(key, value);
076    
077                    _ehcache.put(element);
078            }
079    
080            public void put(Serializable key, Object value, int timeToLive) {
081                    Element element = new Element(key, value);
082    
083                    element.setTimeToLive(timeToLive);
084    
085                    _ehcache.put(element);
086            }
087    
088            public void put(Serializable key, Serializable value) {
089                    Element element = new Element(key, value);
090    
091                    _ehcache.put(element);
092            }
093    
094            public void put(Serializable key, Serializable value, int timeToLive) {
095                    Element element = new Element(key, value);
096    
097                    element.setTimeToLive(timeToLive);
098    
099                    _ehcache.put(element);
100            }
101    
102            public void registerCacheListener(CacheListener cacheListener) {
103                    registerCacheListener(cacheListener, CacheListenerScope.ALL);
104            }
105    
106            public void registerCacheListener(
107                    CacheListener cacheListener, CacheListenerScope cacheListenerScope) {
108    
109                    if (_cacheEventListeners.containsKey(cacheListener)) {
110                            return;
111                    }
112    
113                    CacheEventListener cacheEventListener =
114                            new PortalCacheCacheEventListener(cacheListener, this);
115    
116                    _cacheEventListeners.put(cacheListener, cacheEventListener);
117    
118                    NotificationScope notificationScope = getNotificationScope(
119                            cacheListenerScope);
120    
121                    RegisteredEventListeners registeredEventListeners =
122                            _ehcache.getCacheEventNotificationService();
123    
124                    registeredEventListeners.registerListener(
125                            cacheEventListener, notificationScope);
126            }
127    
128            public void remove(Serializable key) {
129                    _ehcache.remove(key);
130            }
131    
132            public void removeAll() {
133                    _ehcache.removeAll();
134            }
135    
136            public void setEhcache(Ehcache ehcache) {
137                    _ehcache = ehcache;
138            }
139    
140            public void unregisterCacheListener(CacheListener cacheListener) {
141                    CacheEventListener cacheEventListener = _cacheEventListeners.get(
142                            cacheListener);
143    
144                    if (cacheEventListener != null) {
145                            RegisteredEventListeners registeredEventListeners =
146                                    _ehcache.getCacheEventNotificationService();
147    
148                            registeredEventListeners.unregisterListener(cacheEventListener);
149                    }
150    
151                    _cacheEventListeners.remove(cacheListener);
152            }
153    
154            public void unregisterCacheListeners() {
155                    RegisteredEventListeners registeredEventListeners =
156                            _ehcache.getCacheEventNotificationService();
157    
158                    for (CacheEventListener cacheEventListener :
159                                    _cacheEventListeners.values()) {
160    
161                            registeredEventListeners.unregisterListener(cacheEventListener);
162                    }
163    
164                    _cacheEventListeners.clear();
165            }
166    
167            protected NotificationScope getNotificationScope(
168                    CacheListenerScope cacheListenerScope) {
169    
170                    if (cacheListenerScope.equals(CacheListenerScope.ALL)) {
171                            return NotificationScope.ALL;
172                    }
173                    else if (cacheListenerScope.equals(CacheListenerScope.LOCAL)) {
174                            return NotificationScope.LOCAL;
175                    }
176                    else {
177                            return NotificationScope.REMOTE;
178                    }
179            }
180    
181            private Map<CacheListener, CacheEventListener> _cacheEventListeners =
182                    new ConcurrentHashMap<CacheListener, CacheEventListener>();
183            private Ehcache _ehcache;
184    
185    }