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.constructs.nonstop;
18  
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import net.sf.ehcache.CacheException;
28  import net.sf.ehcache.Element;
29  import net.sf.ehcache.Status;
30  import net.sf.ehcache.concurrent.CacheLockProvider;
31  import net.sf.ehcache.search.Attribute;
32  import net.sf.ehcache.search.Results;
33  import net.sf.ehcache.search.attribute.AttributeExtractor;
34  import net.sf.ehcache.store.ElementValueComparator;
35  import net.sf.ehcache.store.Policy;
36  import net.sf.ehcache.store.StoreListener;
37  import net.sf.ehcache.store.StoreQuery;
38  import net.sf.ehcache.store.TerracottaStore;
39  import net.sf.ehcache.writer.CacheWriterManager;
40  
41  /***
42   * All operations in this Store never returns
43   *
44   * @author Abhishek Sanoujam
45   */
46  public class BlockingMockStore implements TerracottaStore {
47  
48      private static final List<String> skipMethods;
49      private final CacheLockProvider cacheLockProvider = new NullCacheLockProvider();
50  
51      static {
52          // list of methods (in Store) which are:
53          // - indirectly used from other methods in Ehcache before reaching the Store layer
54          // - nonstopStore does not delegate these methods to the executorService
55          List<String> skip = new ArrayList<String>();
56          skip.add("bufferFull");
57          skip.add("containsKeyOnDisk");
58          skip.add("containsKeyOffHeap");
59          skip.add("getOffHeapSize");
60          skipMethods = skip;
61      }
62  
63      private volatile boolean blocking = true;
64  
65      public void setBlocking(boolean enabled) {
66          this.blocking = enabled;
67      }
68  
69      private String getPreviousMethodName() {
70          StackTraceElement[] stackTrace = new Exception().fillInStackTrace().getStackTrace();
71          StackTraceElement element = stackTrace[2];
72          return element.getMethodName();
73      }
74  
75      private void neverReturn() {
76          if (blocking && !skipMethods.contains(getPreviousMethodName())) {
77              try {
78                  Thread.currentThread().join();
79              } catch (Exception e) {
80                  throw new CacheException(e);
81              }
82          }
83      }
84  
85      public boolean bufferFull() {
86          neverReturn();
87          return false;
88      }
89  
90      public boolean containsKey(Object key) {
91          neverReturn();
92          return false;
93      }
94  
95      public boolean containsKeyInMemory(Object key) {
96          neverReturn();
97          return false;
98      }
99  
100     public boolean containsKeyOnDisk(Object key) {
101         neverReturn();
102         return false;
103     }
104 
105     public void dispose() {
106         neverReturn();
107     }
108 
109     public void expireElements() {
110         neverReturn();
111     }
112 
113     public void flush() throws IOException {
114         neverReturn();
115     }
116 
117     public Element get(Object key) {
118         neverReturn();
119         return null;
120     }
121 
122     public Policy getInMemoryEvictionPolicy() {
123         neverReturn();
124         return null;
125     }
126 
127     public int getInMemorySize() {
128         neverReturn();
129         return 0;
130     }
131 
132     public long getInMemorySizeInBytes() {
133         neverReturn();
134         return 0;
135     }
136 
137     public Object getInternalContext() {
138         neverReturn();
139         return cacheLockProvider;
140     }
141 
142     public List getKeys() {
143         neverReturn();
144         return null;
145     }
146 
147     public int getOnDiskSize() {
148         neverReturn();
149         return 0;
150     }
151 
152     public long getOnDiskSizeInBytes() {
153         neverReturn();
154         return 0;
155     }
156 
157     public Element getQuiet(Object key) {
158         neverReturn();
159         return null;
160     }
161 
162     public int getSize() {
163         neverReturn();
164         return 0;
165     }
166 
167     public Status getStatus() {
168         neverReturn();
169         return null;
170     }
171 
172     public int getTerracottaClusteredSize() {
173         neverReturn();
174         return 0;
175     }
176 
177     public boolean isCacheCoherent() {
178         neverReturn();
179         return false;
180     }
181 
182     public boolean isClusterCoherent() {
183         neverReturn();
184         return false;
185     }
186 
187     public boolean isNodeCoherent() {
188         neverReturn();
189         return false;
190     }
191 
192     public boolean put(Element element) throws CacheException {
193         neverReturn();
194         return false;
195     }
196 
197     public void putAll(Collection<Element> elements) throws CacheException {
198         neverReturn();
199     }
200 
201     public Element putIfAbsent(Element element) throws NullPointerException {
202         neverReturn();
203         return null;
204     }
205 
206     public boolean putWithWriter(Element element, CacheWriterManager writerManager) throws CacheException {
207         neverReturn();
208         return false;
209     }
210 
211     public Element remove(Object key) {
212         neverReturn();
213         return null;
214     }
215 
216     public void removeAll(Collection<Object> keys) {
217         neverReturn();
218     }
219 
220     public void removeAll() throws CacheException {
221         neverReturn();
222     }
223 
224     public Element removeElement(Element element, ElementValueComparator comparator) throws NullPointerException {
225         neverReturn();
226         return null;
227     }
228 
229     public Element removeWithWriter(Object key, CacheWriterManager writerManager) throws CacheException {
230         neverReturn();
231         return null;
232     }
233 
234     public boolean replace(Element old, Element element, ElementValueComparator comparator) throws NullPointerException,
235             IllegalArgumentException {
236         neverReturn();
237         return false;
238     }
239 
240     public Element replace(Element element) throws NullPointerException {
241         neverReturn();
242         return null;
243     }
244 
245     public void setInMemoryEvictionPolicy(Policy policy) {
246         neverReturn();
247     }
248 
249     public void setNodeCoherent(boolean coherent) throws UnsupportedOperationException {
250         neverReturn();
251     }
252 
253     public void waitUntilClusterCoherent() throws UnsupportedOperationException {
254         neverReturn();
255     }
256 
257     public void addStoreListener(StoreListener listener) {
258         neverReturn();
259     }
260 
261     public void removeStoreListener(StoreListener listener) {
262         neverReturn();
263     }
264 
265     public int getOffHeapSize() {
266         neverReturn();
267         return 0;
268     }
269 
270     public long getOffHeapSizeInBytes() {
271         neverReturn();
272         return 0;
273     }
274 
275     public boolean containsKeyOffHeap(Object key) {
276         neverReturn();
277         return false;
278     }
279 
280     public Object getMBean() {
281         return null;
282     }
283 
284     public void setAttributeExtractors(Map<String, AttributeExtractor> extractors) {
285         // no-op
286     }
287 
288     public Results executeQuery(StoreQuery query) {
289         throw new UnsupportedOperationException();
290     }
291 
292     public <T> Attribute<T> getSearchAttribute(String attributeName) {
293         throw new UnsupportedOperationException();
294     }
295 
296     public Set getLocalKeys() {
297         // should never block
298         return Collections.EMPTY_SET;
299     }
300 
301     public Element unlockedGet(Object key) {
302         neverReturn();
303         return null;
304     }
305 
306     public Element unlockedGetQuiet(Object key) {
307         neverReturn();
308         return null;
309     }
310 
311     public Element unsafeGet(Object key) {
312         // unsafe gets never block
313         return null;
314     }
315 
316     public Element unsafeGetQuiet(Object key) {
317         // unsafe gets never block
318         return null;
319     }
320 
321 }