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.webcache;
016    
017    import com.liferay.portal.kernel.cache.PortalCache;
018    import com.liferay.portal.kernel.cache.SingleVMPool;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
022    import com.liferay.portal.kernel.util.Time;
023    import com.liferay.portal.kernel.webcache.WebCacheException;
024    import com.liferay.portal.kernel.webcache.WebCacheItem;
025    import com.liferay.portal.kernel.webcache.WebCachePool;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    @DoPrivileged
031    public class WebCachePoolImpl implements WebCachePool {
032    
033            public void afterPropertiesSet() {
034                    _portalCache = (PortalCache<String, Object>)_singleVMPool.getCache(
035                            _CACHE_NAME);
036            }
037    
038            @Override
039            public void clear() {
040                    _portalCache.removeAll();
041            }
042    
043            @Override
044            public Object get(String key, WebCacheItem wci) {
045                    Object obj = _portalCache.get(key);
046    
047                    if (obj != null) {
048                            return obj;
049                    }
050    
051                    try {
052                            obj = wci.convert(key);
053    
054                            int timeToLive = (int)(wci.getRefreshTime() / Time.SECOND);
055    
056                            _portalCache.put(key, obj, timeToLive);
057                    }
058                    catch (WebCacheException wce) {
059                            if (_log.isWarnEnabled()) {
060                                    Throwable cause = wce.getCause();
061    
062                                    if (cause != null) {
063                                            _log.warn(cause, cause);
064                                    }
065                                    else {
066                                            _log.warn(wce, wce);
067                                    }
068                            }
069                    }
070    
071                    return obj;
072            }
073    
074            @Override
075            public void remove(String key) {
076                    _portalCache.remove(key);
077            }
078    
079            public void setSingleVMPool(SingleVMPool singleVMPool) {
080                    _singleVMPool = singleVMPool;
081            }
082    
083            private static final String _CACHE_NAME = WebCachePool.class.getName();
084    
085            private static Log _log = LogFactoryUtil.getLog(WebCachePoolImpl.class);
086    
087            private PortalCache<String, Object> _portalCache;
088            private SingleVMPool _singleVMPool;
089    
090    }