001    /**
002     * Copyright (c) 2000-present 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    import com.liferay.registry.Registry;
027    import com.liferay.registry.RegistryUtil;
028    import com.liferay.registry.dependency.ServiceDependencyListener;
029    import com.liferay.registry.dependency.ServiceDependencyManager;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    @DoPrivileged
035    public class WebCachePoolImpl implements WebCachePool {
036    
037            public void afterPropertiesSet() {
038                    ServiceDependencyManager serviceDependencyManager =
039                            new ServiceDependencyManager();
040    
041                    serviceDependencyManager.addServiceDependencyListener(
042                            new ServiceDependencyListener() {
043    
044                                    @Override
045                                    public void dependenciesFulfilled() {
046                                            Registry registry = RegistryUtil.getRegistry();
047    
048                                            SingleVMPool singleVMPool = registry.getService(
049                                                    SingleVMPool.class);
050    
051                                            _portalCache =
052                                                    (PortalCache<String, Object>)
053                                                            singleVMPool.getPortalCache(_CACHE_NAME);
054                                    }
055    
056                                    @Override
057                                    public void destroy() {
058                                    }
059    
060                            });
061    
062                    serviceDependencyManager.registerDependencies(SingleVMPool.class);
063            }
064    
065            @Override
066            public void clear() {
067                    _portalCache.removeAll();
068            }
069    
070            @Override
071            public Object get(String key, WebCacheItem wci) {
072                    Object obj = _portalCache.get(key);
073    
074                    if (obj != null) {
075                            return obj;
076                    }
077    
078                    try {
079                            obj = wci.convert(key);
080    
081                            int timeToLive = (int)(wci.getRefreshTime() / Time.SECOND);
082    
083                            _portalCache.put(key, obj, timeToLive);
084                    }
085                    catch (WebCacheException wce) {
086                            if (_log.isWarnEnabled()) {
087                                    Throwable cause = wce.getCause();
088    
089                                    if (cause != null) {
090                                            _log.warn(cause, cause);
091                                    }
092                                    else {
093                                            _log.warn(wce, wce);
094                                    }
095                            }
096                    }
097    
098                    return obj;
099            }
100    
101            @Override
102            public void remove(String key) {
103                    _portalCache.remove(key);
104            }
105    
106            private static final String _CACHE_NAME = WebCachePool.class.getName();
107    
108            private static final Log _log = LogFactoryUtil.getLog(
109                    WebCachePoolImpl.class);
110    
111            private PortalCache<String, Object> _portalCache;
112    
113    }