SocketPermission SocketPermission(String, String) in Java

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

Syntax:

java.net.SocketPermission.SocketPermission(String host, String action)

This method takes two arguments. 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. Optionally, a port or a port range may be supplied (separated from the DNS name or IP address by a colon).

To specify the local machine, use "localhost" as the host. Also note: An empty host String ("") is equivalent to "localhost".

The actions parameter contains a comma-separated list of the actions granted for the specified host (and port(s)). Possible actions are"connect", "listen", "accept", "resolve", or any combination of those. "resolve" is automatically added when any of the other three are specified.

Parameters: Two parameters are required for this method.

host: the hostname or IP address of the computer, optionally including a colon followed by a port or port range.

action: the action string.

Throws:

1. NullPointerException - if any parameters are null.

2. IllegalArgumentException - if the format of the host is invalid, or if the action string is empty, malformed, or contains an action other than the specified possible actions

Approach 1: When no exception

Java

package com.SocketPermission;

import java.net.SocketPermission;

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

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

        System.out.println(socketPermission);
    }
}

Output:

("java.net.SocketPermission" "localhost" "connect, resolve")


Approach 2: NullPointerException

Java

package com.SocketPermission;

import java.net.SocketPermission;

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

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

        System.out.println(socketPermission);
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: action can't be null at java.base/java.net.SocketPermission.getMask(SocketPermission.java:502) at java.base/java.net.SocketPermission.<init>(SocketPermission.java:299) at com.SocketPermission.SocketPermission1.main(SocketPermission1.java:10)


Approach 3: IllegalArgumentException 

Java

package com.SocketPermission;

import java.net.SocketPermission;

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

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

        System.out.println(socketPermission);
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: invalid permission: hello at java.base/java.net.SocketPermission.getMask(SocketPermission.java:589) at java.base/java.net.SocketPermission.<init>(SocketPermission.java:299) at com.SocketPermission.SocketPermission1.main(SocketPermission1.java:10)


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