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   * Singleton cache Provider plugin for Hibernate 3.2 and ehcache-1.2. New in this provider is support for
32   * non Serializable keys and values. This provider works as a Singleton. No matter how many Hibernate Configurations
33   * you have, only one ehcache CacheManager is used. See EhCacheProvider for a non-singleton implementation.
34   * <p/>
35   * Ehcache-1.2 also has many other features such as cluster support and listeners, which can be used seamlessly simply
36   * by configurion in ehcache.xml.
37   * <p/>
38   * Use <code>hibernate.cache.provider_class=net.sf.ehcache.hibernate.SingletonEhCacheProvider</code> in the Hibernate configuration
39   * to enable this provider for Hibernate's second level cache.
40   * <p/>
41   * Updated for ehcache-1.2. Note this provider requires ehcache-1.2.jar. Make sure ehcache-1.1.jar or earlier
42   * is not in the classpath or it will not work.
43   * <p/>
44   * See http://ehcache.org for documentation on ehcache
45   * <p/>
46   *
47   * @author Greg Luck
48   * @author Emmanuel Bernard
49   * @version $Id: SingletonEhCacheProvider.html 13146 2011-08-01 17:12:39Z oletizi $
50   */
51  @Deprecated
52  public final class SingletonEhCacheProvider extends AbstractEhcacheProvider {
53  
54      /***
55       * The Hibernate system property specifying the location of the ehcache configuration file name.
56       * <p/
57       * If not set, ehcache.xml will be looked for in the root of the classpath.
58       * <p/>
59       * If set to say ehcache-1.xml, ehcache-1.xml will be looked for in the root of the classpath.
60       */
61      public static final String NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME = "net.sf.ehcache.configurationResourceName";
62  
63      private static final Logger LOG = LoggerFactory.getLogger(SingletonEhCacheProvider.class.getName());
64  
65      /***
66       * To be backwardly compatible with a lot of Hibernate code out there, allow multiple starts and stops on the
67       * one singleton CacheManager. Keep a count of references to only stop on when only one reference is held.
68       */
69      private static int referenceCount;
70  
71      private final ProviderMBeanRegistrationHelper mbeanRegistrationHelper = new ProviderMBeanRegistrationHelper();
72  
73      /***
74       * Callback to perform any necessary initialization of the underlying cache implementation
75       * during SessionFactory construction.
76       * <p/>
77       *
78       * @param properties current configuration settings.
79       */
80      public final void start(Properties properties) throws CacheException {
81          String configurationResourceName = null;
82          if (properties != null) {
83              configurationResourceName = (String) properties.get(NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME);
84          }
85          if (configurationResourceName == null || configurationResourceName.length() == 0) {
86              manager = CacheManager.create();
87              referenceCount++;
88          } else {
89              URL url;
90              try {
91                  url = new URL(configurationResourceName);
92              } catch (MalformedURLException e) {
93                  if (!configurationResourceName.startsWith("/")) {
94                      configurationResourceName = "/" + configurationResourceName;
95                          LOG.debug("prepending / to {}. It should be placed in the rootof the classpath rather than in a package.", 
96                                  configurationResourceName);
97                  }
98                  url = loadResource(configurationResourceName);
99              }
100             manager = CacheManager.create(url);
101             referenceCount++;
102         }
103         mbeanRegistrationHelper.registerMBean(manager, properties);
104     }
105     
106     /***
107      * Callback to perform any necessary cleanup of the underlying cache implementation
108      * during SessionFactory.close().
109      */
110     public void stop() {
111         if (manager != null) {
112             referenceCount--;
113             if (referenceCount == 0) {
114                 manager.shutdown();
115             }
116             manager = null;
117         }
118     }
119 }