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                            if (obj == null) {
082                                    return null;
083                            }
084    
085                            int timeToLive = (int)(wci.getRefreshTime() / Time.SECOND);
086    
087                            _portalCache.put(key, obj, timeToLive);
088                    }
089                    catch (WebCacheException wce) {
090                            if (_log.isWarnEnabled()) {
091                                    Throwable cause = wce.getCause();
092    
093                                    if (cause != null) {
094                                            _log.warn(cause, cause);
095                                    }
096                                    else {
097                                            _log.warn(wce, wce);
098                                    }
099                            }
100                    }
101    
102                    return obj;
103            }
104    
105            @Override
106            public void remove(String key) {
107                    _portalCache.remove(key);
108            }
109    
110            private static final String _CACHE_NAME = WebCachePool.class.getName();
111    
112            private static final Log _log = LogFactoryUtil.getLog(
113                    WebCachePoolImpl.class);
114    
115            private PortalCache<String, Object> _portalCache;
116    
117    }