Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 1997-2003 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 package java.util;
 
 import java.util.Map;
This class is for property permissions.

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.

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:

read
read permission. Allows System.getProperty to be called.
write
write permission. Allows System.setProperty to be called.

The actions string is converted to lowercase before processing.

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).

 
 
 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     = |;
    
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.

Parameters:
mask the actions mask to use.
    private void init(int mask)
    {
        if ((mask & ) != mask)
                throw new IllegalArgumentException("invalid actions mask");
        if (mask == )
                throw new IllegalArgumentException("invalid actions mask");
        if (getName() == null)
                throw new NullPointerException("name can't be null");
        this. = mask;
    }

    
Creates a new PropertyPermission object with the specified name. The name is the name of the system property, and actions contains a comma-separated list of the desired actions granted on the property. Possible actions are "read" and "write".

Parameters:
name the name of the PropertyPermission.
actions the actions string.
Throws:
java.lang.NullPointerException if name is null.
java.lang.IllegalArgumentException if name is empty or if actions is invalid.
    public PropertyPermission(String nameString actions)
    {
        super(name,actions);
        init(getMask(actions));
    }

    
Checks if this PropertyPermission object "implies" the specified permission.

More specifically, this method returns true if:

  • p is an instanceof PropertyPermission,

  • p's actions are a subset of this object's actions, and

  • p's name is implied by this object's name. For example, "java.*" implies "java.home".

Parameters:
p the permission to check against.
Returns:
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 = (PropertyPermissionp;
        // 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. & that.mask) == that.mask) && super.implies(that);
    }


    
Checks two PropertyPermission objects for equality. Checks that obj is a PropertyPermission, and has the same name and actions as this object.

Parameters:
obj the object we are testing for equality with this object.
Returns:
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 = (PropertyPermissionobj;
        return (this. == 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, getName().hashCode(), where getName is from the Permission superclass.

Returns:
a hash code value for this object.
    public int hashCode() {
        return this.getName().hashCode();
    }


    
Converts an actions String to an actions mask.

Parameters:
action the action string.
Returns:
the actions mask.
    private static int getMask(String actions) {
        int mask = ;
        if (actions == null) {
            return mask;
        }
        // Check against use of constants (used heavily within the JDK)
        if (actions == .) {
            return ;
        } if (actions == .) {
            return ;
        } else if (actions == .) {
            return |;
        }
        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 |= ;
            } 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 |= ;
            } 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.

Returns:
the canonical string representation of the actions.
    static String getActions(int mask)
    {
        StringBuilder sb = new StringBuilder();
        boolean comma = false;
        if ((mask & ) == ) {
            comma = true;
            sb.append("read");
        }
        if ((mask & ) == ) {
            if (commasb.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 getActions will return the string "read,write".

Returns:
the canonical string representation of the actions.
    public String getActions()
    {
        if ( == null)
             = getActions(this.);
        return ;
    }

    
Return the current action mask. Used by the PropertyPermissionCollection

Returns:
the actions mask.
    int getMask() {
        return ;
    }

    
Returns a new PermissionCollection object for storing PropertyPermission objects.

Returns:
a new PermissionCollection object suitable for storing PropertyPermissions.
        return new PropertyPermissionCollection();
    }
    private static final long serialVersionUID = 885438825399942851L;

    
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 ( == 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 IOExceptionClassNotFoundException
    {
        // Read in the action, then initialize the rest
        s.defaultReadObject();
        init(getMask());
    }
A PropertyPermissionCollection stores a set of PropertyPermission permissions.

implements 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.

    // No sync access; OK for this to be stale.
    private boolean all_allowed;

    
Create an empty PropertyPermissions object.
    public PropertyPermissionCollection() {
         = new HashMap(32);     // Capacity for default policy
         = false;
    }

    
Adds a permission to the PropertyPermissions. The key for the hash is the name.

Parameters:
permission the Permission object to add.
Throws:
java.lang.IllegalArgumentException - if the permission is not a PropertyPermission
java.lang.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 = (PropertyPermissionpermission;
        String propName = pp.getName();
        synchronized (this) {
            PropertyPermission existing = (PropertyPermission.get(propName);
            if (existing != null) {
                int oldMask = existing.getMask();
                int newMask = pp.getMask();
                if (oldMask != newMask) {
                    int effective = oldMask | newMask;
                    String actions = PropertyPermission.getActions(effective);
                    .put(propNamenew PropertyPermission(propNameactions));
                }
            } else {
                .put(propNamepermission);
            }
        }
        if (!) {
            if (propName.equals("*"))
                 = true;
        }
    }

    
Check and see if this set of permissions implies the permissions expressed in "permission".

Parameters:
p the Permission object to compare
Returns:
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 = (PropertyPermissionpermission;
        PropertyPermission x;
        int desired = pp.getMask();
        int effective = 0;
        // short circuit if the "*" Permission was added
        if () {
            synchronized (this) {
                x = (PropertyPermission.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);
        synchronized (this) {
            x = (PropertyPermission.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 lastoffset;
        offset = name.length()-1;
        while ((last = name.lastIndexOf("."offset)) != -1) {
            name = name.substring(0, last+1) + "*";
            //System.out.println("check "+name);
            synchronized (this) {
                x = (PropertyPermission.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.

Returns:
an enumeration of all the PropertyPermission objects.
    public Enumeration elements() {
        // Convert Iterator of Map values into an Enumeration
        synchronized (this) {
            return Collections.enumeration(.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".),
    };

    

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 outthrows IOException {
        // Don't call out.defaultWriteObject()
        // Copy perms into a Hashtable
        Hashtable permissions = new Hashtable(.size()*2);
        synchronized (this) {
            permissions.putAll();
        }
        // Write out serializable fields
        ObjectOutputStream.PutField pfields = out.putFields();
        pfields.put("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 inthrows IOException,
        // Don't call defaultReadObject()
        // Read in serialized fields
        ObjectInputStream.GetField gfields = in.readFields();
        // Get all_allowed
         = gfields.get("all_allowed"false);
        // Get permissions
        Hashtable permissions = (Hashtable)gfields.get("permissions"null);
         = new HashMap(permissions.size()*2);
        .putAll(permissions);
    }
New to GrepCode? Check out our FAQ X