SocketPermission implies(Permission) in Java

implies(Permission): This method is available in the java.net.SocketPermission class of Java.

Syntax:

boolean java.net.SocketPermission.implies(Permission p)

This method takes one argument. This method checks if this socket permission object "implies" the specified permission.

More specifically, this method first ensures that all of the following are  true (and returns false if any of them are not):

1. p is an instance of SocketPermission.

2. p's actions are a proper subset of this object's actions.

3. p's port range is included in this port range.

Note: the port range is ignored when p only contains the action, 'resolve'. 

Then implies checking each of the following, in order, and for each return true if the stated condition is true:

1. If this object was initialized with a single IP address and one of p'sIP addresses is equal to this object's IP address.

2. If this object is a wildcard domain (such as *.example.com), and p's canonical name (the name without any preceding *)ends with this object's canonical hostname.

3. If this object was not initialized with a single IP address, and one of this object's IP addresses equals one of p's IP addresses.

4. If this canonical name equals p's canonical name. If none of the above are true, implies returns false.

Parameters: One parameter is required for this method.

p: the permission to check against.

Returns: true if the specified permission is implied by this object, false if not.

Exceptions: NA

Approach

Java

package com.SocketPermission;

import java.net.SocketPermission;
import java.security.Permission;

public class SocketPermissionimplies {
    public static void main(String[] args) {

        String host = "localhost";
        String action = "connect";
        SocketPermission socketPermission = new SocketPermission(host, action);

        Permission p = new Permission(action) {

            /**
             *
             */
            private static final long serialVersionUID = 1L;

            @Override
            public boolean implies(Permission permission) {
                return false;
            }

            @Override
            public int hashCode() {
                return 0;
            }

            @Override
            public String getActions() {
                return null;
            }

            @Override
            public boolean equals(Object obj) {
                return false;
            }
        };

        System.out.println(socketPermission.implies(p));
    }
}

Output:

false


Some other methods of SocketPermission

SocketPermission(String, String)This method creates a new SocketPermission object with the specified actions. The host is expressed as a DNS name, or as a numerical IP address.

equals(Object)This method checks two SocketPermission objects for equality.

getActions()This method returns the canonical string representation of the actions. Always returns present actions in the following order: connect, listen, accept, resolve.

hashCode()This method returns the hash code value for this object.

implies(Permission)This method checks if this socket permission object "implies" the specified permission.

newPermissionCollection()This method returns a new PermissionCollection object for storing SocketPermission objects.

No comments:

Post a Comment