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 * Cache Provider plugin for Hibernate 3.2 and ehcache-1.2. New in this provider are ehcache support for multiple
31 * Hibernate session factories, each with its own ehcache configuration, and non Serializable keys and values.
32 * Ehcache-1.2 also has many other features such as cluster support and listeners, which can be used seamlessly simply
33 * by configurion in ehcache.xml.
34 * <p/>
35 * Use <code>hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider</code> in the Hibernate configuration
36 * to enable this provider for Hibernate's second level cache.
37 * <p/>
38 * When configuring multiple ehcache CacheManagers, as you would where you have multiple Hibernate Configurations and
39 * multiple SessionFactories, specify in each Hibernate configuration the ehcache configuration using
40 * the property <code>net.sf.ehcache.configurationResourceName</code> An example to set an ehcach configuration
41 * called ehcache-2.xml would be <code>net.sf.ehcache.configurationResourceName=/ehcache-2.xml</code>. If the leading
42 * slash is not there one will be added. The configuration file will be looked for in the root of the classpath.
43 * <p/>
44 * Updated for ehcache-1.2. Note this provider requires ehcache-1.2.jar. Make sure ehcache-1.1.jar or earlier
45 * is not in the classpath or it will not work.
46 * <p/>
47 * See http://ehcache.org for documentation on ehcache
48 * <p/>
49 *
50 * @author Greg Luck
51 * @author Emmanuel Bernard
52 * @version $Id: EhCacheProvider.java 1770 2010-01-27 18:59:56Z cdennis $
53 */
54 @Deprecated
55 public final class EhCacheProvider extends AbstractEhcacheProvider {
56
57 /***
58 * The Hibernate system property specifying the location of the ehcache configuration file name.
59 * <p/>
60 * If not set, ehcache.xml will be looked for in the root of the classpath.
61 * <p/>
62 * If set to say ehcache-1.xml, ehcache-1.xml will be looked for in the root of the classpath.
63 */
64 public static final String NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME = "net.sf.ehcache.configurationResourceName";
65
66 private static final Logger LOG = LoggerFactory.getLogger(EhCacheProvider.class.getName());
67
68 private final ProviderMBeanRegistrationHelper mbeanRegistrationHelper = new ProviderMBeanRegistrationHelper();
69
70 /***
71 * Callback to perform any necessary initialization of the underlying cache implementation
72 * during SessionFactory construction.
73 * <p/>
74 *
75 * @param properties current configuration settings.
76 */
77 public final void start(Properties properties) throws CacheException {
78 if (manager != null) {
79 LOG.warn("Attempt to restart an already started EhCacheProvider. Use sessionFactory.close() " +
80 " between repeated calls to buildSessionFactory. Using previously created EhCacheProvider." +
81 " If this behaviour is required, consider using SingletonEhCacheProvider.");
82 return;
83 }
84 try {
85 String configurationResourceName = null;
86 if (properties != null) {
87 configurationResourceName = (String) properties.get(NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME);
88 }
89 if (configurationResourceName == null || configurationResourceName.length() == 0) {
90 manager = new CacheManager();
91 } else {
92 URL url = loadResource(configurationResourceName);
93 manager = new CacheManager(HibernateUtil.loadAndCorrectConfiguration(url));
94 }
95 mbeanRegistrationHelper.registerMBean(manager, properties);
96 } catch (net.sf.ehcache.CacheException e) {
97 if (e.getMessage().startsWith("Cannot parseConfiguration CacheManager. Attempt to create a new instance of " +
98 "CacheManager using the diskStorePath")) {
99 throw new CacheException("Attempt to restart an already started EhCacheProvider. Use sessionFactory.close() " +
100 " between repeated calls to buildSessionFactory. Consider using SingletonEhCacheProvider. Error from " +
101 " ehcache was: " + e.getMessage());
102 } else {
103 throw e;
104 }
105 }
106 }
107
108 /***
109 * Callback to perform any necessary cleanup of the underlying cache implementation
110 * during SessionFactory.close().
111 */
112 public final void stop() {
113 if (manager != null) {
114 manager.shutdown();
115 manager = null;
116 }
117 }
118 }
119