Skip to content

Commit d7494e8

Browse files
committed
IpFetcher: format.
1 parent 4d93c07 commit d7494e8

1 file changed

Lines changed: 61 additions & 58 deletions

File tree

src/main/java/cloudgene/mapred/util/IpFetcher.java

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,74 +10,77 @@
1010
import java.util.logging.Logger;
1111

1212
/**
13-
* Provides the static method fetchServerIp(), which returns this server's IP.
13+
* Provides the static method {@link #fetchServerIp()}, which returns this
14+
* server's IP.
1415
*/
1516
public final class IpFetcher {
1617

17-
private IpFetcher() {}
18+
private IpFetcher() {}
1819

19-
private static String serverIp = null;
20+
private static String serverIp = null;
2021

21-
/**
22-
* Loops through the system's reported IP addresses and returns the first IPv4 external address. If anything goes
23-
* wrong, returns null.
24-
*
25-
* Note that the returned IP is only guaranteed to be visible in the local network link. E.g., a router in the
26-
* way could map this IP to a different one.
27-
*/
28-
private static String fetchSystemIpV4() {
29-
try {
30-
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
31-
while (networkInterfaces.hasMoreElements()) {
32-
NetworkInterface iface = networkInterfaces.nextElement();
22+
/**
23+
* Loops through the system's reported IP addresses and returns the first IPv4
24+
* external address. If anything goes wrong, returns null.
25+
*
26+
* Note that the returned IP is only guaranteed to be visible in the local
27+
* network link. E.g., a router in the way could map this IP to a different one.
28+
*/
29+
private static String fetchSystemIpV4() {
30+
try {
31+
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
32+
while (networkInterfaces.hasMoreElements()) {
33+
NetworkInterface iface = networkInterfaces.nextElement();
3334

34-
if (!iface.isUp() || iface.isLoopback() || iface.isVirtual() || iface.isPointToPoint()) {
35-
continue;
36-
}
35+
if (!iface.isUp() || iface.isLoopback() || iface.isVirtual() || iface.isPointToPoint()) {
36+
continue;
37+
}
3738

38-
Enumeration<InetAddress> addresses = iface.getInetAddresses();
39-
while (addresses.hasMoreElements()) {
40-
InetAddress addr = addresses.nextElement();
39+
Enumeration<InetAddress> addresses = iface.getInetAddresses();
40+
while (addresses.hasMoreElements()) {
41+
InetAddress addr = addresses.nextElement();
4142

42-
if (addr instanceof Inet4Address && !addr.isLoopbackAddress() && !addr.isLinkLocalAddress()) {
43-
return addr.getHostAddress();
44-
}
45-
}
46-
}
47-
return null;
48-
} catch (Exception e) {
49-
return null;
50-
}
51-
}
43+
if (addr instanceof Inet4Address && !addr.isLoopbackAddress() && !addr.isLinkLocalAddress()) {
44+
return addr.getHostAddress();
45+
}
46+
}
47+
}
48+
return null;
49+
} catch (Exception e) {
50+
return null;
51+
}
52+
}
5253

53-
/**
54-
* Does the actual IP fetching work behind fetchServerIp(), see comments there.
55-
*/
56-
private static String actuallyFetchTheIp() {
57-
Logger.getLogger("com.amazonaws").setLevel(Level.SEVERE); // Avoid warning dumps if we're not in AWS
58-
String ec2Ip = EC2MetadataUtils.getPrivateIpAddress();
59-
if (ec2Ip != null) return ec2Ip;
54+
/**
55+
* Does the actual IP fetching work behind fetchServerIp(), see comments there.
56+
*/
57+
private static String actuallyFetchTheIp() {
58+
Logger.getLogger("com.amazonaws").setLevel(Level.SEVERE); // Avoid warning dumps if we're not in AWS
59+
String ec2Ip = EC2MetadataUtils.getPrivateIpAddress();
60+
if (ec2Ip != null) {
61+
return ec2Ip;
62+
}
6063

61-
String localIp = fetchSystemIpV4();
62-
if (localIp != null) return localIp;
64+
String localIp = fetchSystemIpV4();
65+
if (localIp != null) {
66+
return localIp;
67+
}
6368

64-
throw new IllegalStateException("Failed to obtain a valid server IPv4 address from available metadata.");
65-
}
69+
throw new IllegalStateException("Failed to obtain a valid server IPv4 address from available metadata.");
70+
}
6671

67-
/**
68-
* Fetches this server's IPv4, as visible from its local network.
69-
* In an AWS environment, it uses the EC2 metadata service to get the "private IP" that is valid in the local VPC.
70-
* Otherwise, attempts to get the address from the system.
71-
* Raises an IllegalStateException if no valid IPv4 can be obtained.
72-
*
73-
* Results are cached for the lifetime of the application.
74-
*
75-
* @return This server's IPv4, as visible from its local network.
76-
*/
77-
public static String fetchServerIp() {
78-
if (serverIp == null) {
79-
serverIp = actuallyFetchTheIp();
80-
}
81-
return serverIp;
82-
}
72+
/**
73+
* Fetches this server's IPv4, as visible from its local network. In an AWS
74+
* environment, it uses the EC2 metadata service to get the "private IP" that is
75+
* valid in the local VPC. Otherwise, attempts to get the address from the
76+
* system. Raises an IllegalStateException if no valid IPv4 can be obtained.
77+
*
78+
* Results are cached for the lifetime of the application.
79+
*/
80+
public static String fetchServerIp() {
81+
if (serverIp == null) {
82+
serverIp = actuallyFetchTheIp();
83+
}
84+
return serverIp;
85+
}
8386
}

0 commit comments

Comments
 (0)