SerializedFileLoader.java :  » 6.0-JDK-Modules » jsr107 » ri » cache » loader » Java Open Source

Java Open Source » 6.0 JDK Modules » jsr107 
jsr107 » ri » cache » loader » SerializedFileLoader.java
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);
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.