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.loader;
18
19
20 import net.sf.ehcache.CacheException;
21
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Random;
27
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31
32 /***
33 * A cache loader that throws exceptions when used
34 * <p/>
35 * Each load has a random delay to introduce some nice threading entropy.
36 *
37 * @author Greg Luck
38 * @version $Id: ExceptionThrowingLoader.html 13146 2011-08-01 17:12:39Z oletizi $
39 */
40 public class ExceptionThrowingLoader extends CountingCacheLoader {
41
42 private static final Logger LOG = LoggerFactory.getLogger(ExceptionThrowingLoader.class.getName());
43
44 private int loadCounter;
45 private int loadAllCounter;
46 private Random random = new Random();
47 private String name = "ExceptionThrowingLoader";
48
49 /***
50 * loads an object. Application writers should implement this
51 * method to customize the loading of cache object. This method is called
52 * by the caching service when the requested object is not in the cache.
53 * <p/>
54 *
55 * @param key the key identifying the object being loaded
56 * @return The object that is to be stored in the cache.
57 */
58 public Object load(Object key) throws CacheException {
59 try {
60 Thread.sleep(random.nextInt(3) + 1);
61 } catch (InterruptedException e) {
62 LOG.error("Interrupted");
63 }
64 throw new CacheException("Some exception with key " + key);
65 }
66
67 /***
68 * loads multiple object. Application writers should implement this
69 * method to customize the loading of cache object. This method is called
70 * by the caching service when the requested object is not in the cache.
71 * <p/>
72 *
73 * @param keys a Collection of keys identifying the objects to be loaded
74 * @return A Map of objects that are to be stored in the cache.
75 */
76
77 public Map loadAll(Collection keys) throws CacheException {
78 Map map = new HashMap(keys.size());
79 for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
80 Object key = iterator.next();
81 try {
82 Thread.sleep(random.nextInt(4));
83 } catch (InterruptedException e) {
84 LOG.error("Interrupted");
85 }
86 map.put(key, Integer.valueOf(loadAllCounter++));
87 throw new CacheException("Some exception with key " + key);
88 }
89
90 return map;
91 }
92
93
94 /***
95 * Load using both a key and an argument.
96 * <p/>
97 * JCache will use the load(key) method where the argument is null.
98 */
99 public Object load(Object key, Object argument) throws CacheException {
100 try {
101 Thread.sleep(random.nextInt(3) + 1);
102 } catch (InterruptedException e) {
103 LOG.error("Interrupted");
104 }
105 throw new CacheException("Some exception with key " + key);
106 }
107
108 /***
109 * Load using both a key and an argument.
110 * <p/>
111 * JCache will use the loadAll(key) method where the argument is null.
112 */
113 public Map loadAll(Collection keys, Object argument) throws CacheException {
114 throw new CacheException("Some exception with key " + keys.toArray()[0]);
115 }
116
117 }