View Javadoc

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.EhcacheEntityRegion;
19  
20  import org.hibernate.cache.CacheException;
21  import org.hibernate.cache.EntityRegion;
22  import org.hibernate.cache.access.EntityRegionAccessStrategy;
23  import org.hibernate.cache.access.SoftLock;
24  import org.hibernate.cfg.Settings;
25  
26  /***
27   * Ehcache specific read-only entity region access strategy
28   *
29   * @author Chris Dennis
30   */
31  public class ReadOnlyEhcacheEntityRegionAccessStrategy extends AbstractEhcacheAccessStrategy<EhcacheEntityRegion>
32          implements EntityRegionAccessStrategy {
33      
34      /***
35       * Create a read-only access strategy accessing the given entity region.
36       */
37      public ReadOnlyEhcacheEntityRegionAccessStrategy(EhcacheEntityRegion region, Settings settings) {
38          super(region, settings);
39      }
40  
41      /***
42       * {@inheritDoc}
43       */
44      public EntityRegion 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          //throw new UnsupportedOperationException("Can't write to a readonly object");
82      }
83  
84      /***
85       * This cache is asynchronous hence a no-op
86       */
87      public boolean insert(Object key, Object value, Object version) throws CacheException {
88          return false;
89      }
90  
91      /***
92       * {@inheritDoc}
93       */
94      public boolean afterInsert(Object key, Object value, Object version) throws CacheException {
95          region.put(key, value);
96          return true;
97      }
98  
99      /***
100      * Throws UnsupportedOperationException since this cache is read-only
101      *
102      * @throws UnsupportedOperationException always
103      */
104     public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) throws UnsupportedOperationException {
105         throw new UnsupportedOperationException("Can't write to a readonly object");
106     }
107 
108     /***
109      * Throws UnsupportedOperationException since this cache is read-only
110      *
111      * @throws UnsupportedOperationException always
112      */
113     public boolean afterUpdate(Object key, Object value, Object currentVersion, Object previousVersion, SoftLock lock)
114             throws UnsupportedOperationException {
115         throw new UnsupportedOperationException("Can't write to a readonly object");
116     }
117 }