From 6cd5a1b0f78a8245783600ab3257e5f2e2c08496 Mon Sep 17 00:00:00 2001 From: Todd Lipcon Date: Thu, 20 Oct 2011 22:26:32 +0000 Subject: [PATCH] HADOOP-7749. Add a NetUtils createSocketAddr call which provides more help in exception messages. Contributed by Todd Lipcon. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1187103 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-common/CHANGES.txt | 3 ++ .../java/org/apache/hadoop/net/NetUtils.java | 39 +++++++++++++++++-- .../org/apache/hadoop/net/TestNetUtils.java | 21 ++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 6aa4b00619..a90be24438 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -486,6 +486,9 @@ Release 0.23.0 - Unreleased HADOOP-7705. Add a log4j back end that can push out JSON data, one per line. (stevel) + HADOOP-7749. Add a NetUtils createSocketAddr call which provides more + help in exception messages. (todd) + OPTIMIZATIONS HADOOP-7333. Performance improvement in PureJavaCrc32. (Eric Caspole diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java index 5f35b85b79..ceaccb285b 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java @@ -150,12 +150,38 @@ public static InetSocketAddress createSocketAddr(String target) { */ public static InetSocketAddress createSocketAddr(String target, int defaultPort) { + return createSocketAddr(target, defaultPort, null); + } + + /** + * Create an InetSocketAddress from the given target string and + * default port. If the string cannot be parsed correctly, the + * configName parameter is used as part of the + * exception message, allowing the user to better diagnose + * the misconfiguration. + * + * @param target a string of either "host" or "host:port" + * @param defaultPort the default port if target does not + * include a port number + * @param configName the name of the configuration from which + * target was loaded. This is used in the + * exception message in the case that parsing fails. + */ + public static InetSocketAddress createSocketAddr(String target, + int defaultPort, + String configName) { + String helpText = ""; + if (configName != null) { + helpText = " (configuration property '" + configName + "')"; + } if (target == null) { - throw new IllegalArgumentException("Target address cannot be null."); + throw new IllegalArgumentException("Target address cannot be null." + + helpText); } int colonIndex = target.indexOf(':'); if (colonIndex < 0 && defaultPort == -1) { - throw new RuntimeException("Not a host:port pair: " + target); + throw new RuntimeException("Not a host:port pair: " + target + + helpText); } String hostname; int port = -1; @@ -165,7 +191,14 @@ public static InetSocketAddress createSocketAddr(String target, } else { // must be the old style : hostname = target.substring(0, colonIndex); - port = Integer.parseInt(target.substring(colonIndex + 1)); + String portStr = target.substring(colonIndex + 1); + try { + port = Integer.parseInt(portStr); + } catch (NumberFormatException nfe) { + throw new IllegalArgumentException( + "Can't parse port '" + portStr + "'" + + helpText); + } } } else { // a new uri diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java index d0927b9821..3ddac25397 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java @@ -163,6 +163,27 @@ public void testWrapUnknownHostException() throws Throwable { assertRemoteDetailsIncluded(wrapped); assertInException(wrapped, "/UnknownHost"); } + + @Test + public void testCreateSocketAddress() throws Throwable { + InetSocketAddress addr = NetUtils.createSocketAddr( + "127.0.0.1:12345", 1000, "myconfig"); + assertEquals("127.0.0.1", addr.getAddress().getHostAddress()); + assertEquals(12345, addr.getPort()); + + addr = NetUtils.createSocketAddr( + "127.0.0.1", 1000, "myconfig"); + assertEquals("127.0.0.1", addr.getAddress().getHostAddress()); + assertEquals(1000, addr.getPort()); + + try { + addr = NetUtils.createSocketAddr( + "127.0.0.1:blahblah", 1000, "myconfig"); + fail("Should have failed to parse bad port"); + } catch (IllegalArgumentException iae) { + assertInException(iae, "myconfig"); + } + } private void assertRemoteDetailsIncluded(IOException wrapped) throws Throwable {