1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.dao.orm.hibernate;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.util.ReflectionUtil;
19  
20  import java.lang.reflect.Field;
21  
22  import java.util.Properties;
23  
24  import net.sf.ehcache.CacheManager;
25  import net.sf.ehcache.util.FailSafeTimer;
26  
27  import org.hibernate.cache.CacheException;
28  import org.hibernate.cache.CacheProvider;
29  
30  /**
31   * <a href="EhCacheProvider.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   * @author Shuyang Zhou
35   */
36  @SuppressWarnings("deprecation")
37  public class EhCacheProvider extends CacheProviderWrapper {
38  
39      public static CacheManager getCacheManager() throws SystemException {
40          try {
41              Class<?> clazz = Class.forName(
42                  "net.sf.ehcache.hibernate.AbstractEhcacheProvider");
43  
44              Field field = clazz.getDeclaredField("manager");
45  
46              field.setAccessible(true);
47  
48              CacheManager cacheManager = (CacheManager)field.get(
49                  _cacheProvider);
50  
51              if (cacheManager == null) {
52                  throw new SystemException("CacheManager has been initialized");
53              }
54  
55              return cacheManager;
56          }
57          catch (Exception e) {
58              throw new SystemException(e);
59          }
60      }
61  
62      public EhCacheProvider() {
63          super("net.sf.ehcache.hibernate.EhCacheProvider");
64  
65          _cacheProvider = cacheProvider;
66      }
67  
68      public void start(Properties properties) throws CacheException {
69          super.start(properties);
70  
71          try {
72              CacheManager cacheManager = getCacheManager();
73  
74              FailSafeTimer failSafeTimer = cacheManager.getTimer();
75  
76              failSafeTimer.cancel();
77  
78              Field cacheManagerTimerField = ReflectionUtil.getDeclaredField(
79                  CacheManager.class, "cacheManagerTimer");
80  
81              cacheManagerTimerField.set(cacheManager, null);
82          }
83          catch (Exception e) {
84              throw new CacheException(e);
85          }
86      }
87  
88      private static CacheProvider _cacheProvider;
89  
90  }