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.dao.orm.hibernate;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.util.ReflectionUtil;
019    
020    import java.lang.reflect.Field;
021    
022    import java.util.Properties;
023    
024    import net.sf.ehcache.CacheManager;
025    import net.sf.ehcache.util.FailSafeTimer;
026    
027    import org.hibernate.cache.CacheException;
028    import org.hibernate.cache.CacheProvider;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     * @author Shuyang Zhou
033     */
034    @SuppressWarnings("deprecation")
035    public class EhCacheProvider extends CacheProviderWrapper {
036    
037            public static CacheManager getCacheManager() throws SystemException {
038                    try {
039                            Class<?> clazz = Class.forName(
040                                    "net.sf.ehcache.hibernate.AbstractEhcacheProvider");
041    
042                            Field field = clazz.getDeclaredField("manager");
043    
044                            field.setAccessible(true);
045    
046                            CacheManager cacheManager = (CacheManager)field.get(
047                                    _cacheProvider);
048    
049                            if (cacheManager == null) {
050                                    throw new SystemException("CacheManager has been initialized");
051                            }
052    
053                            return cacheManager;
054                    }
055                    catch (Exception e) {
056                            throw new SystemException(e);
057                    }
058            }
059    
060            public EhCacheProvider() {
061                    super("net.sf.ehcache.hibernate.EhCacheProvider");
062    
063                    _cacheProvider = cacheProvider;
064            }
065    
066            public void start(Properties properties) throws CacheException {
067                    super.start(properties);
068    
069                    try {
070                            CacheManager cacheManager = getCacheManager();
071    
072                            FailSafeTimer failSafeTimer = cacheManager.getTimer();
073    
074                            failSafeTimer.cancel();
075    
076                            Field cacheManagerTimerField = ReflectionUtil.getDeclaredField(
077                                    CacheManager.class, "cacheManagerTimer");
078    
079                            cacheManagerTimerField.set(cacheManager, null);
080                    }
081                    catch (Exception e) {
082                            throw new CacheException(e);
083                    }
084            }
085    
086            private static CacheProvider _cacheProvider;
087    
088    }