HADOOP-8238. svn merge -c 1308192 from trunk
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1308193 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ffc5f0b915
commit
6e7c70870a
|
@ -194,6 +194,9 @@ Release 2.0.0 - UNRELEASED
|
||||||
HADOOP-8218. RPC.closeProxy shouldn't throw error when closing a mock
|
HADOOP-8218. RPC.closeProxy shouldn't throw error when closing a mock
|
||||||
(todd)
|
(todd)
|
||||||
|
|
||||||
|
HADOOP-8238. NetUtils#getHostNameOfIP blows up if given ip:port
|
||||||
|
string w/o port. (eli)
|
||||||
|
|
||||||
BREAKDOWN OF HADOOP-7454 SUBTASKS
|
BREAKDOWN OF HADOOP-7454 SUBTASKS
|
||||||
|
|
||||||
HADOOP-7455. HA: Introduce HA Service Protocol Interface. (suresh)
|
HADOOP-7455. HA: Introduce HA Service Protocol Interface. (suresh)
|
||||||
|
|
|
@ -570,31 +570,29 @@ public class NetUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Pattern ipPattern = // Pattern for matching hostname to ip:port
|
private static final Pattern ipPortPattern = // Pattern for matching ip[:port]
|
||||||
Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:?\\d*");
|
Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}(:\\d+)?");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempt to obtain the host name of a name specified by ip address.
|
* Attempt to obtain the host name of the given string which contains
|
||||||
* Check that the node name is an ip addr and if so, attempt to determine
|
* an IP address and an optional port.
|
||||||
* its host name. If the name is not an IP addr, or the actual name cannot
|
|
||||||
* be determined, return null.
|
|
||||||
*
|
*
|
||||||
* @return Host name or null
|
* @param ipPort string of form ip[:port]
|
||||||
|
* @return Host name or null if the name can not be determined
|
||||||
*/
|
*/
|
||||||
public static String getHostNameOfIP(String ip) {
|
public static String getHostNameOfIP(String ipPort) {
|
||||||
// If name is not an ip addr, don't bother looking it up
|
if (null == ipPort || !ipPortPattern.matcher(ipPort).matches()) {
|
||||||
if(!ipPattern.matcher(ip).matches())
|
|
||||||
return null;
|
|
||||||
|
|
||||||
String hostname = "";
|
|
||||||
try {
|
|
||||||
String n = ip.substring(0, ip.indexOf(':'));
|
|
||||||
hostname = InetAddress.getByName(n).getHostName();
|
|
||||||
} catch (UnknownHostException e) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return hostname;
|
try {
|
||||||
|
int colonIdx = ipPort.indexOf(':');
|
||||||
|
String ip = (-1 == colonIdx) ? ipPort
|
||||||
|
: ipPort.substring(0, ipPort.indexOf(':'));
|
||||||
|
return InetAddress.getByName(ip).getHostName();
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -499,6 +499,18 @@ public class TestNetUtils {
|
||||||
assertEquals("scheme://host.a.b/path", uri.toString());
|
assertEquals("scheme://host.a.b/path", uri.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetHostNameOfIP() {
|
||||||
|
assertNull(NetUtils.getHostNameOfIP(null));
|
||||||
|
assertNull(NetUtils.getHostNameOfIP(""));
|
||||||
|
assertNull(NetUtils.getHostNameOfIP("crazytown"));
|
||||||
|
assertNull(NetUtils.getHostNameOfIP("127.0.0.1:")); // no port
|
||||||
|
assertNull(NetUtils.getHostNameOfIP("127.0.0.1:-1")); // bogus port
|
||||||
|
assertNull(NetUtils.getHostNameOfIP("127.0.0.1:A")); // bogus port
|
||||||
|
assertNotNull(NetUtils.getHostNameOfIP("127.0.0.1"));
|
||||||
|
assertNotNull(NetUtils.getHostNameOfIP("127.0.0.1:1"));
|
||||||
|
}
|
||||||
|
|
||||||
private <T> void assertBetterArrayEquals(T[] expect, T[]got) {
|
private <T> void assertBetterArrayEquals(T[] expect, T[]got) {
|
||||||
String expectStr = StringUtils.join(expect, ", ");
|
String expectStr = StringUtils.join(expect, ", ");
|
||||||
String gotStr = StringUtils.join(got, ", ");
|
String gotStr = StringUtils.join(got, ", ");
|
||||||
|
|
Loading…
Reference in New Issue