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.EhcacheCollectionRegion;
19
20 import org.hibernate.cache.CacheException;
21 import org.hibernate.cache.CollectionRegion;
22 import org.hibernate.cache.access.CollectionRegionAccessStrategy;
23 import org.hibernate.cache.access.SoftLock;
24 import org.hibernate.cfg.Settings;
25
26 /***
27 * Ehcache specific read-only collection region access strategy
28 *
29 * @author Chris Dennis
30 */
31 public class ReadOnlyEhcacheCollectionRegionAccessStrategy extends AbstractEhcacheAccessStrategy<EhcacheCollectionRegion>
32 implements CollectionRegionAccessStrategy {
33
34 /***
35 * Create a read-only access strategy accessing the given collection region.
36 */
37 public ReadOnlyEhcacheCollectionRegionAccessStrategy(EhcacheCollectionRegion region, Settings settings) {
38 super(region, settings);
39 }
40
41 /***
42 * {@inheritDoc}
43 */
44 public CollectionRegion getRegion() {
45 return region;
46 }
47
48 /***
49 * {@inheritDoc}
50 */
51 public Object get(Object key, long txTimestamp) throws CacheException {
52 return region.get(key);
53 }
54
55 /***
56 * {@inheritDoc}
57 */
58 public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride)
59 throws CacheException {
60 if (minimalPutOverride && region.contains(key)) {
61 return false;
62 } else {
63 region.put(key, value);
64 return true;
65 }
66 }
67
68 /***
69 * Throws UnsupportedOperationException since this cache is read-only
70 *
71 * @throws UnsupportedOperationException always
72 */
73 public SoftLock lockItem(Object key, Object version) throws UnsupportedOperationException {
74 throw new UnsupportedOperationException("Can't write to a readonly object");
75 }
76
77 /***
78 * A no-op since this cache is read-only
79 */
80 public void unlockItem(Object key, SoftLock lock) throws CacheException {
81
82 }
83 }