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.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            public void clear() {
039                    _portalCache.removeAll();
040            }
041    
042            public Object get(String key, WebCacheItem wci) {
043                    Object obj = _portalCache.get(key);
044    
045                    if (obj == null) {
046                            try {
047                                    obj = wci.convert(key);
048    
049                                    int timeToLive = (int)(wci.getRefreshTime() / Time.SECOND);
050    
051                                    _portalCache.put(key, obj, timeToLive);
052                            }
053                            catch (WebCacheException wce) {
054                                    if (_log.isWarnEnabled()) {
055                                            Throwable cause = wce.getCause();
056    
057                                            if (cause != null) {
058                                                    _log.warn(cause, cause);
059                                            }
060                                            else {
061                                                    _log.warn(wce, wce);
062                                            }
063                                    }
064                            }
065                    }
066    
067                    return obj;
068            }
069    
070            public void remove(String key) {
071                    _portalCache.remove(key);
072            }
073    
074            public void setSingleVMPool(SingleVMPool singleVMPool) {
075                    _singleVMPool = singleVMPool;
076            }
077    
078            private static final String _CACHE_NAME = WebCachePool.class.getName();
079    
080            private static Log _log = LogFactoryUtil.getLog(WebCachePoolImpl.class);
081    
082            private PortalCache<String, Object> _portalCache;
083            private SingleVMPool _singleVMPool;
084    
085    }