propertypermission.java

来自「java源代码 请看看啊 提点宝贵的意见」· Java 代码 · 共 637 行 · 第 1/2 页

JAVA
637
字号
/* * @(#)PropertyPermission.java	1.30 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.util;import java.io.Serializable;import java.io.IOException;import java.security.*;import java.util.Map;import java.util.HashMap;import java.util.Enumeration;import java.util.Hashtable;import java.util.Collections;import java.io.ObjectStreamField;import java.io.ObjectOutputStream;import java.io.ObjectInputStream;import java.io.IOException;import sun.security.util.SecurityConstants;/** * This class is for property permissions. * * <P> * The name is the name of the property ("java.home", * "os.name", etc). The naming * convention follows the  hierarchical property naming convention. * Also, an asterisk * may appear at the end of the name, following a ".", or by itself, to * signify a wildcard match. For example: "java.*" or "*" is valid, * "*java" or "a*b" is not valid. * <P> * <P> * The actions to be granted are passed to the constructor in a string containing * a list of zero or more comma-separated keywords. The possible keywords are * "read" and "write". Their meaning is defined as follows: * <P> * <DL> *    <DT> read *    <DD> read permission. Allows <code>System.getProperty</code> to *         be called. *    <DT> write *    <DD> write permission. Allows <code>System.setProperty</code> to *         be called. * </DL> * <P> * The actions string is converted to lowercase before processing. * <P> * Care should be taken before granting code permission to access * certain system properties.  For example, granting permission to * access the "java.home" system property gives potentially malevolent * code sensitive information about the system environment (the Java * installation directory).  Also, granting permission to access * the "user.name" and "user.home" system properties gives potentially * malevolent code sensitive information about the user environment * (the user's account name and home directory). * * @see java.security.BasicPermission * @see java.security.Permission * @see java.security.Permissions * @see java.security.PermissionCollection * @see java.lang.SecurityManager * * @version 1.30 03/01/23 * * @author Roland Schemers * @since 1.2 * * @serial exclude */public final class PropertyPermission extends BasicPermission {    /**     * Read action.     */    private final static int READ    = 0x1;    /**     * Write action.     */    private final static int WRITE   = 0x2;    /**     * All actions (read,write);     */    private final static int ALL     = READ|WRITE;    /**     * No actions.     */    private final static int NONE    = 0x0;    /**     * The actions mask.     *     */    private transient int mask;    /**     * The actions string.     *     * @serial      */    private String actions; // Left null as long as possible, then                            // created and re-used in the getAction function.    /**     * initialize a PropertyPermission object. Common to all constructors.     * Also called during de-serialization.     *     * @param mask the actions mask to use.     *     */    private void init(int mask)    {	if ((mask & ALL) != mask)		throw new IllegalArgumentException("invalid actions mask");	if (mask == NONE)		throw new IllegalArgumentException("invalid actions mask");	if (getName() == null)		throw new NullPointerException("name can't be null");	this.mask = mask;    }    /**     * Creates a new PropertyPermission object with the specified name.     * The name is the name of the system property, and     * <i>actions</i> contains a comma-separated list of the     * desired actions granted on the property. Possible actions are     * "read" and "write".     *     * @param name the name of the PropertyPermission.     * @param actions the actions string.     */    public PropertyPermission(String name, String actions)    {	super(name,actions);	init(getMask(actions));    }    /**     * Checks if this PropertyPermission object "implies" the specified     * permission.     * <P>     * More specifically, this method returns true if:<p>     * <ul>     * <li> <i>p</i> is an instanceof PropertyPermission,<p>     * <li> <i>p</i>'s actions are a subset of this     * object's actions, and <p>     * <li> <i>p</i>'s name is implied by this object's     *      name. For example, "java.*" implies "java.home".     * </ul>     * @param p the permission to check against.     *     * @return true if the specified permission is implied by this object,     * false if not.     */    public boolean implies(Permission p) {	if (!(p instanceof PropertyPermission))	    return false;	PropertyPermission that = (PropertyPermission) p;	// we get the effective mask. i.e., the "and" of this and that.	// They must be equal to that.mask for implies to return true.	return ((this.mask & that.mask) == that.mask) && super.implies(that);    }    /**     * Checks two PropertyPermission objects for equality. Checks that <i>obj</i> is     * a PropertyPermission, and has the same name and actions as this object.     * <P>     * @param obj the object we are testing for equality with this object.     * @return true if obj is a PropertyPermission, and has the same name and     * actions as this PropertyPermission object.     */    public boolean equals(Object obj) {	if (obj == this)	    return true;	if (! (obj instanceof PropertyPermission))	    return false;	PropertyPermission that = (PropertyPermission) obj;	return (this.mask == that.mask) &&	    (this.getName().equals(that.getName()));    }    /**     * Returns the hash code value for this object.     * The hash code used is the hash code of this permissions name, that is,     * <code>getName().hashCode()</code>, where <code>getName</code> is     * from the Permission superclass.     *     * @return a hash code value for this object.     */    public int hashCode() {	return this.getName().hashCode();    }    /**     * Converts an actions String to an actions mask.     *     * @param action the action string.     * @return the actions mask.     */    private static int getMask(String actions) {	int mask = NONE;	if (actions == null) {	    return mask;	}	// Check against use of constants (used heavily within the JDK)	if (actions == SecurityConstants.PROPERTY_READ_ACTION) {	    return READ;	} if (actions == SecurityConstants.PROPERTY_WRITE_ACTION) {	    return WRITE;	} else if (actions == SecurityConstants.PROPERTY_RW_ACTION) {	    return READ|WRITE;	}	char[] a = actions.toCharArray();	int i = a.length - 1;	if (i < 0)	    return mask;	while (i != -1) {	    char c;	    // skip whitespace	    while ((i!=-1) && ((c = a[i]) == ' ' ||			       c == '\r' ||			       c == '\n' ||			       c == '\f' ||			       c == '\t'))		i--;	    // check for the known strings	    int matchlen;	    if (i >= 3 && (a[i-3] == 'r' || a[i-3] == 'R') &&			  (a[i-2] == 'e' || a[i-2] == 'E') &&			  (a[i-1] == 'a' || a[i-1] == 'A') &&			  (a[i] == 'd' || a[i] == 'D'))	    {		matchlen = 4;		mask |= READ;	    } else if (i >= 4 && (a[i-4] == 'w' || a[i-4] == 'W') &&				 (a[i-3] == 'r' || a[i-3] == 'R') &&				 (a[i-2] == 'i' || a[i-2] == 'I') &&				 (a[i-1] == 't' || a[i-1] == 'T') &&				 (a[i] == 'e' || a[i] == 'E'))	    {		matchlen = 5;		mask |= WRITE;	    } else {		// parse error		throw new IllegalArgumentException(			"invalid permission: " + actions);	    }	    // make sure we didn't just match the tail of a word	    // like "ackbarfaccept".  Also, skip to the comma.	    boolean seencomma = false;	    while (i >= matchlen && !seencomma) {		switch(a[i-matchlen]) {		case ',':		    seencomma = true;		    /*FALLTHROUGH*/		case ' ': case '\r': case '\n':		case '\f': case '\t':		    break;		default:		    throw new IllegalArgumentException(			    "invalid permission: " + actions);		}		i--;	    }	    // point i at the location of the comma minus one (or -1).	    i -= matchlen;	}	return mask;    }    /**     * Return the canonical string representation of the actions.     * Always returns present actions in the following order:     * read, write.     *     * @return the canonical string representation of the actions.     */    static String getActions(int mask)    {	StringBuffer sb = new StringBuffer();        boolean comma = false;	if ((mask & READ) == READ) {	    comma = true;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?