001: /**
002: * Copyright 2003-2007 Luck Consulting Pty Ltd
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package net.sf.ehcache.hibernate;
016:
017: import net.sf.ehcache.CacheManager;
018: import net.sf.ehcache.util.ClassLoaderUtil;
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.hibernate.cache.Cache;
022: import org.hibernate.cache.CacheException;
023: import org.hibernate.cache.CacheProvider;
024: import org.hibernate.cache.Timestamper;
025:
026: import java.net.URL;
027: import java.util.Properties;
028:
029: /**
030: * Cache Provider plugin for Hibernate 3.2 and ehcache-1.2. New in this provider are ehcache support for multiple
031: * Hibernate session factories, each with its own ehcache configuration, and non Serializable keys and values.
032: * Ehcache-1.2 also has many other features such as cluster support and listeners, which can be used seamlessly simply
033: * by configurion in ehcache.xml.
034: * <p/>
035: * Use <code>hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider</code> in the Hibernate configuration
036: * to enable this provider for Hibernate's second level cache.
037: * <p/>
038: * When configuring multiple ehcache CacheManagers, as you would where you have multiple Hibernate Configurations and
039: * multiple SessionFactories, specify in each Hibernate configuration the ehcache configuration using
040: * the property <code>net.sf.ehcache.configurationResourceName</code> An example to set an ehcach configuration
041: * called ehcache-2.xml would be <code>net.sf.ehcache.configurationResourceName=/ehcache-2.xml</code>. If the leading
042: * slash is not there one will be added. The configuration file will be looked for in the root of the classpath.
043: * <p/>
044: * Updated for ehcache-1.2. Note this provider requires ehcache-1.2.jar. Make sure ehcache-1.1.jar or earlier
045: * is not in the classpath or it will not work.
046: * <p/>
047: * See http://ehcache.sf.net for documentation on ehcache
048: * <p/>
049: *
050: * @author Greg Luck
051: * @author Emmanuel Bernard
052: * @version $Id: EhCacheProvider.java 519 2007-07-27 07:11:45Z gregluck $
053: */
054: public final class EhCacheProvider implements CacheProvider {
055:
056: /**
057: * The Hibernate system property specifying the location of the ehcache configuration file name.
058: * <p/
059: * If not set, ehcache.xml will be looked for in the root of the classpath.
060: * <p/>
061: * If set to say ehcache-1.xml, ehcache-1.xml will be looked for in the root of the classpath.
062: */
063: public static final String NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME = "net.sf.ehcache.configurationResourceName";
064:
065: private static final Log LOG = LogFactory
066: .getLog(EhCacheProvider.class);
067:
068: private CacheManager manager;
069:
070: /**
071: * Builds a Cache.
072: * <p/>
073: * Even though this method provides properties, they are not used.
074: * Properties for EHCache are specified in the ehcache.xml file.
075: * Configuration will be read from ehcache.xml for a cache declaration
076: * where the name attribute matches the name parameter in this builder.
077: *
078: * @param name the name of the cache. Must match a cache configured in ehcache.xml
079: * @param properties not used
080: * @return a newly built cache will be built and initialised
081: * @throws org.hibernate.cache.CacheException
082: * inter alia, if a cache of the same name already exists
083: */
084: public final Cache buildCache(String name, Properties properties)
085: throws CacheException {
086: try {
087: net.sf.ehcache.Ehcache cache = manager.getEhcache(name);
088: if (cache == null) {
089: LOG
090: .warn("Could not find a specific ehcache configuration for cache named ["
091: + name + "]; using defaults.");
092: manager.addCache(name);
093: cache = manager.getEhcache(name);
094: EhCacheProvider.LOG.debug("started EHCache region: "
095: + name);
096: }
097: return new net.sf.ehcache.hibernate.EhCache(cache);
098: } catch (net.sf.ehcache.CacheException e) {
099: throw new CacheException(e);
100: }
101: }
102:
103: /**
104: * Returns the next timestamp.
105: */
106: public final long nextTimestamp() {
107: return Timestamper.next();
108: }
109:
110: /**
111: * Callback to perform any necessary initialization of the underlying cache implementation
112: * during SessionFactory construction.
113: * <p/>
114: *
115: * @param properties current configuration settings.
116: */
117: public final void start(Properties properties)
118: throws CacheException {
119: if (manager != null) {
120: LOG
121: .warn("Attempt to restart an already started EhCacheProvider. Use sessionFactory.close() "
122: + " between repeated calls to buildSessionFactory. Using previously created EhCacheProvider."
123: + " If this behaviour is required, consider using SingletonEhCacheProvider.");
124: return;
125: }
126: try {
127: String configurationResourceName = null;
128: if (properties != null) {
129: configurationResourceName = (String) properties
130: .get(NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME);
131: }
132: if (configurationResourceName == null
133: || configurationResourceName.length() == 0) {
134: manager = new CacheManager();
135: } else {
136: if (!configurationResourceName.startsWith("/")) {
137: configurationResourceName = "/"
138: + configurationResourceName;
139: if (LOG.isDebugEnabled()) {
140: LOG
141: .debug("prepending / to "
142: + configurationResourceName
143: + ". It should be placed in the root"
144: + "of the classpath rather than in a package.");
145: }
146: }
147: URL url = loadResource(configurationResourceName);
148: manager = new CacheManager(url);
149: }
150: } catch (net.sf.ehcache.CacheException e) {
151: if (e.getMessage().startsWith(
152: "Cannot parseConfiguration CacheManager. Attempt to create a new instance of "
153: + "CacheManager using the diskStorePath")) {
154: throw new CacheException(
155: "Attempt to restart an already started EhCacheProvider. Use sessionFactory.close() "
156: + " between repeated calls to buildSessionFactory. Consider using SingletonEhCacheProvider. Error from "
157: + " ehcache was: " + e.getMessage());
158: } else {
159: throw e;
160: }
161: }
162: }
163:
164: private URL loadResource(String configurationResourceName) {
165: ClassLoader standardClassloader = ClassLoaderUtil
166: .getStandardClassLoader();
167: URL url = null;
168: if (standardClassloader != null) {
169: url = standardClassloader
170: .getResource(configurationResourceName);
171: }
172: if (url == null) {
173: url = this .getClass()
174: .getResource(configurationResourceName);
175: }
176: if (LOG.isDebugEnabled()) {
177: LOG
178: .debug("Creating EhCacheProvider from a specified resource: "
179: + configurationResourceName
180: + " Resolved to URL: " + url);
181: }
182: if (url == null) {
183: if (LOG.isWarnEnabled()) {
184: LOG
185: .warn("A configurationResourceName was set to "
186: + configurationResourceName
187: + " but the resource could not be loaded from the classpath."
188: + "Ehcache will configure itself using defaults.");
189: }
190: }
191: return url;
192: }
193:
194: /**
195: * Callback to perform any necessary cleanup of the underlying cache implementation
196: * during SessionFactory.close().
197: */
198: public final void stop() {
199: if (manager != null) {
200: manager.shutdown();
201: manager = null;
202: }
203: }
204:
205: /**
206: * Not sure what this is supposed to do.
207: *
208: * @return false to be safe
209: */
210: public final boolean isMinimalPutsEnabledByDefault() {
211: return false;
212: }
213:
214: }
|