@ Nonnull public static String determineHostName ( ) throws UnknownHostException { if ( ! OPT_HOSTNAME . isPresent ( ) ) { final String hostName = InetAddress . getLocalHost ( ) . getHostName ( ) ; if ( Strings . isNullOrEmpty ( hostName ) ) { throw new UnknownHostException ( "Unable to lookup localhost, got back empty hostname" ) ; } if ( Strings . nullToEmpty ( hostName ) . equals ( "0.0.0.0" ) ) { throw new UnknownHostException ( "Unable to resolve correct hostname saw bad host" ) ; } OPT_HOSTNAME = Optional . of ( hostName ) ; return OPT_HOSTNAME . get ( ) ; } else { return OPT_HOSTNAME . get ( ) ; } }
Determine the hostname of the machine that we are on . Do not allow blank or 0 . 0 . 0 . 0 as valid hostnames . The host name will be save into the OPT_HOSTNAME static variable .