/**
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 *
 * Copyright (C)
 * <a href="http://www.inab.org">Spanish National Institute of Bioinformatics (INB)</a>
 * <a href="http://www.bsc.es">Barcelona Supercomputing Center (BSC)</a>
 * <a href="http://inb.bsc.es">Computational Node 6</a>
 */

package org.inb.biomoby.central.cache;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;

/**
 * Simple cache store definition based on file (located in a $HOME/.MobyCentralCache/ directory)
 * 
 * @author Dmitry Repchevsky
 */

public class MobyCentralFileCache implements IMobyCentralCache
{
    private File temp_dir;

    public MobyCentralFileCache()
    {
      String path = System.getProperty("user.home");

      path = path + File.separatorChar + ".MobyCentralCache" + File.separatorChar;

      temp_dir = new File(path);
      temp_dir.mkdirs();
    }

    public Reader getReader(URL url) throws IOException
    {
        File file = new File(temp_dir, url.getPath());

        if (!file.exists())
        {
            return null;
        }
        
        return new FileReader(file);
    }

    public Writer getWriter(URL url) throws IOException
    {
        File file = new File(temp_dir, url.getPath());

        return new FileWriter(file);
    }
}
