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.transactional;
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    
027    /**
028     * @author Shuyang Zhou
029     * @author Edward Han
030     */
031    public class TransactionalPortalCache implements PortalCache {
032    
033            public TransactionalPortalCache(PortalCache portalCache) {
034                    _portalCache = portalCache;
035            }
036    
037            public void destroy() {
038            }
039    
040            public Collection<Object> get(Collection<Serializable> keys) {
041                    List<Object> values = new ArrayList<Object>(keys.size());
042    
043                    for (Serializable key : keys) {
044                            values.add(get(key));
045                    }
046    
047                    return values;
048            }
049    
050            public Object get(Serializable key) {
051                    Object result = null;
052    
053                    if (TransactionalPortalCacheHelper.isEnabled()) {
054                            result = TransactionalPortalCacheHelper.get(_portalCache, key);
055    
056                            if (result == _nullHolder) {
057                                    return null;
058                            }
059                    }
060    
061                    if (result == null) {
062                            result = _portalCache.get(key);
063                    }
064    
065                    return result;
066            }
067    
068            public String getName() {
069                    return _portalCache.getName();
070            }
071    
072            public void put(Serializable key, Object value) {
073                    if (TransactionalPortalCacheHelper.isEnabled()) {
074                            if (value == null) {
075                                    value = _nullHolder;
076                            }
077    
078                            TransactionalPortalCacheHelper.put(_portalCache, key, value);
079                    }
080                    else {
081                            _portalCache.put(key, value);
082                    }
083            }
084    
085            public void put(Serializable key, Object value, int timeToLive) {
086                    if (TransactionalPortalCacheHelper.isEnabled()) {
087                            if (value == null) {
088                                    value = _nullHolder;
089                            }
090    
091                            TransactionalPortalCacheHelper.put(_portalCache, key, value);
092                    }
093                    else {
094                            _portalCache.put(key, value, timeToLive);
095                    }
096            }
097    
098            public void put(Serializable key, Serializable value) {
099                    if (TransactionalPortalCacheHelper.isEnabled()) {
100                            if (value == null) {
101                                    value = _nullHolder;
102                            }
103    
104                            TransactionalPortalCacheHelper.put(_portalCache, key, value);
105                    }
106                    else {
107                            _portalCache.put(key, value);
108                    }
109            }
110    
111            public void put(Serializable key, Serializable value, int timeToLive) {
112                    if (TransactionalPortalCacheHelper.isEnabled()) {
113                            if (value == null) {
114                                    value = _nullHolder;
115                            }
116    
117                            TransactionalPortalCacheHelper.put(_portalCache, key, value);
118                    }
119                    else {
120                            _portalCache.put(key, value, timeToLive);
121                    }
122            }
123    
124            public void registerCacheListener(CacheListener cacheListener) {
125                    _portalCache.registerCacheListener(cacheListener);
126            }
127    
128            public void registerCacheListener(
129                    CacheListener cacheListener, CacheListenerScope cacheListenerScope) {
130    
131                    _portalCache.registerCacheListener(cacheListener, cacheListenerScope);
132            }
133    
134            public void remove(Serializable key) {
135                    if (TransactionalPortalCacheHelper.isEnabled()) {
136                            TransactionalPortalCacheHelper.remove(_portalCache, key);
137                    }
138    
139                    _portalCache.remove(key);
140            }
141    
142            public void removeAll() {
143                    if (TransactionalPortalCacheHelper.isEnabled()) {
144                            TransactionalPortalCacheHelper.removeAll(_portalCache);
145                    }
146    
147                    _portalCache.removeAll();
148            }
149    
150            public void unregisterCacheListener(CacheListener cacheListener) {
151                    _portalCache.unregisterCacheListener(cacheListener);
152            }
153    
154            public void unregisterCacheListeners() {
155                    _portalCache.unregisterCacheListeners();
156            }
157    
158            private static Serializable _nullHolder = new String();
159    
160            private PortalCache _portalCache;
161    
162    }