package ri.cache.loader;
import javax.cache.spi.CacheLoader;
import javax.cache.spi.CacheLoaderException;
import java.io.*;
/**
* SerializedFileLoader
*
* @author Brian Goetz
*/
public class SerializedFileLoader extends AbstractCacheLoader<String, Object> implements CacheLoader<String, Object> {
private final File rootDirectory;
public SerializedFileLoader(File rootDirectory) {
this.rootDirectory = rootDirectory;
}
public SerializedFileLoader(String rootDirectory) {
this(new File(rootDirectory));
}
public Object load(String key) throws CacheLoaderException {
File f = new File(rootDirectory, (String) key);
try {
InputStream is = new FileInputStream(f);
try {
ObjectInputStream ois = new ObjectInputStream(is);
try {
return ois.readObject();
} finally {
ois.close();
}
}
catch (IOException e) {
throw new CacheLoaderException("IOException reading file " + f.getAbsolutePath(), e);
} catch (ClassNotFoundException e) {
throw new CacheLoaderException("ClassNotFoundException deserializing file " + f.getAbsolutePath(), e);
} finally {
is.close();
}
}
catch (IOException e) {
throw new CacheLoaderException("IOException reading file " + f.getAbsolutePath(), e);
}
}
}
|