Java PropertyPermission newPermissionCollection() Method



Description

The Java PropertyPermission newPermissionCollection() method creates and returns a new PermissionCollection object.

Declaration

Following is the declaration for java.util.PropertyPermission.newPermissionCollection() method

public PermissionCollection newPermissionCollection()

Parameters

NA

Return Value

This method returns the newly created PermissionCollection object.

Exception

NA

Checking Read Permission in a PropertyPermission Instance Example

The following example shows the usage of Java PropertyPermission newPermissionCollection() method to get a permission collection object. We've built a multiple PropertyPermission objects, and then add them to the permission collection retrieved using newPermissionCollection() method and then collection is used to check if it contains a particular permission or not using implies method.

package com.tutorialspoint;

import java.security.PermissionCollection;
import java.util.PropertyPermission;

public class PropertyPermissionDemo {
   private static PermissionCollection permissions;
   
   public static void main(String[] args) {

      // Build property permissions collection
      PropertyPermission permission = 
      new PropertyPermission("java.*", "read");
      permissions = permission.newPermissionCollection();
      permissions.add(permission);
      permissions.add(new PropertyPermission("java.home.*", "read,write"));

      // Check permissions
      checkFileReadPermission("java.home");
      checkFileReadPermission("java.home.usr");
   }

   private static void checkFileReadPermission(String path) {
      
      // Check permission on read action
      if(permissions.implies(new PropertyPermission(path, "read"))) {
         System.out.println("Has permissions on "+path+" for read");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Has permissions on java.home for read
Has permissions on java.home.usr for read

Checking Write Permission in a PropertyPermission Instance Example

The following example shows the usage of Java PropertyPermission newPermissionCollection() method to get a permission collection object. We've built a multiple PropertyPermission objects, and then add them to the permission collection retrieved using newPermissionCollection() method and then collection is used to check if it contains a particular permission or not using implies method.

package com.tutorialspoint;

import java.security.PermissionCollection;
import java.util.PropertyPermission;

public class PropertyPermissionDemo {
   private static PermissionCollection permissions;
   
   public static void main(String[] args) {

      // Build property permissions collection
      PropertyPermission permission = 
      new PropertyPermission("java.*", "read");
      permissions = permission.newPermissionCollection();
      permissions.add(permission);
      permissions.add(new PropertyPermission("java.home.*", "read,write"));

      // Check permissions
      checkFileWritePermission("java.home");
      checkFileWritePermission("java.home.usr");
   }
   
   private static void checkFileWritePermission(String path) {
      
      // Check permission on read action
      if(permissions.implies(new PropertyPermission(path, "write"))) {
         System.out.println("Has permissions on "+path+" for write");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Has permissions on java.home.usr for write
java_util_propertypermission.htm
Advertisements