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