FileDocCategorySizeDatePackage
NetUtil.javaAPI DocGlassfish v2 API4292Fri May 04 22:33:16 BST 2007com.sun.enterprise.admin.common

NetUtil

public class NetUtil extends Object
A utility class to check the port availability etc.

Fields Summary
public static final int
kMaxPortNo
private static Random
random
private static com.sun.enterprise.admin.util.SOMLocalStringsManager
localizedStrMgr
Constructors Summary
Methods Summary
public static voidcheckPortAvailability(int port)
Static method to check whether the port provided is available to connect to. Uses ServerSocket for this check.

throws
PortInUseException

	
    		            		    		     
	
          
    
        ServerSocket trialSocket = null;
        try
        {
            trialSocket = new ServerSocket(port);
        }
        catch(IOException ioe)
        {
			String msg = localizedStrMgr.getString( "admin.common.port_in_use", new String( port + "" ) );
            throw new PortInUseException( msg );
        }
        finally
        {
            try
            {
				if (trialSocket != null)
				{
					trialSocket.close();
				}
            }
            catch(Exception e)
            {
                Debug.printStackTrace(e);
            }
        }
    
public static intgetFreePort()


       
    
        synchronized(NetUtil.class)
        {
            /* scared of using infinite while loop */
            for (int i = 0; i < 1024; i++) 
            {
                int nextInt = random.nextInt(kMaxPortNo);
                if (nextInt <= 1024)
                {
                    continue;
                }
                else if (isPortAvalable(nextInt))
                {
                    return nextInt;
                }
            }
        }
        return 0;
    
public static booleanisPortAvalable(int portNo)
Returns whether the given port number is available at the time of call.

param
portNo integer specifying the port.

		boolean available = true;
		
		try
		{
			checkPortAvailability(portNo);
		}
		catch(Exception e)
		{
			available = false;
		}
		return ( available );