Adds javadoc to NetworkConstants
Also renames some internal variables to be more understandable No API changes! :)
This commit is contained in:
parent
9cc9f4e1ec
commit
2653facd3b
@ -24,62 +24,101 @@ import java.net.SocketException;
|
|||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A class that holds a number of network-related constants.
|
||||||
|
*/
|
||||||
public final class NetworkConstants {
|
public final class NetworkConstants {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link InetAddress} representing the host machine
|
||||||
|
*
|
||||||
|
* We cache this because some machines take almost forever to return from
|
||||||
|
* {@link InetAddress}.getLocalHost(). This may be due to incorrect
|
||||||
|
* configuration of the hosts and DNS client configuration files.
|
||||||
|
*/
|
||||||
public static final InetAddress LOCALHOST;
|
public static final InetAddress LOCALHOST;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The loopback {@link NetworkInterface} on the current machine
|
||||||
|
*/
|
||||||
public static final NetworkInterface LOOPBACK_IF;
|
public static final NetworkInterface LOOPBACK_IF;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The logger being used by this class
|
||||||
|
*/
|
||||||
private static final InternalLogger logger =
|
private static final InternalLogger logger =
|
||||||
InternalLoggerFactory.getInstance(NetworkConstants.class);
|
InternalLoggerFactory.getInstance(NetworkConstants.class);
|
||||||
|
|
||||||
static {
|
static {
|
||||||
// We cache this because some machine takes almost forever to return
|
|
||||||
// from InetAddress.getLocalHost(). I think it's due to the incorrect
|
//Start the process of discovering localhost
|
||||||
// /etc/hosts or /etc/resolve.conf.
|
|
||||||
InetAddress localhost = null;
|
InetAddress localhost = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
//Let's start by getting localhost automatically
|
||||||
localhost = InetAddress.getLocalHost();
|
localhost = InetAddress.getLocalHost();
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
|
//No? That's okay.
|
||||||
try {
|
try {
|
||||||
|
//Try to force an IPv4 localhost address
|
||||||
localhost = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
|
localhost = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
|
||||||
} catch (UnknownHostException e1) {
|
} catch (UnknownHostException e1) {
|
||||||
|
//No? Okay. You must be using IPv6
|
||||||
try {
|
try {
|
||||||
|
//Try to force an IPv6 localhost address
|
||||||
localhost = InetAddress.getByAddress(
|
localhost = InetAddress.getByAddress(
|
||||||
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 });
|
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 });
|
||||||
} catch (UnknownHostException e2) {
|
} catch (UnknownHostException e2) {
|
||||||
logger.error("Failed to resolve localhost", e2);
|
//No? Okay.
|
||||||
|
logger.error("Failed to resolve localhost - Incorrect network configuration?", e2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Set the localhost constant
|
||||||
LOCALHOST = localhost;
|
LOCALHOST = localhost;
|
||||||
|
|
||||||
NetworkInterface loopbackIf;
|
//Prepare to get the local NetworkInterface
|
||||||
|
NetworkInterface loopbackInterface;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
loopbackIf = NetworkInterface.getByInetAddress(LOCALHOST);
|
//Automatically get the loopback interface
|
||||||
|
loopbackInterface = NetworkInterface.getByInetAddress(LOCALHOST);
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
loopbackIf = null;
|
//No? Alright. There is a backup!
|
||||||
|
loopbackInterface = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If null is returned, iterate over all the available network interfaces.
|
//Check to see if a network interface was not found
|
||||||
if (loopbackIf == null) {
|
if (loopbackInterface == null) {
|
||||||
try {
|
try {
|
||||||
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
|
//Start iterating over all network interfaces
|
||||||
e.hasMoreElements();) {
|
for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
||||||
NetworkInterface nif = e.nextElement();
|
interfaces.hasMoreElements();) {
|
||||||
if (nif.isLoopback()) {
|
//Get the "next" interface
|
||||||
loopbackIf = nif;
|
NetworkInterface networkInterface = interfaces.nextElement();
|
||||||
|
|
||||||
|
//Check to see if the interface is a loopback interface
|
||||||
|
if (networkInterface.isLoopback()) {
|
||||||
|
//Phew! The loopback interface was found.
|
||||||
|
loopbackInterface = networkInterface;
|
||||||
|
//No need to keep iterating
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
|
//Nope. Can't do anything else, sorry!
|
||||||
logger.error("Failed to enumerate network interfaces", e);
|
logger.error("Failed to enumerate network interfaces", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LOOPBACK_IF = loopbackIf;
|
//Set the loopback interface constant
|
||||||
|
LOOPBACK_IF = loopbackInterface;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A constructor to stop this class being constructed.
|
||||||
|
*/
|
||||||
private NetworkConstants() {
|
private NetworkConstants() {
|
||||||
// Unused
|
// Unused
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user