FileDocCategorySizeDatePackage
XYZPayrollPermission.javaAPI DocExample3959Sat Jan 13 12:01:04 GMT 2001javasec.samples.ch05

XYZPayrollPermission

public class XYZPayrollPermission extends Permission

Fields Summary
protected int
mask
private static int
VIEW
private static int
UPDATE
Constructors Summary
public XYZPayrollPermission(String name)


       
        // Our permission must always have an action, so we
        // choose a default one here.
        this(name, "view");
    
public XYZPayrollPermission(String name, String action)

        // Our superclass, however, does not support actions
        // so we don't provide one to that.
        super(name);
        parse(action);
    
Methods Summary
public booleanequals(java.lang.Object o)

        if (!(o instanceof XYZPayrollPermission))
            return false;
        
        // For equality, we check the name and action mask.
        // We must provide a method definition like this, since
        // the security system expects us to do a deep check for
        // equality rather than relying on object reference
        // equality.
        XYZPayrollPermission p = (XYZPayrollPermission) o;
        return ((p.getName().equals(getName())) && (p.mask == mask));
    
public java.lang.StringgetActions()

        // This method must return the same string, no matter how
        // the action list was passed to the constructor.
        if (mask == 0)
            return "";
        else if (mask == VIEW)
            return "view";
        else if (mask == UPDATE)
            return "update";
        else if (mask == (VIEW | UPDATE))
            return "view, update";
        else throw new IllegalArgumentException("Unknown mask");
    
public inthashCode()

        // We must always provide a hash code for permissions,
        // because the hashes must match if the permissions compare
        // as equals. The default implementation of this method
        // wouldn't provide that.
        return getName().hashCode() ^ mask;
    
public booleanimplies(java.security.Permission permission)

        if (!(permission instanceof XYZPayrollPermission))
            return false;
        
        XYZPayrollPermission p = (XYZPayrollPermission) permission;
        String name = getName();
        // The name must be either the wildcard *, which signifies
        // all possible names, or the name must match our name
        if (!name.equals("*") && !name.equals(p.getName()))
            return false;
        // Similarly, the requested actions must all match actions
        // that we've been constructed with.
        if ((mask & p.mask) != p.mask)
            return false;
        // Only if both the action and name match do we return true.
        return true;
    
public static voidmain(java.lang.String[] args)

        XYZPayrollPermission p1 = new XYZPayrollPermission("sdo", "view");
        XYZPayrollPermission p2 = new XYZPayrollPermission(args[0], args[1]);
        System.out.println("P1 is " + p1);
        System.out.println("P2 is " + p2);
        System.out.println("P1 -> P2 is " + p1.implies(p2));
        System.out.println("P2 -> P1 is " + p2.implies(p1));
    
public java.security.PermissionCollectionnewPermissionsCollection()

        // More about this in a later example.
        return new XYZPayrollPermissionCollection();
    
private voidparse(java.lang.String action)

        // Look in the action string for the words view and
        // update, separated by white space or by a comma
        StringTokenizer st = new StringTokenizer(action, ",\t ");

        mask = 0;
        while (st.hasMoreTokens()) {
            String tok = st.nextToken();
            if (tok.equals("view"))
                mask |= VIEW;
            else if (tok.equals("update"))
                mask |= UPDATE;
            else throw new IllegalArgumentException(
                                    "Unknown action " + tok);
        }