1   /**
2    *  Copyright 2003-2007 Greg Luck
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.jcache;
18  
19  import net.sf.jsr107cache.CacheLoader;
20  import net.sf.jsr107cache.CacheException;
21  
22  import java.util.Random;
23  import java.util.Map;
24  import java.util.Collection;
25  import java.util.HashMap;
26  import java.util.Iterator;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  /**
32   * A cache loader that counts the number of things it has loaded, useful for testing.
33   * Each load has a random delay to introduce some nice threading entropy
34   * @author Greg Luck
35   * @version $Id: CountingCacheLoader.java 512 2007-07-10 09:18:45Z gregluck $
36   */
37  class CountingCacheLoader implements CacheLoader {
38  
39      private static final Log LOG = LogFactory.getLog(CountingCacheLoader.class.getName());
40  
41      private int loadCounter;
42      private int loadAllCounter;
43      private Random random = new Random();
44  
45      /**
46       * loads an object. Application writers should implement this
47       * method to customize the loading of cache object. This method is called
48       * by the caching service when the requested object is not in the cache.
49       * <p/>
50       *
51       * @param key the key identifying the object being loaded
52       * @return The object that is to be stored in the cache.
53       * @throws net.sf.jsr107cache.CacheException
54       *
55       */
56      public Object load(Object key) throws CacheException {
57          try {
58              Thread.sleep(random.nextInt(3) + 1);
59          } catch (InterruptedException e) {
60              LOG.error("Interrupted");
61          }
62          return new Integer(loadCounter++);
63      }
64  
65      /**
66       * loads multiple object. Application writers should implement this
67       * method to customize the loading of cache object. This method is called
68       * by the caching service when the requested object is not in the cache.
69       * <p/>
70       *
71       * @param keys a Collection of keys identifying the objects to be loaded
72       * @return A Map of objects that are to be stored in the cache.
73       * @throws net.sf.jsr107cache.CacheException
74       *
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, new Integer(loadAllCounter++));
87          }
88          return map;
89      }
90  
91  
92      /**
93       * @return
94       */
95      public int getLoadCounter() {
96          return loadCounter;
97      }
98  
99      /**
100      * @return
101      */
102     public int getLoadAllCounter() {
103         return loadAllCounter;
104     }
105 }