propertypermission.java

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

JAVA
637
字号
	    sb.append("read");	}	if ((mask & WRITE) == WRITE) {	    if (comma) sb.append(',');    	    else comma = true;	    sb.append("write");	}	return sb.toString();    }    /**     * Returns the "canonical string representation" of the actions.     * That is, this method always returns present actions in the following order:     * read, write. For example, if this PropertyPermission object     * allows both write and read actions, a call to <code>getActions</code>     * will return the string "read,write".     *     * @return the canonical string representation of the actions.     */    public String getActions()    {	if (actions == null)	    actions = getActions(this.mask);	return actions;    }    /**     * Return the current action mask.     * Used by the PropertyPermissionCollection     *     * @return the actions mask.     */    int getMask() {	return mask;    }    /**     * Returns a new PermissionCollection object for storing     * PropertyPermission objects.     * <p>     *     * @return a new PermissionCollection object suitable for storing     * PropertyPermissions.     */    public PermissionCollection newPermissionCollection() {	return new PropertyPermissionCollection();    }    /**     * WriteObject is called to save the state of the PropertyPermission     * to a stream. The actions are serialized, and the superclass     * takes care of the name.     */    private synchronized void writeObject(java.io.ObjectOutputStream s)        throws IOException    {	// Write out the actions. The superclass takes care of the name	// call getActions to make sure actions field is initialized	if (actions == null)	    getActions();	s.defaultWriteObject();    }    /**     * readObject is called to restore the state of the PropertyPermission from     * a stream.     */    private synchronized void readObject(java.io.ObjectInputStream s)         throws IOException, ClassNotFoundException    {	// Read in the action, then initialize the rest	s.defaultReadObject();	init(getMask(actions));    }}/** * A PropertyPermissionCollection stores a set of PropertyPermission * permissions. * * @see java.security.Permission * @see java.security.Permissions * @see java.security.PermissionCollection * * @version 1.30, 01/23/03 * * @author Roland Schemers * * @serial include */final class PropertyPermissionCollection extends PermissionCollectionimplements Serializable{    /**     * Key is property name; value is PropertyPermission.     * Not serialized; see serialization section at end of class.     */    private transient Map perms;    /**     * Boolean saying if "*" is in the collection.     *     * @see #serialPersistentFields     */    private boolean all_allowed;    /**     * Create an empty PropertyPermissions object.     *     */    public PropertyPermissionCollection() {	perms = new HashMap(32);     // Capacity for default policy	all_allowed = false;    }    /**     * Adds a permission to the PropertyPermissions. The key for the hash is     * the name.     *     * @param permission the Permission object to add.     *     * @exception IllegalArgumentException - if the permission is not a     *                                       PropertyPermission     *     * @exception SecurityException - if this PropertyPermissionCollection     *                                object has been marked readonly     */    public void add(Permission permission)    {	if (! (permission instanceof PropertyPermission))	    throw new IllegalArgumentException("invalid permission: "+					       permission);	if (isReadOnly())	    throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");	PropertyPermission pp = (PropertyPermission) permission;	PropertyPermission existing =	    (PropertyPermission) perms.get(pp.getName());        // No need to synchronize because all adds are done sequentially	// before any implies() calls	if (existing != null) {	    int oldMask = existing.getMask();	    int newMask = pp.getMask();	    if (oldMask != newMask) {		int effective = oldMask | newMask;		String actions = PropertyPermission.getActions(effective);		perms.put(pp.getName(),			new PropertyPermission(pp.getName(), actions));	    }	} else {	    perms.put(pp.getName(), permission);	}        if (!all_allowed) {	    if (pp.getName().equals("*"))		all_allowed = true;	}    }    /**     * Check and see if this set of permissions implies the permissions     * expressed in "permission".     *     * @param p the Permission object to compare     *     * @return true if "permission" is a proper subset of a permission in     * the set, false if not.     */    public boolean implies(Permission permission)    {	if (! (permission instanceof PropertyPermission))   		return false;	PropertyPermission pp = (PropertyPermission) permission;	PropertyPermission x;	int desired = pp.getMask();	int effective = 0;	// short circuit if the "*" Permission was added	if (all_allowed) {	    x = (PropertyPermission) perms.get("*");	    if (x != null) {		effective |= x.getMask();		if ((effective & desired) == desired)		    return true;	    }	}	// strategy:	// Check for full match first. Then work our way up the	// name looking for matches on a.b.*	String name = pp.getName();	//System.out.println("check "+name);	x = (PropertyPermission) perms.get(name);	if (x != null) {	    // we have a direct hit!	    effective |= x.getMask();	    if ((effective & desired) == desired)		return true;	}	// work our way up the tree...	int last, offset;	offset = name.length()-1;	while ((last = name.lastIndexOf(".", offset)) != -1) {	    name = name.substring(0, last+1) + "*";	    //System.out.println("check "+name);	    x = (PropertyPermission) perms.get(name);	    if (x != null) {		effective |= x.getMask();		if ((effective & desired) == desired)		    return true;	    }	    offset = last -1;	}	// we don't have to check for "*" as it was already checked	// at the top (all_allowed), so we just return false	return false;    }    /**     * Returns an enumeration of all the PropertyPermission objects in the     * container.     *     * @return an enumeration of all the PropertyPermission objects.     */    public Enumeration elements() {        // Convert Iterator of Map values into an Enumeration	return Collections.enumeration(perms.values());    }    private static final long serialVersionUID = 7015263904581634791L;    // Need to maintain serialization interoperability with earlier releases,    // which had the serializable field:    //    // Table of permissions.    //    // @serial    //    // private Hashtable permissions;    /**     * @serialField permissions java.util.Hashtable     *     A table of the PropertyPermissions.     * @serialField all_allowed boolean     *     boolean saying if "*" is in the collection.     */    private static final ObjectStreamField[] serialPersistentFields = {        new ObjectStreamField("permissions", Hashtable.class),	new ObjectStreamField("all_allowed", Boolean.TYPE),    };    /**     * @serialData Default fields.     */    /*     * Writes the contents of the perms field out as a Hashtable for     * serialization compatibility with earlier releases. all_allowed     * unchanged.     */    private void writeObject(ObjectOutputStream out) throws IOException {	// Don't call out.defaultWriteObject()	// Copy perms into a Hashtable	Hashtable permissions = new Hashtable(perms.size()*2);	permissions.putAll(perms);	// Write out serializable fields        ObjectOutputStream.PutField pfields = out.putFields();	pfields.put("all_allowed", all_allowed);        pfields.put("permissions", permissions);        out.writeFields();    }    /*     * Reads in a Hashtable of PropertyPermissions and saves them in the      * perms field. Reads in all_allowed.     */    private void readObject(ObjectInputStream in) throws IOException,     ClassNotFoundException {	// Don't call defaultReadObject()	// Read in serialized fields	ObjectInputStream.GetField gfields = in.readFields();	// Get all_allowed	all_allowed = gfields.get("all_allowed", false);	// Get permissions	Hashtable permissions = (Hashtable)gfields.get("permissions", null);	perms = new HashMap(permissions.size()*2);	perms.putAll(permissions);    }}

⌨️ 快捷键说明

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