/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.persistent;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;

import org.jfox.framework.Component;
import org.jfox.framework.ComponentContext;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class FilePersistenter implements Persistenter, Component {

    private String directory = ".";

    public FilePersistenter() {

    }

    public FilePersistenter(String directory) {
        this.directory = directory;
    }

    public synchronized Object load(Object identity) throws Exception {
        if(identity == null) throw new IllegalArgumentException("identity can not be null.");

        Object result = null;
        ObjectInputStream ois = null;
        File file = null;
        try {
            String filename = getFullFileName(identity);
            file = new File(filename);
            ois = new ObjectInputStream(new FileInputStream(file));
            result = ois.readObject();
        }
        catch(Exception e) {
            throw e;
        }
        finally {
            try {
                ois.close();
                if(file != null) {
                    file.delete();
                }
            }
            catch(IOException e) {
            }
        }
        return result;
    }

    // identity is the filename
    public synchronized void store(Object identity, Serializable data) throws Exception {
        if(identity == null || data == null) throw new IllegalArgumentException("identity and data can not be null.");
        String filename = getFullFileName(identity);
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(filename));
            oos.writeObject(data);
        }
        catch(Exception e) {
            throw e;
        }
        finally {
            try {
                oos.close();
            }
            catch(IOException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean remove(Object identity) {
        if(identity == null) throw new IllegalArgumentException("identity can not be null.");
        try {
            String filename = getFullFileName(identity);
            File file = new File(filename);
            return file.delete();
        }
        catch(Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    private String getFullFileName(Object identity) {
        return directory + File.separator + identity.toString() + ".ser";
    }


    public void setComponentContext(ComponentContext ctx) {
    }

    public static void main(String[] args) throws Exception {
        Date date = new Date();
        FilePersistenter fp = new FilePersistenter();
        String identity = "test";
        fp.store(identity, date);
        Date serDate = (Date) fp.load(identity);
        System.out.println(serDate.toString());
    }

}


