HADOOP-8286. Simplify getting a socket address from conf (Daryn Sharp via bobby)
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1327108 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
207a2ada0c
commit
a98ba41092
|
@ -446,6 +446,9 @@ Release 0.23.3 - UNRELEASED
|
|||
HADOOP-8283. Allow tests to control token service value (Daryn Sharp via
|
||||
bobby)
|
||||
|
||||
HADOOP-8286. Simplify getting a socket address from conf (Daryn Sharp via
|
||||
bobby)
|
||||
|
||||
Release 0.23.2 - UNRELEASED
|
||||
|
||||
INCOMPATIBLE CHANGES
|
||||
|
|
|
@ -30,6 +30,7 @@ import java.io.OutputStream;
|
|||
import java.io.OutputStreamWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
@ -68,6 +69,7 @@ import org.apache.hadoop.fs.Path;
|
|||
import org.apache.hadoop.fs.CommonConfigurationKeys;
|
||||
import org.apache.hadoop.io.Writable;
|
||||
import org.apache.hadoop.io.WritableUtils;
|
||||
import org.apache.hadoop.net.NetUtils;
|
||||
import org.apache.hadoop.util.ReflectionUtils;
|
||||
import org.apache.hadoop.util.StringUtils;
|
||||
import org.codehaus.jackson.JsonFactory;
|
||||
|
@ -1162,6 +1164,20 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
|
|||
set(name, StringUtils.arrayToString(values));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the socket address for <code>name</code> property as a
|
||||
* <code>InetSocketAddress</code>.
|
||||
* @param name property name.
|
||||
* @param defaultAddress the default value
|
||||
* @param defaultPort the default port
|
||||
* @return InetSocketAddress
|
||||
*/
|
||||
public InetSocketAddress getSocketAddr(
|
||||
String name, String defaultAddress, int defaultPort) {
|
||||
final String address = get(name, defaultAddress);
|
||||
return NetUtils.createSocketAddr(address, defaultPort, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a class by name.
|
||||
*
|
||||
|
|
|
@ -23,6 +23,7 @@ import java.io.File;
|
|||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
|
@ -35,6 +36,7 @@ import static org.junit.Assert.assertArrayEquals;
|
|||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.hadoop.fs.Path;
|
||||
import org.apache.hadoop.net.NetUtils;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
|
||||
public class TestConfiguration extends TestCase {
|
||||
|
@ -604,6 +606,38 @@ public class TestConfiguration extends TestCase {
|
|||
conf.getPattern("test.pattern3", defaultPattern).pattern());
|
||||
}
|
||||
|
||||
public void testSocketAddress() throws IOException {
|
||||
Configuration conf = new Configuration();
|
||||
final String defaultAddr = "host:1";
|
||||
final int defaultPort = 2;
|
||||
InetSocketAddress addr = null;
|
||||
|
||||
addr = conf.getSocketAddr("myAddress", defaultAddr, defaultPort);
|
||||
assertEquals(defaultAddr, NetUtils.getHostPortString(addr));
|
||||
|
||||
conf.set("myAddress", "host2");
|
||||
addr = conf.getSocketAddr("myAddress", defaultAddr, defaultPort);
|
||||
assertEquals("host2:"+defaultPort, NetUtils.getHostPortString(addr));
|
||||
|
||||
conf.set("myAddress", "host2:3");
|
||||
addr = conf.getSocketAddr("myAddress", defaultAddr, defaultPort);
|
||||
assertEquals("host2:3", NetUtils.getHostPortString(addr));
|
||||
|
||||
boolean threwException = false;
|
||||
conf.set("myAddress", "bad:-port");
|
||||
try {
|
||||
addr = conf.getSocketAddr("myAddress", defaultAddr, defaultPort);
|
||||
} catch (IllegalArgumentException iae) {
|
||||
threwException = true;
|
||||
assertEquals("Does not contain a valid host:port authority: " +
|
||||
"bad:-port (configuration property 'myAddress')",
|
||||
iae.getMessage());
|
||||
|
||||
} finally {
|
||||
assertTrue(threwException);
|
||||
}
|
||||
}
|
||||
|
||||
public void testReload() throws IOException {
|
||||
out=new BufferedWriter(new FileWriter(CONFIG));
|
||||
startConfig();
|
||||
|
|
Loading…
Reference in New Issue