1 /***
2 * Copyright 2003-2008 Luck Consulting Pty Ltd
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 net.sf.ehcache.CacheManager;
19 import net.sf.ehcache.util.ClassLoaderUtil;
20
21
22 import org.hibernate.cache.Cache;
23 import org.hibernate.cache.CacheException;
24 import org.hibernate.cache.CacheProvider;
25 import org.hibernate.cache.Timestamper;
26
27 import java.net.URL;
28 import java.util.Properties;
29 import java.util.logging.Logger;
30 import java.util.logging.Level;
31
32 /***
33 * Cache Provider plugin for Hibernate 3.2 and ehcache-1.2. New in this provider are ehcache support for multiple
34 * Hibernate session factories, each with its own ehcache configuration, and non Serializable keys and values.
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.EhCacheProvider</code> in the Hibernate configuration
39 * to enable this provider for Hibernate's second level cache.
40 * <p/>
41 * When configuring multiple ehcache CacheManagers, as you would where you have multiple Hibernate Configurations and
42 * multiple SessionFactories, specify in each Hibernate configuration the ehcache configuration using
43 * the property <code>net.sf.ehcache.configurationResourceName</code> An example to set an ehcach configuration
44 * called ehcache-2.xml would be <code>net.sf.ehcache.configurationResourceName=/ehcache-2.xml</code>. If the leading
45 * slash is not there one will be added. The configuration file will be looked for in the root of the classpath.
46 * <p/>
47 * Updated for ehcache-1.2. Note this provider requires ehcache-1.2.jar. Make sure ehcache-1.1.jar or earlier
48 * is not in the classpath or it will not work.
49 * <p/>
50 * See http://ehcache.sf.net for documentation on ehcache
51 * <p/>
52 *
53 * @author Greg Luck
54 * @author Emmanuel Bernard
55 * @version $Id: EhCacheProvider.java 744 2008-08-16 20:10:49Z gregluck $
56 */
57 public final class EhCacheProvider implements CacheProvider {
58
59 /***
60 * The Hibernate system property specifying the location of the ehcache configuration file name.
61 * <p/
62 * If not set, ehcache.xml will be looked for in the root of the classpath.
63 * <p/>
64 * If set to say ehcache-1.xml, ehcache-1.xml will be looked for in the root of the classpath.
65 */
66 public static final String NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME = "net.sf.ehcache.configurationResourceName";
67
68 private static final Logger LOG = Logger.getLogger(EhCacheProvider.class.getName());
69
70 private CacheManager manager;
71
72
73 /***
74 * Builds a Cache.
75 * <p/>
76 * Even though this method provides properties, they are not used.
77 * Properties for EHCache are specified in the ehcache.xml file.
78 * Configuration will be read from ehcache.xml for a cache declaration
79 * where the name attribute matches the name parameter in this builder.
80 *
81 * @param name the name of the cache. Must match a cache configured in ehcache.xml
82 * @param properties not used
83 * @return a newly built cache will be built and initialised
84 * @throws org.hibernate.cache.CacheException
85 * inter alia, if a cache of the same name already exists
86 */
87 public final Cache buildCache(String name, Properties properties) throws CacheException {
88 try {
89 net.sf.ehcache.Ehcache cache = manager.getEhcache(name);
90 if (cache == null) {
91 LOG.warning("Could not find a specific ehcache configuration for cache named [" + name + "]; using defaults.");
92 manager.addCache(name);
93 cache = manager.getEhcache(name);
94 EhCacheProvider.LOG.fine("started EHCache region: " + name);
95 }
96 return new net.sf.ehcache.hibernate.EhCache(cache);
97 } catch (net.sf.ehcache.CacheException e) {
98 throw new CacheException(e);
99 }
100 }
101
102 /***
103 * Returns the next timestamp.
104 */
105 public final long nextTimestamp() {
106 return Timestamper.next();
107 }
108
109 /***
110 * Callback to perform any necessary initialization of the underlying cache implementation
111 * during SessionFactory construction.
112 * <p/>
113 *
114 * @param properties current configuration settings.
115 */
116 public final void start(Properties properties) throws CacheException {
117 if (manager != null) {
118 LOG.warning("Attempt to restart an already started EhCacheProvider. Use sessionFactory.close() " +
119 " between repeated calls to buildSessionFactory. Using previously created EhCacheProvider." +
120 " If this behaviour is required, consider using SingletonEhCacheProvider.");
121 return;
122 }
123 try {
124 String configurationResourceName = null;
125 if (properties != null) {
126 configurationResourceName = (String) properties.get(NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME);
127 }
128 if (configurationResourceName == null || configurationResourceName.length() == 0) {
129 manager = new CacheManager();
130 } else {
131 URL url = loadResource(configurationResourceName);
132 manager = new CacheManager(url);
133 }
134 } catch (net.sf.ehcache.CacheException e) {
135 if (e.getMessage().startsWith("Cannot parseConfiguration CacheManager. Attempt to create a new instance of " +
136 "CacheManager using the diskStorePath")) {
137 throw new CacheException("Attempt to restart an already started EhCacheProvider. Use sessionFactory.close() " +
138 " between repeated calls to buildSessionFactory. Consider using SingletonEhCacheProvider. Error from " +
139 " ehcache was: " + e.getMessage());
140 } else {
141 throw e;
142 }
143 }
144 }
145
146
147 private URL loadResource(String configurationResourceName) {
148 ClassLoader standardClassloader = ClassLoaderUtil.getStandardClassLoader();
149 URL url = null;
150 if (standardClassloader != null) {
151 url = standardClassloader.getResource(configurationResourceName);
152 }
153 if (url == null) {
154 url = this.getClass().getResource(configurationResourceName);
155 }
156 if (LOG.isLoggable(Level.FINE)) {
157 LOG.fine("Creating EhCacheProvider from a specified resource: "
158 + configurationResourceName + " Resolved to URL: " + url);
159 }
160 if (url == null) {
161 if (LOG.isLoggable(Level.WARNING)) {
162 LOG.warning("A configurationResourceName was set to " + configurationResourceName +
163 " but the resource could not be loaded from the classpath." +
164 "Ehcache will configure itself using defaults.");
165 }
166 }
167 return url;
168 }
169
170 /***
171 * Callback to perform any necessary cleanup of the underlying cache implementation
172 * during SessionFactory.close().
173 */
174 public final void stop() {
175 if (manager != null) {
176 manager.shutdown();
177 manager = null;
178 }
179 }
180
181
182 /***
183 * Not sure what this is supposed to do.
184 *
185 * @return false to be safe
186 */
187 public final boolean isMinimalPutsEnabledByDefault() {
188 return false;
189 }
190
191 }
192