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.CollectionRegion;
23  import org.hibernate.cache.access.CollectionRegionAccessStrategy;
24  import org.hibernate.cache.access.SoftLock;
25  
26  /***
27   * Implementation of {@link CollectionRegionAccessStrategy} that handles {@link NonStopCacheException} using
28   * {@link HibernateNonstopCacheExceptionHandler}
29   *
30   * @author Abhishek Sanoujam
31   *
32   */
33  public class NonstopAwareCollectionRegionAccessStrategy implements CollectionRegionAccessStrategy {
34  
35      private final CollectionRegionAccessStrategy actualStrategy;
36      private final HibernateNonstopCacheExceptionHandler hibernateNonstopExceptionHandler;
37  
38      /***
39       * Constructor accepting the actual {@link CollectionRegionAccessStrategy} and the {@link HibernateNonstopCacheExceptionHandler}
40       *
41       * @param actualStrategy
42       * @param hibernateNonstopExceptionHandler
43       */
44      public NonstopAwareCollectionRegionAccessStrategy(CollectionRegionAccessStrategy 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 CollectionRegion getRegion() {
56          return actualStrategy.getRegion();
57      }
58  
59      /***
60       * {@inheritDoc}
61       *
62       * @see org.hibernate.cache.access.EntityRegionAccessStrategy#evict(java.lang.Object)
63       */
64      public void evict(Object key) throws CacheException {
65          try {
66              actualStrategy.evict(key);
67          } catch (NonStopCacheException nonStopCacheException) {
68              hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
69          }
70      }
71  
72      /***
73       * {@inheritDoc}
74       *
75       * @see org.hibernate.cache.access.EntityRegionAccessStrategy#evictAll()
76       */
77      public void evictAll() throws CacheException {
78          try {
79              actualStrategy.evictAll();
80          } catch (NonStopCacheException nonStopCacheException) {
81              hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
82          }
83      }
84  
85      /***
86       * {@inheritDoc}
87       *
88       * @see org.hibernate.cache.access.EntityRegionAccessStrategy#get(java.lang.Object, long)
89       */
90      public Object get(Object key, long txTimestamp) throws CacheException {
91          try {
92              return actualStrategy.get(key, txTimestamp);
93          } catch (NonStopCacheException nonStopCacheException) {
94              hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
95              return null;
96          }
97      }
98  
99      /***
100      * {@inheritDoc}
101      *
102      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#lockItem(java.lang.Object, java.lang.Object)
103      */
104     public SoftLock lockItem(Object key, Object version) throws CacheException {
105         try {
106             return actualStrategy.lockItem(key, version);
107         } catch (NonStopCacheException nonStopCacheException) {
108             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
109             return null;
110         }
111     }
112 
113     /***
114      * {@inheritDoc}
115      *
116      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#lockRegion()
117      */
118     public SoftLock lockRegion() throws CacheException {
119         try {
120             return actualStrategy.lockRegion();
121         } catch (NonStopCacheException nonStopCacheException) {
122             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
123             return null;
124         }
125     }
126 
127     /***
128      * {@inheritDoc}
129      *
130      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#putFromLoad(java.lang.Object, java.lang.Object, long, java.lang.Object,
131      *      boolean)
132      */
133     public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride)
134             throws CacheException {
135         try {
136             return actualStrategy.putFromLoad(key, value, txTimestamp, version, minimalPutOverride);
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#putFromLoad(java.lang.Object, java.lang.Object, long, java.lang.Object)
147      */
148     public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version) throws CacheException {
149         try {
150             return actualStrategy.putFromLoad(key, value, txTimestamp, version);
151         } catch (NonStopCacheException nonStopCacheException) {
152             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
153             return false;
154         }
155     }
156 
157     /***
158      * {@inheritDoc}
159      *
160      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#remove(java.lang.Object)
161      */
162     public void remove(Object key) throws CacheException {
163         try {
164             actualStrategy.remove(key);
165         } catch (NonStopCacheException nonStopCacheException) {
166             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
167         }
168     }
169 
170     /***
171      * {@inheritDoc}
172      *
173      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#removeAll()
174      */
175     public void removeAll() throws CacheException {
176         try {
177             actualStrategy.removeAll();
178         } catch (NonStopCacheException nonStopCacheException) {
179             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
180         }
181     }
182 
183     /***
184      * {@inheritDoc}
185      *
186      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#unlockItem(java.lang.Object, org.hibernate.cache.access.SoftLock)
187      */
188     public void unlockItem(Object key, SoftLock lock) throws CacheException {
189         try {
190             actualStrategy.unlockItem(key, lock);
191         } catch (NonStopCacheException nonStopCacheException) {
192             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
193         }
194     }
195 
196     /***
197      * {@inheritDoc}
198      *
199      * @see org.hibernate.cache.access.EntityRegionAccessStrategy#unlockRegion(org.hibernate.cache.access.SoftLock)
200      */
201     public void unlockRegion(SoftLock lock) throws CacheException {
202         try {
203             actualStrategy.unlockRegion(lock);
204         } catch (NonStopCacheException nonStopCacheException) {
205             hibernateNonstopExceptionHandler.handleNonstopCacheException(nonStopCacheException);
206         }
207     }
208 
209 }