001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 String getName() {
074                    return _portalCache.getName();
075            }
076    
077            @Override
078            public void put(K key, V value) {
079                    if (TransactionalPortalCacheHelper.isEnabled()) {
080                            if (value == null) {
081                                    TransactionalPortalCacheHelper.put(
082                                            _portalCache, key, (V)NULL_HOLDER);
083                            }
084                            else {
085                                    TransactionalPortalCacheHelper.put(_portalCache, key, value);
086                            }
087                    }
088                    else {
089                            _portalCache.put(key, value);
090                    }
091            }
092    
093            @Override
094            public void put(K key, V value, int timeToLive) {
095                    if (TransactionalPortalCacheHelper.isEnabled()) {
096                            if (value == null) {
097                                    TransactionalPortalCacheHelper.put(
098                                            _portalCache, key, (V)NULL_HOLDER, timeToLive);
099                            }
100                            else {
101                                    TransactionalPortalCacheHelper.put(
102                                            _portalCache, key, value, timeToLive);
103                            }
104                    }
105                    else {
106                            _portalCache.put(key, value, timeToLive);
107                    }
108            }
109    
110            @Override
111            public void registerCacheListener(CacheListener<K, V> cacheListener) {
112                    _portalCache.registerCacheListener(cacheListener);
113            }
114    
115            @Override
116            public void registerCacheListener(
117                    CacheListener<K, V> cacheListener,
118                    CacheListenerScope cacheListenerScope) {
119    
120                    _portalCache.registerCacheListener(cacheListener, cacheListenerScope);
121            }
122    
123            @Override
124            public void remove(K key) {
125                    if (TransactionalPortalCacheHelper.isEnabled()) {
126                            TransactionalPortalCacheHelper.put(
127                                    _portalCache, key, (V)NULL_HOLDER);
128                    }
129                    else {
130                            _portalCache.remove(key);
131                    }
132            }
133    
134            @Override
135            public void removeAll() {
136                    if (TransactionalPortalCacheHelper.isEnabled()) {
137                            TransactionalPortalCacheHelper.removeAll(_portalCache);
138                    }
139                    else {
140                            _portalCache.removeAll();
141                    }
142            }
143    
144            @Override
145            public void unregisterCacheListener(CacheListener<K, V> cacheListener) {
146                    _portalCache.unregisterCacheListener(cacheListener);
147            }
148    
149            @Override
150            public void unregisterCacheListeners() {
151                    _portalCache.unregisterCacheListeners();
152            }
153    
154            protected static Serializable NULL_HOLDER = "NULL_HOLDER";
155    
156            private PortalCache<K, V> _portalCache;
157    
158    }