001    /**
002     * Copyright (c) 2000-2010 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.cache.ehcache;
016    
017    import com.liferay.portal.kernel.cache.BlockingPortalCache;
018    import com.liferay.portal.kernel.cache.PortalCache;
019    import com.liferay.portal.kernel.cache.PortalCacheManager;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.ReflectionUtil;
023    import com.liferay.portal.util.PropsUtil;
024    import com.liferay.portal.util.PropsValues;
025    
026    import java.lang.reflect.Field;
027    
028    import java.net.URL;
029    
030    import javax.management.MBeanServer;
031    
032    import net.sf.ehcache.CacheManager;
033    import net.sf.ehcache.Ehcache;
034    import net.sf.ehcache.ObjectExistsException;
035    import net.sf.ehcache.management.ManagementService;
036    import net.sf.ehcache.util.FailSafeTimer;
037    
038    /**
039     * @author Joseph Shum
040     * @author Raymond Augé
041     * @author Michael C. Han
042     */
043    public class EhcachePortalCacheManager implements PortalCacheManager {
044    
045            public void afterPropertiesSet() {
046                    URL url = getClass().getResource(PropsUtil.get(_configPropertyKey));
047    
048                    _cacheManager = new CacheManager(url);
049    
050                    FailSafeTimer failSafeTimer = _cacheManager.getTimer();
051    
052                    failSafeTimer.cancel();
053    
054                    try {
055                            Field cacheManagerTimerField = ReflectionUtil.getDeclaredField(
056                                    CacheManager.class, "cacheManagerTimer");
057    
058                            cacheManagerTimerField.set(_cacheManager, null);
059                    }
060                    catch (Exception e) {
061                            throw new RuntimeException(e);
062                    }
063    
064                    if (PropsValues.EHCACHE_PORTAL_CACHE_MANAGER_JMX_ENABLED) {
065                            _managementService = new ManagementService(
066                                    _cacheManager, _mBeanServer, _registerCacheManager,
067                                    _registerCaches, _registerCacheConfigurations,
068                                    _registerCacheStatistics);
069    
070                            _managementService.init();
071                    }
072            }
073    
074            public void clearAll() {
075                    _cacheManager.clearAll();
076            }
077    
078            public void destroy() throws Exception {
079                    try {
080                            _cacheManager.shutdown();
081                    }
082                    finally {
083                            if (_managementService != null) {
084                                    _managementService.dispose();
085                            }
086                    }
087            }
088    
089            public PortalCache getCache(String name) {
090                    return getCache(name, false);
091            }
092    
093            public PortalCache getCache(String name, boolean blocking) {
094                    Ehcache cache = _cacheManager.getEhcache(name);
095    
096                    if (cache == null) {
097                            synchronized (_cacheManager) {
098                                    cache = _cacheManager.getEhcache(name);
099    
100                                    if (cache == null) {
101                                            try {
102                                                    _cacheManager.addCache(name);
103                                            }
104                                            catch (ObjectExistsException oee) {
105    
106                                                    // LEP-7122
107    
108                                            }
109    
110                                            cache = _cacheManager.getEhcache(name);
111    
112                                            cache.setStatisticsEnabled(
113                                                    PropsValues.EHCACHE_STATISTICS_ENABLED);
114    
115                                            if (_log.isInfoEnabled()) {
116                                                    _log.info(
117                                                            "Cache name " + name + " is using implementation " +
118                                                                    cache.getClass().getName());
119                                            }
120                                    }
121                            }
122                    }
123    
124                    PortalCache portalCache = new EhcachePortalCache(cache);
125    
126                    portalCache.setDebug(_debug);
127    
128                    if (PropsValues.EHCACHE_BLOCKING_CACHE_ALLOWED && blocking) {
129                            portalCache = new BlockingPortalCache(portalCache);
130                    }
131    
132                    return portalCache;
133            }
134    
135            public CacheManager getEhcacheManager() {
136                    return _cacheManager;
137            }
138    
139            public void removeCache(String name) {
140                    _cacheManager.removeCache(name);
141            }
142    
143            public void setConfigPropertyKey(String configPropertyKey) {
144                    _configPropertyKey = configPropertyKey;
145            }
146    
147            public void setDebug(boolean debug) {
148                    _debug = debug;
149            }
150    
151            public void setMBeanServer(MBeanServer mBeanServer) {
152                    _mBeanServer = mBeanServer;
153            }
154    
155            public void setRegisterCacheConfigurations(
156                    boolean registerCacheConfigurations) {
157    
158                    _registerCacheConfigurations = registerCacheConfigurations;
159            }
160    
161            public void setRegisterCacheManager(boolean registerCacheManager) {
162                    _registerCacheManager = registerCacheManager;
163            }
164    
165            public void setRegisterCaches(boolean registerCaches) {
166                    _registerCaches = registerCaches;
167            }
168    
169            public void setRegisterCacheStatistics(boolean registerCacheStatistics) {
170                    _registerCacheStatistics = registerCacheStatistics;
171            }
172    
173            private static Log _log = LogFactoryUtil.getLog(
174                    EhcachePortalCacheManager.class);
175    
176            private String _configPropertyKey;
177            private CacheManager _cacheManager;
178            private boolean _debug;
179            private ManagementService _managementService;
180            private MBeanServer _mBeanServer;
181            private boolean _registerCacheManager = true;
182            private boolean _registerCaches = true;
183            private boolean _registerCacheConfigurations = true;
184            private boolean _registerCacheStatistics = true;
185    
186    }