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.exceptionhandler;
18
19 /***
20 * Copyright 2003-2010 Terracotta, Inc.
21 *
22 * Licensed under the Apache License, Version 2.0 (the "License");
23 * you may not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
25 *
26 * http://www.apache.org/licenses/LICENSE-2.0
27 *
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
33 */
34
35 import net.sf.ehcache.CacheException;
36 import net.sf.ehcache.Ehcache;
37 import net.sf.ehcache.Status;
38 import net.sf.ehcache.loader.CacheLoader;
39
40 import java.util.Collection;
41 import java.util.Map;
42 import java.util.Random;
43
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47
48 /***
49 * A cache loader that throws a custom exception on load
50 * <p/>
51 * <p/>
52 * Used for testing exception handlers
53 *
54 * @author Greg Luck
55 * @version $Id: CustomExceptionThrowingLoader.html 13146 2011-08-01 17:12:39Z oletizi $
56 */
57 public class CustomExceptionThrowingLoader implements CacheLoader {
58
59 private static final Logger LOG = LoggerFactory.getLogger(CustomExceptionThrowingLoader.class.getName());
60
61 private Random random = new Random();
62 private String name = "CustomExceptionThrowingLoader";
63
64 /***
65 * loads an object. Application writers should implement this
66 * method to customize the loading of cache object. This method is called
67 * by the caching service when the requested object is not in the cache.
68 * <p/>
69 *
70 * @param key the key identifying the object being loaded
71 * @return The object that is to be stored in the cache.
72 * @throws UnsupportedOperationException
73 */
74 public Object load(Object key) {
75 try {
76 Thread.sleep(random.nextInt(3) + 1);
77 } catch (InterruptedException e) {
78 LOG.error("Interrupted");
79 }
80 throw new UnsupportedOperationException("load not supported by CustomExceptionThrowingLoader");
81 }
82
83 /***
84 * loads multiple object. Application writers should implement this
85 * method to customize the loading of cache object. This method is called
86 * by the caching service when the requested object is not in the cache.
87 * <p/>
88 *
89 * @param keys a Collection of keys identifying the objects to be loaded
90 * @return A Map of objects that are to be stored in the cache.
91 * @throws UnsupportedOperationException
92 */
93
94 public Map loadAll(Collection keys) {
95
96 try {
97 Thread.sleep(random.nextInt(4));
98 } catch (InterruptedException e) {
99 LOG.error("Interrupted");
100 }
101 throw new UnsupportedOperationException("loadAll not supported by CustomExceptionThrowingLoader");
102
103 }
104
105 /***
106 * Load using both a key and an argument.
107 * <p/>
108 * JCache will use the load(key) method where the argument is null.
109 *
110 * @param key
111 * @param argument
112 * @return
113 * @throws UnsupportedOperationException
114 */
115 public Object load(Object key, Object argument) throws CacheException {
116
117 try {
118 Thread.sleep(random.nextInt(3) + 1);
119 } catch (InterruptedException e) {
120 LOG.error("Interrupted");
121 }
122 throw new UnsupportedOperationException("2-arg load not supported by CustomExceptionThrowingLoader");
123 }
124
125 /***
126 * Load using both a key and an argument.
127 * <p/>
128 * JCache will use the loadAll(key) method where the argument is null.
129 *
130 * @param keys
131 * @param argument
132 * @return
133 * @throws UnsupportedOperationException
134 */
135 public Map loadAll(Collection keys, Object argument) throws CacheException {
136 try {
137 Thread.sleep(random.nextInt(3) + 1);
138 } catch (InterruptedException e) {
139 LOG.error("Interrupted");
140 }
141 throw new UnsupportedOperationException("2-arg loadAll not supported by CustomExceptionThrowingLoader");
142 }
143
144 /***
145 * Gets the name of a CacheLoader
146 *
147 * @return
148 */
149 public String getName() {
150 return name;
151 }
152
153 /***
154 * Creates a clone of this extension. This method will only be called by ehcache before a
155 * cache is initialized.
156 * <p/>
157 * Implementations should throw CloneNotSupportedException if they do not support clone
158 * but that will stop them from being used with defaultCache.
159 *
160 * @return a clone
161 * @throws CloneNotSupportedException if the extension could not be cloned.
162 */
163 public CacheLoader clone(Ehcache cache) throws CloneNotSupportedException {
164 return null;
165 }
166
167 /***
168 * Notifies providers to initialise themselves.
169 * <p/>
170 * This method is called during the Cache's initialise method after it has changed it's
171 * status to alive. Cache operations are legal in this method.
172 *
173 * @throws net.sf.ehcache.CacheException
174 */
175 public void init() {
176
177 }
178
179 /***
180 * Providers may be doing all sorts of exotic things and need to be able to clean up on
181 * dispose.
182 * <p/>
183 * Cache operations are illegal when this method is called. The cache itself is partly
184 * disposed when this method is called.
185 *
186 * @throws net.sf.ehcache.CacheException
187 */
188 public void dispose() throws net.sf.ehcache.CacheException {
189
190 }
191
192 /***
193 * @return the status of the extension
194 */
195 public Status getStatus() {
196 return null;
197 }
198
199 }
200
201