private static InetAddress chooseAddress(String preferred) throws UnknownHostException {
        Set<InetAddress> addresses = getAddresses();
        if (preferred != null && !preferred.isEmpty()) {
            //Favor preferred address if exists
            try {
                InetAddress preferredAddress = InetAddress.getByName(preferred);
                if (addresses != null && addresses.contains(preferredAddress)) {
                    LOG.info("preferred address is " + preferredAddress.getHostAddress() + " for host " + preferredAddress.getHostName());
                    return preferredAddress;
                }
            } catch (UnknownHostException e) {
                //noop
            }
            for (InetAddress address : addresses) {
                if (preferred.equals(address.getHostName())) {
                    return address;
                }
            }
            StringBuffer hostNameBuffer = new StringBuffer();
            for (InetAddress address : addresses) {
                if (hostNameBuffer.length() > 0) {
                    hostNameBuffer.append(", ");
                }
                hostNameBuffer.append(address.getHostName() + "/" + address.getHostAddress());
            }
            LOG.warn("Could not find network address for preferred '" + preferred + "' when the addresses were: " + hostNameBuffer);
        }
        if (addresses.contains(InetAddress.getLocalHost())) {
            //Then if local host address is not bound to a loop-back interface, use it.
            return InetAddress.getLocalHost();
        } else if (addresses != null && !addresses.isEmpty()) {
            //else return the first available addrress
            return addresses.toArray(new InetAddress[addresses.size()])[0];
        } else {
            //else we are forcedt to use the localhost address.
            return InetAddress.getLocalHost();
        }
    }
private static InetAddress chooseAddress ( String preferred ) throws UnknownHostException { Set < InetAddress > addresses = getAddresses ( ) ; if ( preferred != null && ! preferred . isEmpty ( ) ) { try { InetAddress preferredAddress = InetAddress . getByName ( preferred ) ; if ( addresses != null && addresses . contains ( preferredAddress ) ) { LOG . info ( "preferred address is " + preferredAddress . getHostAddress ( ) + " for host " + preferredAddress . getHostName ( ) ) ; return preferredAddress ; } } catch ( UnknownHostException e ) { } for ( InetAddress address : addresses ) { if ( preferred . equals ( address . getHostName ( ) ) ) { return address ; } } StringBuffer hostNameBuffer = new StringBuffer ( ) ; for ( InetAddress address : addresses ) { if ( hostNameBuffer . length ( ) > 0 ) { hostNameBuffer . append ( ", " ) ; } hostNameBuffer . append ( address . getHostName ( ) + "/" + address . getHostAddress ( ) ) ; } LOG . warn ( "Could not find network address for preferred '" + preferred + "' when the addresses were: " + hostNameBuffer ) ; } if ( addresses . contains ( InetAddress . getLocalHost ( ) ) ) { return InetAddress . getLocalHost ( ) ; } else if ( addresses != null && ! addresses . isEmpty ( ) ) { return addresses . toArray ( new InetAddress [ addresses . size ( ) ] ) [ 0 ] ; } else { return InetAddress . getLocalHost ( ) ; } }
Chooses one of the available {@link InetAddress} based on the specified preference.
If the preferred address is not part of the available addresses it will be ignored.

@param preferred
@return