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