001    /**
002     * Copyright (c) 2000-2011 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.BasePortalCache;
018    import com.liferay.portal.kernel.cache.PortalCache;
019    
020    import java.io.Serializable;
021    
022    import java.util.ArrayList;
023    import java.util.Collection;
024    import java.util.List;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class TransactionalPortalCache extends BasePortalCache {
030    
031            public TransactionalPortalCache(PortalCache portalCache) {
032                    _portalCache = portalCache;
033            }
034    
035            public Collection<Object> get(Collection<String> keys) {
036                    List<Object> values = new ArrayList<Object>(keys.size());
037    
038                    for (String key : keys) {
039                            values.add(get(key));
040                    }
041    
042                    return values;
043            }
044    
045            public Object get(String key) {
046                    Object result = null;
047    
048                    if (TransactionalPortalCacheHelper.isEnabled()) {
049                            result = TransactionalPortalCacheHelper.get(_portalCache, key);
050    
051                            if (result == _nullHolder) {
052                                    return null;
053                            }
054                    }
055    
056                    if (result == null) {
057                            result = _portalCache.get(key);
058                    }
059    
060                    return result;
061            }
062    
063            public void put(String key, Object obj) {
064                    if (TransactionalPortalCacheHelper.isEnabled()) {
065                            if (obj == null) {
066                                    obj = _nullHolder;
067                            }
068    
069                            TransactionalPortalCacheHelper.put(_portalCache, key, obj);
070                    }
071                    else {
072                            _portalCache.put(key, obj);
073                    }
074            }
075    
076            public void put(String key, Object obj, int timeToLive) {
077                    if (TransactionalPortalCacheHelper.isEnabled()) {
078                            if (obj == null) {
079                                    obj = _nullHolder;
080                            }
081    
082                            TransactionalPortalCacheHelper.put(_portalCache, key, obj);
083                    }
084                    else {
085                            _portalCache.put(key, obj, timeToLive);
086                    }
087            }
088    
089            public void put(String key, Serializable obj) {
090                    if (TransactionalPortalCacheHelper.isEnabled()) {
091                            if (obj == null) {
092                                    obj = _nullHolder;
093                            }
094    
095                            TransactionalPortalCacheHelper.put(_portalCache, key, obj);
096                    }
097                    else {
098                            _portalCache.put(key, obj);
099                    }
100            }
101    
102            public void put(String key, Serializable obj, int timeToLive) {
103                    if (TransactionalPortalCacheHelper.isEnabled()) {
104                            if (obj == null) {
105                                    obj = _nullHolder;
106                            }
107    
108                            TransactionalPortalCacheHelper.put(_portalCache, key, obj);
109                    }
110                    else {
111                            _portalCache.put(key, obj, timeToLive);
112                    }
113            }
114    
115            public void remove(String key) {
116                    if (TransactionalPortalCacheHelper.isEnabled()) {
117                            TransactionalPortalCacheHelper.remove(_portalCache, key);
118                    }
119    
120                    _portalCache.remove(key);
121            }
122    
123            public void removeAll() {
124                    if (TransactionalPortalCacheHelper.isEnabled()) {
125                            TransactionalPortalCacheHelper.removeAll(_portalCache);
126                    }
127    
128                    _portalCache.removeAll();
129            }
130    
131            private static Serializable _nullHolder = new String();
132    
133            private PortalCache _portalCache;
134    
135    }