import java.io.*;
import java.security.AccessController;

public class PasswordSecurityManager extends SecurityManager {

    private String password;
    private BufferedReader buffy;

    public PasswordSecurityManager(String p, BufferedReader b) {
        super();
        this.password = p;
        this.buffy = b;
    }

    private boolean accessOK() {
        int c;
        String response;

        System.out.println("What's the secret password?");
        try {
            response = buffy.readLine();
            if (response.equals(password))
                return true;
            else
                return false;
        } catch (IOException e) {
            return false;
        }
    }

    public void checkRead(String filename) {
//Mention file by name so don't get prompted for password 
//for everything the application loads to create itself

        if((filename.equals(File.separatorChar + 
			"home" + File.separatorChar + 
			"monicap" + File.separatorChar + "text2.txt"))){
         if(!accessOK()){
            super.checkRead(filename);
            throw new SecurityException("No Way!");
         } else {
            FilePermission perm = new FilePermission(File.separatorChar + 
				"home" + File.separatorChar + 
				"monicap" + File.separatorChar + 
				"text2.txt", "read");
            checkPermission(perm);
         }
        }
    }

    public void checkWrite(String filename) {
//Mention file by name so don't get prompted for password 
//for everything the application loads to create itself
        if((filename.equals(File.separatorChar + "home" + 
				File.separatorChar + "monicap"
+ File.separatorChar + "text.txt"))){
         if(!accessOK()){
            super.checkWrite(filename);
            throw new SecurityException("No Way!");
	 } else {
            FilePermission perm = new FilePermission(File.separatorChar + 
				"home" + File.separatorChar + "monicap" + 
				File.separatorChar + "text.txt" , "write");
	    checkPermission(perm);
         }
        }
    }
}
