@InterfaceAudience.LimitedPrivate(value={"HDFS","MapReduce"}) @InterfaceStability.Unstable public class NetUtils extends Object
Modifier and Type | Field and Description |
---|---|
static String |
HADOOP_WIKI
Base URL of the Hadoop Wiki: "http://wiki.apache.org/hadoop/"
|
static String |
UNKNOWN_HOST
text included in wrapped exceptions if the host is null: "(unknown)"
|
Constructor and Description |
---|
NetUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
addStaticResolution(String host,
String resolvedName)
Adds a static resolution for host.
|
static void |
connect(Socket socket,
SocketAddress address,
int timeout)
This is a drop-in replacement for
Socket.connect(SocketAddress, int) . |
static void |
connect(Socket socket,
SocketAddress endpoint,
SocketAddress localAddr,
int timeout)
Like
connect(Socket, SocketAddress, int) but
also takes a local address and port to bind the socket to. |
static InetSocketAddress |
createSocketAddr(String target)
Util method to build socket addr from either:
|
static InetSocketAddress |
createSocketAddr(String target,
int defaultPort)
Util method to build socket addr from either:
|
static InetSocketAddress |
createSocketAddr(String target,
int defaultPort,
String configName)
Create an InetSocketAddress from the given target string and
default port.
|
static InetSocketAddress |
createSocketAddrForHost(String host,
int port)
Create a socket address with the given host and port.
|
static List<String[]> |
getAllStaticResolutions()
This is used to get all the resolutions that were added using
addStaticResolution(String, String) . |
static URI |
getCanonicalUri(URI uri,
int defaultPort)
Resolve the uri's hostname and add the default port if not in the uri
|
static InetSocketAddress |
getConnectAddress(InetSocketAddress addr)
Returns an InetSocketAddress that a client can use to connect to the
given listening address.
|
static InetSocketAddress |
getConnectAddress(Server server)
Returns InetSocketAddress that a client can use to
connect to the server.
|
static SocketFactory |
getDefaultSocketFactory(Configuration conf)
Get the default socket factory as specified by the configuration
parameter hadoop.rpc.socket.factory.default
|
static int |
getFreeSocketPort()
Return a free port number.
|
static String |
getHostname()
Return hostname without throwing exception.
|
static String |
getHostNameOfIP(String ipPort)
Attempt to obtain the host name of the given string which contains
an IP address and an optional port.
|
static String |
getHostPortString(InetSocketAddress addr)
Compose a "host:port" string from the address.
|
static SocketInputWrapper |
getInputStream(Socket socket)
Same as
getInputStream(socket, socket.getSoTimeout()).
|
static SocketInputWrapper |
getInputStream(Socket socket,
long timeout)
Return a
SocketInputWrapper for the socket and set the given
timeout. |
static List<InetAddress> |
getIPs(String subnet,
boolean returnSubinterfaces)
Return an InetAddress for each interface that matches the
given subnet specified using CIDR notation.
|
static InetAddress |
getLocalInetAddress(String host)
Checks if
host is a local host name and return InetAddress
corresponding to that address. |
static OutputStream |
getOutputStream(Socket socket)
Same as getOutputStream(socket, 0).
|
static OutputStream |
getOutputStream(Socket socket,
long timeout)
Returns OutputStream for the socket.
|
static SocketFactory |
getSocketFactory(Configuration conf,
Class<?> clazz)
Get the socket factory for the given class according to its
configuration parameter
hadoop.rpc.socket.factory.class.<ClassName>.
|
static SocketFactory |
getSocketFactoryFromProperty(Configuration conf,
String propValue)
Get the socket factory corresponding to the given proxy URI.
|
static String |
getStaticResolution(String host)
Retrieves the resolved name for the passed host.
|
static boolean |
isLocalAddress(InetAddress addr)
Given an InetAddress, checks to see if the address is a local address, by
comparing the address with all the interfaces on the node.
|
static boolean |
isValidSubnet(String subnet) |
static String |
normalizeHostName(String name)
Given a string representation of a host, return its ip address
in textual presentation.
|
static List<String> |
normalizeHostNames(Collection<String> names)
Given a collection of string representation of hosts, return a list of
corresponding IP addresses in the textual representation.
|
static void |
verifyHostnames(String[] names)
Performs a sanity check on the list of hostnames/IPs to verify they at least
appear to be valid.
|
static IOException |
wrapException(String destHost,
int destPort,
String localHost,
int localPort,
IOException exception)
Take an IOException , the local host port and remote host port details and
return an IOException with the input exception as the cause and also
include the host details.
|
public static final String UNKNOWN_HOST
public static final String HADOOP_WIKI
public static SocketFactory getSocketFactory(Configuration conf, Class<?> clazz)
conf
- the configurationclazz
- the class (usually a VersionedProtocol
)public static SocketFactory getDefaultSocketFactory(Configuration conf)
conf
- the configurationpublic static SocketFactory getSocketFactoryFromProperty(Configuration conf, String propValue)
propValue
- the property which is the class name of the
SocketFactory to instantiate; assumed non null and non empty.public static InetSocketAddress createSocketAddr(String target)
public static InetSocketAddress createSocketAddr(String target, int defaultPort)
public static InetSocketAddress createSocketAddr(String target, int defaultPort, String configName)
configName
parameter is used as part of the
exception message, allowing the user to better diagnose
the misconfiguration.target
- a string of either "host" or "host:port"defaultPort
- the default port if target
does not
include a port numberconfigName
- 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 createSocketAddrForHost(String host, int port)
addStaticResolution(String, String)
. The value of
hadoop.security.token.service.use_ip will determine whether the
standard java host resolver is used, or if the fully qualified resolver
is used.host
- the hostname or IP use to instantiate the objectport
- the port numberpublic static URI getCanonicalUri(URI uri, int defaultPort)
uri
- to resolvedefaultPort
- if none is givenpublic static void addStaticResolution(String host, String resolvedName)
getStaticResolution(String)
can be used to query for
the actual hostname.host
- resolvedName
- public static String getStaticResolution(String host)
addStaticResolution(String, String)
host
- public static List<String[]> getAllStaticResolutions()
addStaticResolution(String, String)
. The return
value is a List each element of which contains an array of String
of the form String[0]=hostname, String[1]=resolved-hostnamepublic static InetSocketAddress getConnectAddress(Server server)
server
- public static InetSocketAddress getConnectAddress(InetSocketAddress addr)
addr
- of a listenerpublic static SocketInputWrapper getInputStream(Socket socket) throws IOException
getInputStream(socket, socket.getSoTimeout()).
IOException
getInputStream(Socket, long)
public static SocketInputWrapper getInputStream(Socket socket, long timeout) throws IOException
SocketInputWrapper
for the socket and set the given
timeout. If the socket does not have an associated channel, then its socket
timeout will be set to the specified value. Otherwise, a
SocketInputStream
will be created which reads with the configured
timeout.
Any socket created using socket factories returned by NetUtils()
,
must use this interface instead of Socket.getInputStream()
.
In general, this should be called only once on each socket: see the note
in SocketInputWrapper.setTimeout(long)
for more information.socket
- timeout
- timeout in milliseconds. zero for waiting as
long as necessary.IOException
Socket.getChannel()
public static OutputStream getOutputStream(Socket socket) throws IOException
getOutputStream(Socket, long)
: SocketOutputStream
with the given timeout. If the socket does not
have a channel, Socket.getOutputStream()
is returned. In the later
case, the timeout argument is ignored and the write will wait until
data is available.NetUtils
,
must use this interface instead of Socket.getOutputStream()
.socket
- IOException
getOutputStream(Socket, long)
public static OutputStream getOutputStream(Socket socket, long timeout) throws IOException
SocketOutputStream
with the given timeout. If the socket does not
have a channel, Socket.getOutputStream()
is returned. In the later
case, the timeout argument is ignored and the write will wait until
data is available.NetUtils
,
must use this interface instead of Socket.getOutputStream()
.socket
- timeout
- timeout in milliseconds. This may not always apply. zero
for waiting as long as necessary.IOException
Socket.getChannel()
public static void connect(Socket socket, SocketAddress address, int timeout) throws IOException
Socket.connect(SocketAddress, int)
.
In the case of normal sockets that don't have associated channels, this
just invokes socket.connect(endpoint, timeout)
. If
socket.getChannel()
returns a non-null channel,
connect is implemented using Hadoop's selectors. This is done mainly
to avoid Sun's connect implementation from creating thread-local
selectors, since Hadoop does not have control on when these are closed
and could end up taking all the available file descriptors.socket
- address
- the remote addresstimeout
- timeout in millisecondsIOException
Socket.connect(java.net.SocketAddress, int)
public static void connect(Socket socket, SocketAddress endpoint, SocketAddress localAddr, int timeout) throws IOException
connect(Socket, SocketAddress, int)
but
also takes a local address and port to bind the socket to.socket
- endpoint
- the remote addresslocalAddr
- the local address to bind the socket totimeout
- timeout in millisecondsIOException
public static String normalizeHostName(String name)
name
- a string representation of a host:
either a textual representation its IP address or its host namepublic static List<String> normalizeHostNames(Collection<String> names)
names
- a collection of string representations of hostsnormalizeHostName(String)
public static void verifyHostnames(String[] names) throws UnknownHostException
names
- - List of hostnames/IPsUnknownHostException
public static String getHostNameOfIP(String ipPort)
ipPort
- string of form ip[:port]public static String getHostname()
public static String getHostPortString(InetSocketAddress addr)
public static InetAddress getLocalInetAddress(String host) throws SocketException
host
is a local host name and return InetAddress
corresponding to that address.host
- the specified hostInetAddress
or nullSocketException
- if an I/O error occurspublic static boolean isLocalAddress(InetAddress addr)
addr
- address to check if it is local node's addresspublic static IOException wrapException(String destHost, int destPort, String localHost, int localPort, IOException exception)
destHost
- target host (nullable)destPort
- target portlocalHost
- local host (nullable)localPort
- local portexception
- the caught exception.public static boolean isValidSubnet(String subnet)
public static List<InetAddress> getIPs(String subnet, boolean returnSubinterfaces)
subnet
- subnet specified using CIDR notationreturnSubinterfaces
- whether to return IPs associated with subinterfacesIllegalArgumentException
- if subnet is invalidpublic static int getFreeSocketPort()
Copyright © 2013 Apache Software Foundation. All rights reserved.