com.sun.enterprise.util.net
Class NetUtils

java.lang.Object
  extended by com.sun.enterprise.util.net.NetUtils

public class NetUtils
extends java.lang.Object


Nested Class Summary
static class NetUtils.PortAvailability
           
 
Field Summary
static int MAX_PORT
           
 
Method Summary
static NetUtils.PortAvailability checkPort(int portNumber)
          There are 4 possibilities when you want to setup a server socket on a port: 1.
static java.lang.String getCanonicalHostName()
          This method returns the fully qualified name of the host.
static java.net.Socket getClientSocket(java.lang.String host, int port, int msecTimeout)
           
static int getFreePort()
          Gets a free port at the time of call to this method.
static int getFreePort(java.lang.String hostName, int startingPort, int endingPort)
          Returns a random port in the specified range
static java.net.InetAddress[] getHostAddresses()
           
static java.lang.String[] getHostIPs()
           
static java.lang.String getHostName()
           
static int getNextFreePort(java.lang.String hostName, int port)
          Get the next free port (incrementing by 1)
static byte[] ip2bytes(java.lang.String ip)
           
static boolean isEqual(java.lang.String host1, java.lang.String host2)
          Painfully thorough error-handling.
static boolean isLocal(java.lang.String ip)
           
static boolean isLocalHost(java.lang.String ip)
           
static boolean isPortFree(int portNumber)
           
static boolean isPortFree(java.lang.String hostName, int portNumber)
           
static boolean isPortStringValid(java.lang.String portNumber)
           
static boolean isPortValid(int portNumber)
           
static boolean isRemote(java.lang.String ip)
           
static boolean isRunning(int port)
          convenience method for the local machine
static boolean isRunning(java.lang.String host, int port)
          There is sometimes a need for subclasses to know if a local domain is running.
static boolean isSecurePort(java.lang.String hostname, int port)
          This method accepts a hostname and port #.
static boolean isThisHostLocal(java.lang.String hostname)
          Return true if hostname represents the current machine.
static void main(java.lang.String[] args)
           
static java.lang.String trimIP(java.lang.String ip)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_PORT

public static final int MAX_PORT
See Also:
Constant Field Values
Method Detail

isThisHostLocal

public static boolean isThisHostLocal(java.lang.String hostname)
Return true if hostname represents the current machine. A null or empty hostname is considered local, as is the name "localhost". Otherwise, all the IP addresses corresponding to hostname are compared with all the IP addresses corresponding to "localhost", as well as all the IP addresses for all the network interfaces on this machine. Note that hostname can also be an IP address in string form.

Returns:
true if hostname is the local host

isEqual

public static boolean isEqual(java.lang.String host1,
                              java.lang.String host2)
Painfully thorough error-handling. Some would say over-engineered but I plan on never looking at this code again!

Parameters:
host1 -
host2 -
Returns:

getClientSocket

public static java.net.Socket getClientSocket(java.lang.String host,
                                              int port,
                                              int msecTimeout)

getHostName

public static java.lang.String getHostName()

getCanonicalHostName

public static java.lang.String getCanonicalHostName()
                                             throws java.net.UnknownHostException
This method returns the fully qualified name of the host. If the name can't be resolved (on windows if there isn't a domain specified), just host name is returned

Throws:
java.net.UnknownHostException - so it can be handled on a case by case basis

getHostAddresses

public static java.net.InetAddress[] getHostAddresses()

getHostIPs

public static java.lang.String[] getHostIPs()

trimIP

public static java.lang.String trimIP(java.lang.String ip)

ip2bytes

public static byte[] ip2bytes(java.lang.String ip)

isLocalHost

public static boolean isLocalHost(java.lang.String ip)

isLocal

public static boolean isLocal(java.lang.String ip)

isRemote

public static boolean isRemote(java.lang.String ip)

getNextFreePort

public static int getNextFreePort(java.lang.String hostName,
                                  int port)
Get the next free port (incrementing by 1)

Parameters:
hostName - The host name on which the port is to be obtained
port - The port number
Returns:
The next incremental port number or 0 if a port cannot be found.

getFreePort

public static int getFreePort(java.lang.String hostName,
                              int startingPort,
                              int endingPort)
Returns a random port in the specified range

Parameters:
hostName - The host on which the port is to be obtained.
startingPort - starting port in the range
endingPort - ending port in the range
Returns:
the new port or 0 if the range is invalid.

isPortValid

public static boolean isPortValid(int portNumber)

isPortStringValid

public static boolean isPortStringValid(java.lang.String portNumber)

isPortFree

public static boolean isPortFree(java.lang.String hostName,
                                 int portNumber)

checkPort

public static NetUtils.PortAvailability checkPort(int portNumber)
There are 4 possibilities when you want to setup a server socket on a port: 1. The port is already in use 2. The user does not have permission to open up shop on that port An example of (2) is a non-root user on UNIX trying to use port 80 3. The port number is not in the legal range 4. OK -- you can use it!

Parameters:
portNumber -
Returns:
one of the 4 possibilities for this port

isPortFree

public static boolean isPortFree(int portNumber)

getFreePort

public static int getFreePort()
Gets a free port at the time of call to this method. The logic leverages the built in java.net.ServerSocket implementation which binds a server socket to a free port when instantiated with a port 0 .

Note that this method guarantees the availability of the port only at the time of call. The method does not bind to this port.

Checking for free port can fail for several reasons which may indicate potential problems with the system. This method acknowledges the fact and following is the general contract:

  • Best effort is made to find a port which can be bound to. All the exceptional conditions in the due course are considered SEVERE.
  • If any exceptional condition is experienced, 0 is returned, indicating that the method failed for some reasons and the callers should take the corrective action. (The method need not always throw an exception for this).
  • Method is synchronized on this class.

    Returns:
    integer depicting the free port number available at this time 0 otherwise.

  • isSecurePort

    public static boolean isSecurePort(java.lang.String hostname,
                                       int port)
                                throws java.io.IOException,
                                       java.net.ConnectException,
                                       java.net.SocketTimeoutException
    This method accepts a hostname and port #. It uses this information to attempt to connect to the port, send a test query, analyze the result to determine if the port is secure or unsecure (currently only http / https is supported).

    Parameters:
    hostname - - String name of the host where the connections has to be made
    port - - int name of the port where the connections has to be made
    Returns:
    admin password
    Throws:
    java.io.IOException - if an error occurs during the connection
    java.net.ConnectException - if an error occurred while attempting to connect a socket to a remote address and port.
    java.net.SocketTimeoutException - if timeout(4s) expires before connecting

    isRunning

    public static boolean isRunning(java.lang.String host,
                                    int port)
    There is sometimes a need for subclasses to know if a local domain is running. An example of such a command is change-master-password command. The stop-domain command also needs to know if a domain is running without having to provide user name and password on command line (this is the case when I own a domain that has non-default admin user and password) and want to stop it without providing it.

    In such cases, we need to know if the domain is running and this method provides a way to do that.

    Returns:
    boolean indicating whether the server is running

    isRunning

    public static final boolean isRunning(int port)
    convenience method for the local machine


    main

    public static void main(java.lang.String[] args)


    Copyright © 2012 GlassFish Community. All Rights Reserved.