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