svn merge -c 1327108 from trunk. FIXES: HADOOP-8286. Simplify getting a socket address from conf (Daryn Sharp via bobby)
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1327109 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
13b40b4843
commit
8a672d0837
|
@ -328,6 +328,9 @@ Release 0.23.3 - UNRELEASED
|
||||||
HADOOP-8283. Allow tests to control token service value (Daryn Sharp via
|
HADOOP-8283. Allow tests to control token service value (Daryn Sharp via
|
||||||
bobby)
|
bobby)
|
||||||
|
|
||||||
|
HADOOP-8286. Simplify getting a socket address from conf (Daryn Sharp via
|
||||||
|
bobby)
|
||||||
|
|
||||||
Release 0.23.2 - UNRELEASED
|
Release 0.23.2 - UNRELEASED
|
||||||
|
|
||||||
NEW FEATURES
|
NEW FEATURES
|
||||||
|
|
|
@ -30,6 +30,7 @@ import java.io.OutputStream;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -68,6 +69,7 @@ import org.apache.hadoop.fs.Path;
|
||||||
import org.apache.hadoop.fs.CommonConfigurationKeys;
|
import org.apache.hadoop.fs.CommonConfigurationKeys;
|
||||||
import org.apache.hadoop.io.Writable;
|
import org.apache.hadoop.io.Writable;
|
||||||
import org.apache.hadoop.io.WritableUtils;
|
import org.apache.hadoop.io.WritableUtils;
|
||||||
|
import org.apache.hadoop.net.NetUtils;
|
||||||
import org.apache.hadoop.util.ReflectionUtils;
|
import org.apache.hadoop.util.ReflectionUtils;
|
||||||
import org.apache.hadoop.util.StringUtils;
|
import org.apache.hadoop.util.StringUtils;
|
||||||
import org.codehaus.jackson.JsonFactory;
|
import org.codehaus.jackson.JsonFactory;
|
||||||
|
@ -1162,6 +1164,20 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
|
||||||
set(name, StringUtils.arrayToString(values));
|
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.
|
* Load a class by name.
|
||||||
*
|
*
|
||||||
|
|
|
@ -23,6 +23,7 @@ import java.io.File;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -35,6 +36,7 @@ import static org.junit.Assert.assertArrayEquals;
|
||||||
|
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.apache.hadoop.fs.Path;
|
import org.apache.hadoop.fs.Path;
|
||||||
|
import org.apache.hadoop.net.NetUtils;
|
||||||
import org.codehaus.jackson.map.ObjectMapper;
|
import org.codehaus.jackson.map.ObjectMapper;
|
||||||
|
|
||||||
public class TestConfiguration extends TestCase {
|
public class TestConfiguration extends TestCase {
|
||||||
|
@ -604,6 +606,38 @@ public class TestConfiguration extends TestCase {
|
||||||
conf.getPattern("test.pattern3", defaultPattern).pattern());
|
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 {
|
public void testReload() throws IOException {
|
||||||
out=new BufferedWriter(new FileWriter(CONFIG));
|
out=new BufferedWriter(new FileWriter(CONFIG));
|
||||||
startConfig();
|
startConfig();
|
||||||
|
|
Loading…
Reference in New Issue