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