View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.ehcache.hibernate;
17  
18  import java.net.MalformedURLException;
19  import java.net.URL;
20  import java.util.Properties;
21  
22  import net.sf.ehcache.CacheManager;
23  import net.sf.ehcache.hibernate.management.impl.ProviderMBeanRegistrationHelper;
24  
25  import org.hibernate.cache.CacheException;
26  
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /***
31   * Cache Provider plugin for Hibernate 3.2 and ehcache-1.2. New in this provider are ehcache support for multiple
32   * Hibernate session factories, each with its own ehcache configuration, and non Serializable keys and values.
33   * Ehcache-1.2 also has many other features such as cluster support and listeners, which can be used seamlessly simply
34   * by configurion in ehcache.xml.
35   * <p/>
36   * Use <code>hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider</code> in the Hibernate configuration
37   * to enable this provider for Hibernate's second level cache.
38   * <p/>
39   * When configuring multiple ehcache CacheManagers, as you would where you have multiple Hibernate Configurations and
40   * multiple SessionFactories, specify in each Hibernate configuration the ehcache configuration using
41   * the property <code>net.sf.ehcache.configurationResourceName</code> An example to set an ehcach configuration
42   * called ehcache-2.xml would be <code>net.sf.ehcache.configurationResourceName=/ehcache-2.xml</code>. If the leading
43   * slash is not there one will be added. The configuration file will be looked for in the root of the classpath.
44   * <p/>
45   * Updated for ehcache-1.2. Note this provider requires ehcache-1.2.jar. Make sure ehcache-1.1.jar or earlier
46   * is not in the classpath or it will not work.
47   * <p/>
48   * See http://ehcache.org for documentation on ehcache
49   * <p/>
50   *
51   * @author Greg Luck
52   * @author Emmanuel Bernard
53   * @version $Id: EhCacheProvider.java 2334 2010-04-22 14:01:01Z alexsnaps $
54   */
55  @Deprecated
56  public final class EhCacheProvider extends AbstractEhcacheProvider {
57  
58      /***
59       * The Hibernate system property specifying the location of the ehcache configuration file name.
60       * <p/>
61       * If not set, ehcache.xml will be looked for in the root of the classpath.
62       * <p/>
63       * If set to say ehcache-1.xml, ehcache-1.xml will be looked for in the root of the classpath.
64       */
65      public static final String NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME = "net.sf.ehcache.configurationResourceName";
66  
67      private static final Logger LOG = LoggerFactory.getLogger(EhCacheProvider.class.getName());
68  
69      private final ProviderMBeanRegistrationHelper mbeanRegistrationHelper = new ProviderMBeanRegistrationHelper();
70  
71      /***
72       * Callback to perform any necessary initialization of the underlying cache implementation
73       * during SessionFactory construction.
74       * <p/>
75       *
76       * @param properties current configuration settings.
77       */
78      public final void start(Properties properties) throws CacheException {
79          if (manager != null) {
80              LOG.warn("Attempt to restart an already started EhCacheProvider. Use sessionFactory.close() " +
81                      " between repeated calls to buildSessionFactory. Using previously created EhCacheProvider." +
82                      " If this behaviour is required, consider using SingletonEhCacheProvider.");
83              return;
84          }
85          try {
86              String configurationResourceName = null;
87              if (properties != null) {
88                  configurationResourceName = (String) properties.get(NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME);
89              }
90              if (configurationResourceName == null || configurationResourceName.length() == 0) {
91                  manager = new CacheManager();
92              } else {
93                  URL url;
94                  try {
95                      url = new URL(configurationResourceName);
96                  } catch (MalformedURLException e) {
97                      url = loadResource(configurationResourceName);
98                  }
99                  manager = new CacheManager(HibernateUtil.loadAndCorrectConfiguration(url));
100             }
101             mbeanRegistrationHelper.registerMBean(manager, properties);
102         } catch (net.sf.ehcache.CacheException e) {
103             if (e.getMessage().startsWith("Cannot parseConfiguration CacheManager. Attempt to create a new instance of " +
104                     "CacheManager using the diskStorePath")) {
105                 throw new CacheException("Attempt to restart an already started EhCacheProvider. Use sessionFactory.close() " +
106                         " between repeated calls to buildSessionFactory. Consider using SingletonEhCacheProvider. Error from " +
107                         " ehcache was: " + e.getMessage());
108             } else {
109                 throw e;
110             }
111         }
112     }
113 
114     /***
115      * Callback to perform any necessary cleanup of the underlying cache implementation
116      * during SessionFactory.close().
117      */
118     public final void stop() {
119         if (manager != null) {
120             manager.shutdown();
121             manager = null;
122         }
123     }
124 }
125