01: package ri.cache.loader;
02:
03: import javax.cache.spi.CacheLoader;
04: import javax.cache.spi.CacheLoaderException;
05: import java.io.*;
06:
07: /**
08: * SerializedFileLoader
09: *
10: * @author Brian Goetz
11: */
12: public class SerializedFileLoader extends
13: AbstractCacheLoader<String, Object> implements
14: CacheLoader<String, Object> {
15: private final File rootDirectory;
16:
17: public SerializedFileLoader(File rootDirectory) {
18: this .rootDirectory = rootDirectory;
19: }
20:
21: public SerializedFileLoader(String rootDirectory) {
22: this (new File(rootDirectory));
23: }
24:
25: public Object load(String key) throws CacheLoaderException {
26: File f = new File(rootDirectory, (String) key);
27: try {
28: InputStream is = new FileInputStream(f);
29: try {
30: ObjectInputStream ois = new ObjectInputStream(is);
31: try {
32: return ois.readObject();
33: } finally {
34: ois.close();
35: }
36: } catch (IOException e) {
37: throw new CacheLoaderException(
38: "IOException reading file "
39: + f.getAbsolutePath(), e);
40: } catch (ClassNotFoundException e) {
41: throw new CacheLoaderException(
42: "ClassNotFoundException deserializing file "
43: + f.getAbsolutePath(), e);
44: } finally {
45: is.close();
46: }
47: } catch (IOException e) {
48: throw new CacheLoaderException("IOException reading file "
49: + f.getAbsolutePath(), e);
50: }
51: }
52: }
|