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.strategy;
17
18 import net.sf.ehcache.hibernate.regions.EhcacheTransactionalDataRegion;
19 import org.hibernate.cache.CacheException;
20 import org.hibernate.cache.access.SoftLock;
21 import org.hibernate.cfg.Settings;
22
23 /***
24 * Ultimate superclass for all Ehcache specific Hibernate AccessStrategy implementations.
25 *
26 * @param <T> type of the enclosed region
27 *
28 * @author Chris Dennis
29 */
30 abstract class AbstractEhcacheAccessStrategy<T extends EhcacheTransactionalDataRegion> {
31
32 /***
33 * The wrapped Hibernate cache region.
34 */
35 protected final T region;
36 /***
37 * The settings for this persistence unit.
38 */
39 protected final Settings settings;
40
41 /***
42 * Create an access strategy wrapping the given region.
43 */
44 AbstractEhcacheAccessStrategy(T region, Settings settings) {
45 this.region = region;
46 this.settings = settings;
47 }
48
49 /***
50 * This method is a placeholder for method signatures supplied by interfaces pulled in further down the class
51 * hierarchy.
52 *
53 * @see org.hibernate.cache.access.EntityRegionAccessStrategy#putFromLoad(java.lang.Object, java.lang.Object, long, java.lang.Object)
54 * @see org.hibernate.cache.access.CollectionRegionAccessStrategy#putFromLoad(java.lang.Object, java.lang.Object, long, java.lang.Object)
55 */
56 public final boolean putFromLoad(Object key, Object value, long txTimestamp, Object version) throws CacheException {
57 return putFromLoad(key, value, txTimestamp, version, settings.isMinimalPutsEnabled());
58 }
59
60 /***
61 * This method is a placeholder for method signatures supplied by interfaces pulled in further down the class
62 * hierarchy.
63 *
64 * @see org.hibernate.cache.access.EntityRegionAccessStrategy#putFromLoad(java.lang.Object, java.lang.Object, long, java.lang.Object, boolean)
65 * @see org.hibernate.cache.access.CollectionRegionAccessStrategy#putFromLoad(java.lang.Object, java.lang.Object, long, java.lang.Object, boolean)
66 */
67 public abstract boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride)
68 throws CacheException;
69
70 /***
71 * Region locks are not supported.
72 *
73 * @return <code>null</code>
74 *
75 * @see org.hibernate.cache.access.EntityRegionAccessStrategy#lockRegion()
76 * @see org.hibernate.cache.access.CollectionRegionAccessStrategy#lockRegion()
77 */
78 public final SoftLock lockRegion() {
79 return null;
80 }
81
82 /***
83 * Region locks are not supported - perform a cache clear as a precaution.
84 *
85 * @see org.hibernate.cache.access.EntityRegionAccessStrategy#unlockRegion(org.hibernate.cache.access.SoftLock)
86 * @see org.hibernate.cache.access.CollectionRegionAccessStrategy#unlockRegion(org.hibernate.cache.access.SoftLock)
87 */
88 public final void unlockRegion(SoftLock lock) throws CacheException {
89 region.clear();
90 }
91
92 /***
93 * A no-op since this is an asynchronous cache access strategy.
94 *
95 * @see org.hibernate.cache.access.EntityRegionAccessStrategy#remove(java.lang.Object)
96 * @see org.hibernate.cache.access.CollectionRegionAccessStrategy#remove(java.lang.Object)
97 */
98 public void remove(Object key) throws CacheException {
99 }
100
101 /***
102 * Called to evict data from the entire region
103 *
104 * @throws CacheException Propogated from underlying {@link org.hibernate.cache.Region}
105 * @see org.hibernate.cache.access.EntityRegionAccessStrategy#removeAll()
106 * @see org.hibernate.cache.access.CollectionRegionAccessStrategy#removeAll()
107 */
108 public final void removeAll() throws CacheException {
109 region.clear();
110 }
111
112 /***
113 * Remove the given mapping without regard to transactional safety
114 *
115 * @see org.hibernate.cache.access.EntityRegionAccessStrategy#evict(java.lang.Object)
116 * @see org.hibernate.cache.access.CollectionRegionAccessStrategy#evict(java.lang.Object)
117 */
118 public final void evict(Object key) throws CacheException {
119 region.remove(key);
120 }
121
122 /***
123 * Remove all mappings without regard to transactional safety
124 *
125 * @see org.hibernate.cache.access.EntityRegionAccessStrategy#evictAll()
126 * @see org.hibernate.cache.access.CollectionRegionAccessStrategy#evictAll()
127 */
128 public final void evictAll() throws CacheException {
129 region.clear();
130 }
131 }