FileDocCategorySizeDatePackage
XYZPayrollPermission.javaAPI DocExample3651Sun Oct 25 18:13:34 GMT 1998None

XYZPayrollPermission.java

/*
 *
 * Copyright (c) 1998 Scott Oaks. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and
 * without fee is hereby granted.
 *
 * This sample source code is provided for example only,
 * on an unsupported, as-is basis. 
 *
 * AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. AUTHOR SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
 * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
 * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
 * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
 * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
 * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
 * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  AUTHOR
 * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
 * HIGH RISK ACTIVITIES.
 */


import java.security.*;
import java.util.StringTokenizer;
import java.io.FilePermission;

public class XYZPayrollPermission extends Permission {

	protected int mask;
	static private int VIEW = 0x01;
	static private int UPDATE = 0x02;

	public XYZPayrollPermission(String name) {
		this(name, "view");
	}

	public XYZPayrollPermission(String name, String action) {
		super(name);
		if (action == null)
			action = "view";
		parse(action);
	}

	private void parse(String action) {
		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);
		}
	}

	public boolean implies(Permission permission) {
		if (!(permission instanceof XYZPayrollPermission))
			return false;
		
		XYZPayrollPermission p = (XYZPayrollPermission) permission;
		String name = getName();
		if (!name.equals("*") && !name.equals(p.getName()))
			return false;
		if ((mask & p.mask) != p.mask)
			return false;
		return true;
	}

	public boolean equals(Object o) {
		if (!(o instanceof XYZPayrollPermission))
			return false;
		
		XYZPayrollPermission p = (XYZPayrollPermission) o;
		return ((p.getName().equals(getName())) && (p.mask == mask));
	}

	public int hashCode() {
		return getName().hashCode() ^ mask;
	}

	public String getActions() {
		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 PermissionCollection newPermissionsCollection() {
		return new XYZPayrollPermissionCollection();
	}

	public static void main(String args[]) {
		XYZPayrollPermission sdo = new XYZPayrollPermission("sdo", "view");
		XYZPayrollPermission sdoAll = new XYZPayrollPermission("sdo", "view, update");
		XYZPayrollPermission su = new XYZPayrollPermission("*", "view, update");

		System.out.println("sdo -> sdoAll " + sdo.implies(sdoAll));
		System.out.println("sdoAll -> sdo " + sdoAll.implies(sdo));
		System.out.println("sdoAll -> su " + sdoAll.implies(su));
		System.out.println("su -> sdo " + su.implies(sdo));
	}
}