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.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.java 2154 2010-04-06 02:45:52Z cdennis $
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       */
59      public Object load(Object key) throws CacheException {
60          try {
61              Thread.sleep(random.nextInt(3) + 1);
62          } catch (InterruptedException e) {
63              LOG.error("Interrupted");
64          }
65          throw new CacheException("Some exception with key " + key);
66      }
67  
68      /***
69       * loads multiple object. Application writers should implement this
70       * method to customize the loading of cache object. This method is called
71       * by the caching service when the requested object is not in the cache.
72       * <p/>
73       *
74       * @param keys a Collection of keys identifying the objects to be loaded
75       * @return A Map of objects that are to be stored in the cache.
76       *
77       */
78  
79      public Map loadAll(Collection keys) throws CacheException {
80          Map map = new HashMap(keys.size());
81          for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
82              Object key = iterator.next();
83              try {
84                  Thread.sleep(random.nextInt(4));
85              } catch (InterruptedException e) {
86                  LOG.error("Interrupted");
87              }
88              map.put(key, Integer.valueOf(loadAllCounter++));
89              throw new CacheException("Some exception with key " + key);
90          }
91  
92          return map;
93      }
94  
95  
96      /***
97       * Load using both a key and an argument.
98       * <p/>
99       * JCache will use the load(key) method where the argument is null.
100      */
101     public Object load(Object key, Object argument) throws CacheException {
102         try {
103             Thread.sleep(random.nextInt(3) + 1);
104         } catch (InterruptedException e) {
105             LOG.error("Interrupted");
106         }
107         throw new CacheException("Some exception with key " + key);
108     }
109 
110     /***
111      * Load using both a key and an argument.
112      * <p/>
113      * JCache will use the loadAll(key) method where the argument is null.
114      */
115     public Map loadAll(Collection keys, Object argument) throws CacheException {
116         throw new CacheException("Some exception with key " + keys.toArray()[0]);
117     }
118 
119 }