HADOOP-8335. Improve Configuration's address handling (Daryn Sharp via bobby)

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1332427 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Joseph Evans 2012-04-30 21:55:26 +00:00
parent bd956559c8
commit 1e88c1f088
5 changed files with 74 additions and 2 deletions

View File

@ -494,6 +494,9 @@ Release 0.23.3 - UNRELEASED
HADOOP-8330. Update TestSequenceFile.testCreateUsesFsArg() for HADOOP-8305. HADOOP-8330. Update TestSequenceFile.testCreateUsesFsArg() for HADOOP-8305.
(John George via szetszwo) (John George via szetszwo)
HADOOP-8335. Improve Configuration's address handling (Daryn Sharp via
bobby)
Release 0.23.2 - UNRELEASED Release 0.23.2 - UNRELEASED
INCOMPATIBLE CHANGES INCOMPATIBLE CHANGES

View File

@ -1237,6 +1237,29 @@ public InetSocketAddress getSocketAddr(
return NetUtils.createSocketAddr(address, defaultPort, name); return NetUtils.createSocketAddr(address, defaultPort, name);
} }
/**
* Set the socket address for the <code>name</code> property as
* a <code>host:port</code>.
*/
public void setSocketAddr(String name, InetSocketAddress addr) {
set(name, NetUtils.getHostPortString(addr));
}
/**
* Set the socket address a client can use to connect for the
* <code>name</code> property as a <code>host:port</code>. The wildcard
* address is replaced with the local host's address.
* @param name property name.
* @param addr InetSocketAddress of a listener to store in the given property
* @return InetSocketAddress for clients to connect
*/
public InetSocketAddress updateConnectAddr(String name,
InetSocketAddress addr) {
final InetSocketAddress connectAddr = NetUtils.getConnectAddress(addr);
setSocketAddr(name, connectAddr);
return connectAddr;
}
/** /**
* Load a class by name. * Load a class by name.
* *

View File

@ -351,8 +351,19 @@ public static List <String[]> getAllStaticResolutions() {
* @return socket address that a client can use to connect to the server. * @return socket address that a client can use to connect to the server.
*/ */
public static InetSocketAddress getConnectAddress(Server server) { public static InetSocketAddress getConnectAddress(Server server) {
InetSocketAddress addr = server.getListenerAddress(); return getConnectAddress(server.getListenerAddress());
if (addr.getAddress().isAnyLocalAddress()) { }
/**
* Returns the InetSocketAddress that a client can use to connect to the
* given listening address. This returns "hostname:port" of the server,
* or "127.0.0.1:port" when given a wildcard address of "0.0.0.0:port".
*
* @param addr of a listener
* @return socket address that a client can use to connect to the server.
*/
public static InetSocketAddress getConnectAddress(InetSocketAddress addr) {
if (!addr.isUnresolved() && addr.getAddress().isAnyLocalAddress()) {
try { try {
addr = new InetSocketAddress(InetAddress.getLocalHost(), addr.getPort()); addr = new InetSocketAddress(InetAddress.getLocalHost(), addr.getPort());
} catch (UnknownHostException uhe) { } catch (UnknownHostException uhe) {

View File

@ -23,6 +23,7 @@
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.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -671,6 +672,27 @@ public void testSocketAddress() throws IOException {
} }
} }
public void testSetSocketAddress() throws IOException {
Configuration conf = new Configuration();
NetUtils.addStaticResolution("host", "127.0.0.1");
final String defaultAddr = "host:1";
InetSocketAddress addr = NetUtils.createSocketAddr(defaultAddr);
conf.setSocketAddr("myAddress", addr);
assertEquals(defaultAddr, NetUtils.getHostPortString(addr));
}
public void testUpdateSocketAddress() throws IOException {
InetSocketAddress addr = NetUtils.createSocketAddrForHost("host", 1);
InetSocketAddress connectAddr = conf.updateConnectAddr("myAddress", addr);
assertEquals(connectAddr.getHostName(), addr.getHostName());
addr = new InetSocketAddress(1);
connectAddr = conf.updateConnectAddr("myAddress", addr);
assertEquals(connectAddr.getHostName(),
InetAddress.getLocalHost().getHostName());
}
public void testReload() throws IOException { public void testReload() throws IOException {
out=new BufferedWriter(new FileWriter(CONFIG)); out=new BufferedWriter(new FileWriter(CONFIG));
startConfig(); startConfig();

View File

@ -169,6 +169,19 @@ public void testWrapUnknownHostException() throws Throwable {
assertInException(wrapped, "/UnknownHost"); assertInException(wrapped, "/UnknownHost");
} }
@Test
public void testGetConnectAddress() throws IOException {
NetUtils.addStaticResolution("host", "127.0.0.1");
InetSocketAddress addr = NetUtils.createSocketAddrForHost("host", 1);
InetSocketAddress connectAddr = NetUtils.getConnectAddress(addr);
assertEquals(addr.getHostName(), connectAddr.getHostName());
addr = new InetSocketAddress(1);
connectAddr = NetUtils.getConnectAddress(addr);
assertEquals(InetAddress.getLocalHost().getHostName(),
connectAddr.getHostName());
}
@Test @Test
public void testCreateSocketAddress() throws Throwable { public void testCreateSocketAddress() throws Throwable {
InetSocketAddress addr = NetUtils.createSocketAddr( InetSocketAddress addr = NetUtils.createSocketAddr(