Add some logging for easier diagnosis in NetUtil

.. + formatting
This commit is contained in:
Trustin Lee 2013-04-23 22:57:29 +09:00
parent be1426a220
commit 7452d05fa6

View File

@ -63,8 +63,7 @@ public final class NetUtil {
/** /**
* The logger being used by this class * The logger being used by this class
*/ */
private static final InternalLogger logger = private static final InternalLogger logger = InternalLoggerFactory.getInstance(NetUtil.class);
InternalLoggerFactory.getInstance(NetUtil.class);
static { static {
//Start the process of discovering localhost //Start the process of discovering localhost
@ -72,18 +71,22 @@ public final class NetUtil {
try { try {
localhost = InetAddress.getLocalHost(); localhost = InetAddress.getLocalHost();
validateHost(localhost); validateHost(localhost);
} catch (IOException e) { } catch (IOException e0) {
// The default local host names did not work. Try hard-coded IPv4 address. // The default local host names did not work. Try hard-coded IPv4 address.
try { try {
localhost = InetAddress.getByAddress(new byte[]{127, 0, 0, 1}); localhost = InetAddress.getByAddress(new byte[]{ 127, 0, 0, 1 });
validateHost(localhost); validateHost(localhost);
} catch (IOException e1) { } catch (IOException e1) {
// The hard-coded IPv4 address did not work. Try hard coded IPv6 address. // The hard-coded IPv4 address did not work. Try hard coded IPv6 address.
try { try {
localhost = InetAddress.getByAddress(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}); localhost = InetAddress.getByAddress(new byte[]{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 });
validateHost(localhost); validateHost(localhost);
} catch (IOException e2) { } catch (IOException e2) {
throw new Error("Failed to resolve localhost - incorrect network configuration?", e2); // Log all exceptions we caught so far for easier diagnosis.
logger.warn("Failed to resolve localhost with InetAddress.getLocalHost():", e0);
logger.warn("Failed to resolve localhost with InetAddress.getByAddress(127.0.0.1):", e1);
logger.warn("Failed to resolve localhost with InetAddress.getByAddress(::1)", e2);
throw new Error("failed to resolve localhost; incorrect network configuration?");
} }
} }
} }