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