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 net.sf.ehcache.CacheManager;
022    
023    import org.hibernate.cache.CacheProvider;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Shuyang Zhou
028     */
029    @SuppressWarnings("deprecation")
030    public class EhCacheProvider extends CacheProviderWrapper {
031    
032            public static CacheManager getCacheManager() throws SystemException {
033                    try {
034                            Class<?> clazz = Class.forName(
035                                    "net.sf.ehcache.hibernate.AbstractEhcacheProvider");
036    
037                            Field field = clazz.getDeclaredField("manager");
038    
039                            field.setAccessible(true);
040    
041                            CacheManager cacheManager = (CacheManager)field.get(
042                                    _cacheProvider);
043    
044                            if (cacheManager == null) {
045                                    throw new SystemException("CacheManager has been initialized");
046                            }
047    
048                            return cacheManager;
049                    }
050                    catch (Exception e) {
051                            throw new SystemException(e);
052                    }
053            }
054    
055            public EhCacheProvider() {
056                    super("net.sf.ehcache.hibernate.EhCacheProvider");
057    
058                    _cacheProvider = cacheProvider;
059            }
060    
061            private static CacheProvider _cacheProvider;
062    
063    }