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  
17  package net.sf.ehcache.hibernate.nonstop;
18  
19  import net.sf.ehcache.constructs.nonstop.NonStopCacheException;
20  
21  import org.hibernate.cache.CacheException;
22  import org.hibernate.cache.EntityRegion;
23  import org.hibernate.cache.access.EntityRegionAccessStrategy;
24  import org.hibernate.cache.access.SoftLock;
25  
26  /***
27   * Implementation of {@link EntityRegionAccessStrategy} that handles {@link NonStopCacheException} using
28   * {@link HibernateNonstopCacheExceptionHandler}
29   *
30   * @author Abhishek Sanoujam
31   *
32   */
33  public class NonstopAwareEntityRegionAccessStrategy implements EntityRegionAccessStrategy {
34  
35      private final EntityRegionAccessStrategy actualStrategy;
36      private final HibernateNonstopCacheExceptionHandler hibernateNonstopExceptionHandler;
37  
38      /***
39       * Constructor accepting the actual {@link EntityRegionAccessStrategy} and the {@link HibernateNonstopCacheExceptionHandler}
40       *
41       * @param actualStrategy
42       * @param hibernateNonstopExceptionHandler
43       */
44      public NonstopAwareEntityRegionAccessStrategy(EntityRegionAccessStrategy actualStrategy,
45              HibernateNonstopCacheExceptionHandler hibernateNonstopExceptionHandler) {
46          this.actualStrategy = actualStrategy;
47          this.hibernateNonstopExceptionHandler = hibernateNonstopExceptionHandler;
48      }
49  
50      /***
51       * {@inheritDoc}
52       *
53       * @see org.hibernate.cache.access.EntityRegionAccessStrategy#getRegion()
54       */
55      public EntityRegion getRegion() {
56          return actualStrategy.getRegion();
57      }
58  
59      /***
60       * {@inheritDoc}
61       *
62       * @see org.hibernate.cache.access.EntityRegionAccessStrategy#afterInsert(java.lang.Object, java.lang.Object, java.lang.Object)
63       */
64      public boolean afterInsert(Object key, Object value, Object version) throws CacheException {
65          try {
66              return actualStrategy.afterInsert(key, value, version);
67          } catch (NonStopCacheException nonStopCacheException) {
68              hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
69              return false;
70          }
71      }
72  
73      /***
74       * {@inheritDoc}
75       *
76       * @see org.hibernate.cache.access.EntityRegionAccessStrategy#afterUpdate(java.lang.Object, java.lang.Object, java.lang.Object,
77       *      java.lang.Object, org.hibernate.cache.access.SoftLock)
78       */
79      public boolean afterUpdate(Object key, Object value, Object currentVersion, Object previousVersion, SoftLock lock)
80              throws CacheException {
81          try {
82              return actualStrategy.afterUpdate(key, value, currentVersion, previousVersion, lock);
83          } catch (NonStopCacheException nonStopCacheException) {
84              hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
85              return false;
86          }
87      }
88  
89      /***
90       * {@inheritDoc}
91       *
92       * @see org.hibernate.cache.access.EntityRegionAccessStrategy#evict(java.lang.Object)
93       */
94      public void evict(Object key) throws CacheException {
95          try {
96              actualStrategy.evict(key);
97          } catch (NonStopCacheException nonStopCacheException) {
98              hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
99          }
100     }
101 
102     /***
103      * {@inheritDoc}
104      *
105      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#evictAll()
106      */
107     public void evictAll() throws CacheException {
108         try {
109             actualStrategy.evictAll();
110         } catch (NonStopCacheException nonStopCacheException) {
111             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
112         }
113     }
114 
115     /***
116      * {@inheritDoc}
117      *
118      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#get(java.lang.Object, long)
119      */
120     public Object get(Object key, long txTimestamp) throws CacheException {
121         try {
122             return actualStrategy.get(key, txTimestamp);
123         } catch (NonStopCacheException nonStopCacheException) {
124             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
125             return null;
126         }
127     }
128 
129     /***
130      * {@inheritDoc}
131      *
132      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#insert(java.lang.Object, java.lang.Object, java.lang.Object)
133      */
134     public boolean insert(Object key, Object value, Object version) throws CacheException {
135         try {
136             return actualStrategy.insert(key, value, version);
137         } catch (NonStopCacheException nonStopCacheException) {
138             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
139             return false;
140         }
141     }
142 
143     /***
144      * {@inheritDoc}
145      *
146      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#lockItem(java.lang.Object, java.lang.Object)
147      */
148     public SoftLock lockItem(Object key, Object version) throws CacheException {
149         try {
150             return actualStrategy.lockItem(key, version);
151         } catch (NonStopCacheException nonStopCacheException) {
152             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
153             return null;
154         }
155     }
156 
157     /***
158      * {@inheritDoc}
159      *
160      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#lockRegion()
161      */
162     public SoftLock lockRegion() throws CacheException {
163         try {
164             return actualStrategy.lockRegion();
165         } catch (NonStopCacheException nonStopCacheException) {
166             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
167             return null;
168         }
169     }
170 
171     /***
172      * {@inheritDoc}
173      *
174      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#putFromLoad(java.lang.Object, java.lang.Object, long, java.lang.Object,
175      *      boolean)
176      */
177     public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride)
178             throws CacheException {
179         try {
180             return actualStrategy.putFromLoad(key, value, txTimestamp, version, minimalPutOverride);
181         } catch (NonStopCacheException nonStopCacheException) {
182             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
183             return false;
184         }
185     }
186 
187     /***
188      * {@inheritDoc}
189      *
190      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#putFromLoad(java.lang.Object, java.lang.Object, long, java.lang.Object)
191      */
192     public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version) throws CacheException {
193         try {
194             return actualStrategy.putFromLoad(key, value, txTimestamp, version);
195         } catch (NonStopCacheException nonStopCacheException) {
196             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
197             return false;
198         }
199     }
200 
201     /***
202      * {@inheritDoc}
203      *
204      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#remove(java.lang.Object)
205      */
206     public void remove(Object key) throws CacheException {
207         try {
208             actualStrategy.remove(key);
209         } catch (NonStopCacheException nonStopCacheException) {
210             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
211         }
212     }
213 
214     /***
215      * {@inheritDoc}
216      *
217      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#removeAll()
218      */
219     public void removeAll() throws CacheException {
220         try {
221             actualStrategy.removeAll();
222         } catch (NonStopCacheException nonStopCacheException) {
223             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
224         }
225     }
226 
227     /***
228      * {@inheritDoc}
229      *
230      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#unlockItem(java.lang.Object, org.hibernate.cache.access.SoftLock)
231      */
232     public void unlockItem(Object key, SoftLock lock) throws CacheException {
233         try {
234             actualStrategy.unlockItem(key, lock);
235         } catch (NonStopCacheException nonStopCacheException) {
236             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
237         }
238     }
239 
240     /***
241      * {@inheritDoc}
242      *
243      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#unlockRegion(org.hibernate.cache.access.SoftLock)
244      */
245     public void unlockRegion(SoftLock lock) throws CacheException {
246         try {
247             actualStrategy.unlockRegion(lock);
248         } catch (NonStopCacheException nonStopCacheException) {
249             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
250         }
251     }
252 
253     /***
254      * {@inheritDoc}
255      *
256      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#update(java.lang.Object, java.lang.Object, java.lang.Object,
257      *      java.lang.Object)
258      */
259     public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) throws CacheException {
260         try {
261             return actualStrategy.update(key, value, currentVersion, previousVersion);
262         } catch (NonStopCacheException nonStopCacheException) {
263             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
264             return false;
265         }
266     }
267 
268 }