/*
 * my Acl implementation class
 *
 *
 * sun.security package is used(It may need change in the future)
 * hide the owner aspect of sun's ACL
 *
 *
 * july 8 change: do not use sun's acl because of no serializable support
 *
 *
 *
 */


package jowadmin;

//import sun.security.acl.*;
//import java.security.*;
//import java.security.acl.*;

import java.util.*;

import jow.*;

public class MyAclImpl extends Object implements java.io.Serializable {

    //public static PrincipalImpl proxyowner=new PrincipalImpl("proxy");

    public String name;
    public Hashtable userentrytable;
    public Hashtable groupentrytable;

    public MyAclImpl(String name) {
        this.name=name;
        userentrytable=new Hashtable();

        groupentrytable=new Hashtable();
    }



    public synchronized void setUserEntry(String username, int accesstype) throws JOWException{


    MyAclEntryImpl oldentry=( MyAclEntryImpl ) userentrytable.get(username);
    MyAclEntryImpl entry=new MyAclEntryImpl(username, new JOWPermissionImpl(accesstype) );
    if (oldentry==null) {
        // if not exist, build a new one
        System.out.println("build new one");
        userentrytable.put(username,entry);
      }
     else {
        // if exist, replace the old one
        System.out.println("replace old one");
        userentrytable.remove(username);
        userentrytable.put(username,entry);
      }
    }

    public synchronized void setGroupEntry(String groupname, int accesstype) throws JOWException{

    MyAclEntryImpl oldentry=( MyAclEntryImpl )  groupentrytable.get(groupname);
    MyAclEntryImpl entry=new MyAclEntryImpl(groupname,new JOWPermissionImpl(accesstype) );
    if (oldentry==null) {
        // if not exist, build a new one
        groupentrytable.put(groupname,entry);
      }
     else {
        // if exist, replace the old one
        groupentrytable.remove(groupname);
        groupentrytable.put(groupname,entry);
      }
    }

    public synchronized void delUserEntry(String username) throws JOWException{
    MyAclEntryImpl oldentry=( MyAclEntryImpl ) userentrytable.get(username);
    if (oldentry==null) {
        // if not exist, throw exception
      }
     else {
        // if exist, replace the old one
        userentrytable.remove(username);
      }



    }
    public synchronized void delGroupEntry(String groupname) throws JOWException{
    // check if there is a entry for this group
    MyAclEntryImpl oldentry=( MyAclEntryImpl )  groupentrytable.get(groupname);
    if (oldentry==null) {
        // if not exist, throw exception
      }
     else {
        // if exist, replace the old one
        groupentrytable.remove(groupname);
      }

    }

    public synchronized JOWPermissionImpl getUserPerm(String username) throws JOWException {
      MyAclEntryImpl wildentry=(MyAclEntryImpl) userentrytable.get(UserInfo.ALLUSER);
      if (wildentry !=null)  {
            // alluser setting
            return wildentry.getPerm();
        }
    MyAclEntryImpl entry=(MyAclEntryImpl ) userentrytable.get(username);
    if (entry==null) return null;
    return entry.getPerm();

    }

    public synchronized JOWPermissionImpl getGroupPerm(String groupname) throws JOWException {
      MyAclEntryImpl entry=(MyAclEntryImpl ) groupentrytable.get(groupname);
      if (entry==null) return null;
      return entry.getPerm();

    }

    public synchronized boolean checkUserPermission(String username, int accesstype) throws JOWException {
       //MyAclEntryImpl entry;
       JOWPermissionImpl perm=getUserPerm(username);
       if (perm==null) return false;
       return perm.check(accesstype);
    }

    public synchronized boolean checkGroupPermission(String groupname, int accesstype) throws JOWException {
         //MyAclEntryImpl entry=getGroupEntry(groupname);
         JOWPermissionImpl perm=getGroupPerm(groupname);
         if (perm==null) return false;
         return perm.check(accesstype);
    }


    public Enumeration listUserEntry() throws JOWException {
        return userentrytable.elements();
    }

    public Enumeration listUser() throws JOWException {
        return userentrytable.keys();

    }

    public Enumeration listGroupEntry() throws JOWException {
        return groupentrytable.elements();
    }

    public Enumeration listGroup() throws JOWException {
        return groupentrytable.keys();
    }

}
