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.URL;
19 import java.util.Properties;
20
21 import net.sf.ehcache.CacheManager;
22 import net.sf.ehcache.util.ClassLoaderUtil;
23 import net.sf.ehcache.util.Timestamper;
24
25 import org.hibernate.cache.Cache;
26 import org.hibernate.cache.CacheException;
27 import org.hibernate.cache.CacheProvider;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /***
32 * Abstract super-class for all Ehcache Hibernate CacheProvider implementations.
33 *
34 * @author Chris Dennis
35 * @author Greg Luck
36 * @author Emmanuel Bernard
37 */
38 abstract class AbstractEhcacheProvider implements CacheProvider {
39
40 private static final Logger LOG = LoggerFactory.getLogger(AbstractEhcacheProvider.class.getName());
41
42 /***
43 * CacheManager instance that creates/builds Cache instances for this provider.
44 */
45 protected volatile CacheManager manager;
46
47 /***
48 * Builds a Cache.
49 * <p/>
50 * Even though this method provides properties, they are not used.
51 * Properties for EHCache are specified in the ehcache.xml file.
52 * Configuration will be read from ehcache.xml for a cache declaration
53 * where the name attribute matches the name parameter in this builder.
54 *
55 * @param name the name of the cache. Must match a cache configured in ehcache.xml
56 * @param properties not used
57 * @return a newly built cache will be built and initialised
58 * @throws org.hibernate.cache.CacheException
59 * inter alia, if a cache of the same name already exists
60 */
61 public final Cache buildCache(String name, Properties properties) throws CacheException {
62 try {
63 net.sf.ehcache.Ehcache cache = manager.getEhcache(name);
64 if (cache == null) {
65 LOG.warn("Could not find a specific ehcache configuration for cache named [" + name + "]; using defaults.");
66 manager.addCache(name);
67 cache = manager.getEhcache(name);
68 LOG.debug("started EHCache region: " + name);
69 }
70 HibernateUtil.validateEhcache(cache);
71 return new EhCache(cache);
72 } catch (net.sf.ehcache.CacheException e) {
73 throw new CacheException(e);
74 }
75 }
76
77 /***
78 * Returns the next timestamp.
79 */
80 public final long nextTimestamp() {
81 return Timestamper.next();
82 }
83
84 /***
85 * Whether to optimize for minimals puts or minimal gets.
86 * <p>
87 * Indicates whether when operating in non-strict read/write or read-only mode
88 * Hibernate should optimize the access patterns for minimal puts or minimal gets.
89 * In Ehcache we default to minimal puts since this should have minimal to no
90 * affect on unclustered users, and has great benefit for clustered users.
91 * <p>
92 * This setting can be overridden by setting the "hibernate.cache.use_minimal_puts"
93 * property in the Hibernate configuration.
94 *
95 * @return true, optimize for minimal puts
96 */
97 public final boolean isMinimalPutsEnabledByDefault() {
98 return true;
99 }
100
101 /***
102 * Load the supplied resource from the classpath.
103 */
104 protected URL loadResource(String configurationResourceName) {
105 ClassLoader standardClassloader = ClassLoaderUtil.getStandardClassLoader();
106 URL url = null;
107 if (standardClassloader != null) {
108 url = standardClassloader.getResource(configurationResourceName);
109 }
110 if (url == null) {
111 url = this.getClass().getResource(configurationResourceName);
112 }
113
114 LOG.debug("Creating EhCacheProvider from a specified resource: {}. Resolved to URL: ", configurationResourceName, url);
115 if (url == null) {
116 LOG.warn("A configurationResourceName was set to {} but the resource could not be loaded from the classpath." +
117 "Ehcache will configure itself using defaults.", configurationResourceName);
118 }
119 return url;
120 }
121 }