The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.security  [30 examples] > Permissions  [7 examples]

e217. Listing All Permissions Granted to a Loaded Class

This example retrieves all the permissions granted to a particular class. These permissions are effective only if a security manager is installed (see e212 Enabling the Security Manager). However, with a security manager installed, a class will require permission to execute Class.getProtectionDomain() and Policy.getPermissions().
    // Get the protection domain for the class
    ProtectionDomain domain = this.getClass().getProtectionDomain();
    
    // With the protection domain, get all the permissions from the Policy object
    PermissionCollection pcoll = Policy.getPolicy().getPermissions(domain);
    
    // View each permission in the permission collection
    Enumeration enum = pcoll.elements();
    for (; enum.hasMoreElements(); ) {
        Permission p = (Permission)enum.nextElement();
    }
Here's the list of permissions for a class under the default policy file:
    (java.lang.RuntimePermission exitVM)
    (java.lang.RuntimePermission stopThread)
    (java.util.PropertyPermission java.specification.vendor read)
    (java.util.PropertyPermission java.vm.specification.vendor read)
    (java.util.PropertyPermission path.separator read)
    (java.util.PropertyPermission java.vm.name read)
    (java.util.PropertyPermission java.class.version read)
    (java.util.PropertyPermission os.name read)
    (java.util.PropertyPermission java.vendor.url read)
    (java.util.PropertyPermission java.vendor read)
    (java.util.PropertyPermission java.vm.vendor read)
    (java.util.PropertyPermission file.separator read)
    (java.util.PropertyPermission os.version read)
    (java.util.PropertyPermission java.vm.version read)
    (java.util.PropertyPermission java.version read)
    (java.util.PropertyPermission line.separator read)
    (java.util.PropertyPermission java.vm.specification.version read)
    (java.util.PropertyPermission java.specification.name read)
    (java.util.PropertyPermission java.vm.specification.name read)
    (java.util.PropertyPermission java.specification.version read)
    (java.util.PropertyPermission os.arch read)
    (java.net.SocketPermission localhost:1024- listen,resolve)
    (java.io.FilePermission \C:\users\almanac\- read)
Here's the list of permissions for the java.lang.String class which is loaded with the system class loader:
    (java.security.AllPermission <all permissions> <all actions>)

 Related Examples
e212. Enabling the Security Manager
e213. Checking Read/Write Permission for a Directory
e214. Determining If One Permission Implies Another
e215. Creating a Custom Permission
e216. Controlling Access to an Object
e218. Listing All Permissions Granted to Classes Loaded from a URL or Directory

See also: Key Store    Message Digests    Policy Files    Public and Private Keys    Signatures   


© 2002 Addison-Wesley.