// UserDB that uses Property files

import java.util.*;
import java.io.*;

public class UserDBProp extends UserDB {
    protected Properties prop;
    public boolean create(String password) throws IOException {
	FileInputStream istream;
	try {
	    istream  = new FileInputStream(
             dir + "/" + user + ".properties");
  	    istream.close();
	}
	catch (FileNotFoundException e) {
	    // ok to create
	    prop=new Properties();
	    prop.put("password",password);
	    save();
	    return true;
	}
	catch (IOException e) {
	    return false;  // some error?
	}
	return false; // can't create -- already exists
    }
    public boolean load(String password) {
	FileInputStream istream;
        	prop=new Properties();
	try {
	    istream  = new FileInputStream(
             dir + "/" + user + ".properties");
	    prop.load(istream);
	    istream.close();
	}
	catch (IOException e) {
	    prop=null;
	    return false;
	}
	// check password
	if (!password.equals(prop.getProperty("password"))) {
	    prop=null;
	    return false;
	}
	return true;
    }

    public void save() throws IOException {
	FileOutputStream os=new FileOutputStream(
	  dir + "/" + user + ".properties");
	// use save if JDK<1.2
	prop.store(os,"WebProperties");
	os.close();
    }

    public String getProperty(String key, String deflt) {
	if (prop==null) return deflt;
	String rv=prop.getProperty(key);
	if (rv==null || rv.equals("")) rv=deflt;
	return rv;
    }
    public void writeProperty(String key, String val) {
        if (prop==null) return;
	dirty=true;
	prop.put(key,val);
    }
}

