Open In App

Java.util.PropertyPermission class in Java

Last Updated : 07 Jul, 2017
Improve
Improve
Like Article
Like
Save
Share
Report

This class is for property permission. It extends BasicPermission. 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.

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

Constructor:
PropertyPermission(String name, String actions):

  • Creates a new PropertyPermission object with the specified name.
  • The actions to be granted are passed to the constructor in a string containing a list of one 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.

Methods:

  1. boolean equals(Object obj): Checks two PropertyPermission objects for equality. Checks that obj is a PropertyPermission, and has the same name and actions as this object. It overrides equals in class BasicPermission.

    Syntax: public boolean equals(Object obj).
    Returns: true if obj is a PropertyPermission, and 
    has the same name and actions as this PropertyPermission object.
    Exception: NA.
    




    // Java code illustrating equals() method
    import java.util.*;
    class PropertyPermissionDemo
    {
        public static void main(String arg[])
        {
           // creating property permission
           PropertyPermission gfg = (new PropertyPermission("os.name", "read"));
           if(gfg.equals(new PropertyPermission("os.name", "read")))
               System.out.println("both have same name and action");
           else
               System.out.println("both have different name or action");
        }
    }

    
    

    Output:

    both have same name and action
    
  2. String getActions(): Returns the “canonical string representation” of the actions. That is, this method always returns present actions in the following order: read, write. It overrides getActions in class BasicPermission.

    Syntax: public String getAction().
    Returns: the canonical string representation of the actions.
    Exception: NA.
    




    // Java code illustrating getAction() method
    import java.util.*;
    class PropertyPermissionDemo
    {
        public static void main(String arg[])
        {
           // creating property permission
           PropertyPermission gfg = (new PropertyPermission("os.name",
                       "read,write"));
           System.out.print(gfg.getName() + " has permission to " 
                    + gfg.getActions());
        }
    }

    
    

    Output:

    os.name has permission to read,write
    
  3. int hashCode(): 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. It overrides hashCode in BasicPermission.

    Syntax: public int hashCode().
    Returns: a hash code value for this object.
    Exception: NA.
    




    // Java code illustrating hashCode() method
    import java.util.*;
    class PropertyPermissionDemo
    {
        public static void main(String arg[])
        {
           // creating property permission
           PropertyPermission gfg = (new PropertyPermission("os.name",
                  "read,write"));
           System.out.print("hash code of the object is: " +
                  gfg.getName().hashCode());
        }
    }

    
    

    Output:

    hash code of the object is: -1228098475
    
  4. boolean implies(Permission p): This method checks if this PropertyPermission implies the specified Permission. This is done by checking p is PropertyPermission object, actions of p are subset of this object’s actions and if this object’s name implies p’s actions. It overrides implies in class BasicPermission.

    Syntax: public boolean implies(Permission p).
    Returns: true if obj is a PropertyPermission, and 
    has the same name and actions as this PropertyPermission object.
    Exception: NA.
    




    // Java code illustrating implies() method
    import java.util.*;
    class PropertyPermissionDemo
    {
        public static void main(String arg[])
        {
           // creating property permission
           PropertyPermission gfg = (new PropertyPermission("os.name",
                   "read,write"));
           if(gfg.implies(new PropertyPermission("os.name",
                   "read,write")))
               System.out.println(gfg.getName() + " has permission " 
                       + gfg.getActions());
        }
    }

    
    

    Output:

    os.name has permission read,write
    
  5. PermissionCollection newPermissionCollection(): Returns a new PermissionCollection object for storing PropertyPermission objects.

    Syntax: public PermissionCollection newPermissionCollection().
    Returns: a new PermissionCollection object suitable 
    for storing PropertyPermissions.
    Exception: NA.
    




    // Java code illustrating newPermissionCollection() method
    import java.security.PermissionCollection;
    import java.util.*;
    class PropertyPermissionDemo
    {
        public static void main(String arg[])
        {
           // creating property permission
           PropertyPermission gfg = (new PropertyPermission("os.name",
                   "read"));
           // create property permissions collection
           PermissionCollection permission;
           permission = gfg.newPermissionCollection();
           permission.add(gfg);
           permission.add(new PropertyPermission("java.home.*",
                   "read,write"));
             
           if(permission.implies(gfg))
               System.out.println("java.home.*" + " has permission to "
                       + "read" );
             
        }
    }

    
    

    Output:

    java.home.* has permission to read
    


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads