org.apache.hadoop.net
Class NetUtils

java.lang.Object
  extended by org.apache.hadoop.net.NetUtils

public class NetUtils
extends Object


Constructor Summary
NetUtils()
           
 
Method Summary
static void addStaticResolution(String host, String resolvedName)
          Adds a static resolution for host.
static void connect(Socket socket, SocketAddress endpoint, int timeout)
          This is a drop-in replacement for Socket.connect(SocketAddress, int).
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 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(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 InputStream getInputStream(Socket socket)
          Same as getInputStream(socket, socket.getSoTimeout()).

From documentation for getInputStream(Socket, long):
Returns InputStream for the socket.
static InputStream getInputStream(Socket socket, long timeout)
          Returns InputStream for the socket.
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 String getServerAddress(Configuration conf, String oldBindAddressName, String oldPortName, String newBindAddressName)
          Deprecated. 
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 InetSocketAddress makeSocketAddr(String host, int port)
          Create a socket address with the given host and port.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NetUtils

public NetUtils()
Method Detail

getSocketFactory

public 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>. When no such parameter exists then fall back on the default socket factory as configured by hadoop.rpc.socket.factory.class.default. If this default socket factory is not configured, then fall back on the JVM default socket factory.

Parameters:
conf - the configuration
clazz - the class (usually a VersionedProtocol)
Returns:
a socket factory

getDefaultSocketFactory

public static SocketFactory getDefaultSocketFactory(Configuration conf)
Get the default socket factory as specified by the configuration parameter hadoop.rpc.socket.factory.default

Parameters:
conf - the configuration
Returns:
the default socket factory as specified in the configuration or the JVM default socket factory if the configuration does not contain a default socket factory property.

getSocketFactoryFromProperty

public static SocketFactory getSocketFactoryFromProperty(Configuration conf,
                                                         String propValue)
Get the socket factory corresponding to the given proxy URI. If the given proxy URI corresponds to an absence of configuration parameter, returns null. If the URI is malformed raises an exception.

Parameters:
propValue - the property which is the class name of the SocketFactory to instantiate; assumed non null and non empty.
Returns:
a socket factory as defined in the property value.

createSocketAddr

public static InetSocketAddress createSocketAddr(String target)
Util method to build socket addr from either: : ://:/


createSocketAddr

public static InetSocketAddress createSocketAddr(String target,
                                                 int defaultPort)
Util method to build socket addr from either: : ://:/


makeSocketAddr

public static InetSocketAddress makeSocketAddr(String host,
                                               int port)
Create a socket address with the given host and port. The hostname might be replaced with another host that was set via 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.

Parameters:
host - the hostname or IP use to instantiate the object
port - the port number
Returns:
InetSocketAddress

getCanonicalUri

public static URI getCanonicalUri(URI uri,
                                  int defaultPort)
Resolve the uri's hostname and add the default port if not in the uri

Parameters:
uri - to resolve
defaultPort - if none is given
Returns:
URI
Throws:
UnknownHostException

getServerAddress

@Deprecated
public static String getServerAddress(Configuration conf,
                                                 String oldBindAddressName,
                                                 String oldPortName,
                                                 String newBindAddressName)
Deprecated. 

Handle the transition from pairs of attributes specifying a host and port to a single colon separated one.

Parameters:
conf - the configuration to check
oldBindAddressName - the old address attribute name
oldPortName - the old port attribute name
newBindAddressName - the new combined name
Returns:
the complete address from the configuration

addStaticResolution

public static void addStaticResolution(String host,
                                       String resolvedName)
Adds a static resolution for host. This can be used for setting up hostnames with names that are fake to point to a well known host. For e.g. in some testcases we require to have daemons with different hostnames running on the same machine. In order to create connections to these daemons, one can set up mappings from those hostnames to "localhost". getStaticResolution(String) can be used to query for the actual hostname.

Parameters:
host -
resolvedName -

getStaticResolution

public static String getStaticResolution(String host)
Retrieves the resolved name for the passed host. The resolved name must have been set earlier using addStaticResolution(String, String)

Parameters:
host -
Returns:
the resolution

getAllStaticResolutions

public static List<String[]> getAllStaticResolutions()
This is used to get all the resolutions that were added using 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-hostname

Returns:
the list of resolutions

getConnectAddress

public static InetSocketAddress getConnectAddress(Server server)
Returns InetSocketAddress that a client can use to connect to the server. Server.getListenerAddress() is not correct when the server binds to "0.0.0.0". This returns "127.0.0.1:port" when the getListenerAddress() returns "0.0.0.0:port".

Parameters:
server -
Returns:
socket address that a client can use to connect to the server.

getInputStream

public static InputStream getInputStream(Socket socket)
                                  throws IOException
Same as getInputStream(socket, socket.getSoTimeout()).

From documentation for getInputStream(Socket, long):
Returns InputStream for the socket. If the socket has an associated SocketChannel then it returns a SocketInputStream with the given timeout. If the socket does not have a channel, Socket.getInputStream() is returned. In the later case, the timeout argument is ignored and the timeout set with Socket.setSoTimeout(int) applies for reads.

Any socket created using socket factories returned by NetUtils, must use this interface instead of Socket.getInputStream().

Parameters:
socket -
Returns:
InputStream for reading from the socket.
Throws:
IOException
See Also:
getInputStream(Socket, long)

getInputStream

public static InputStream getInputStream(Socket socket,
                                         long timeout)
                                  throws IOException
Returns InputStream for the socket. If the socket has an associated SocketChannel then it returns a SocketInputStream with the given timeout. If the socket does not have a channel, Socket.getInputStream() is returned. In the later case, the timeout argument is ignored and the timeout set with Socket.setSoTimeout(int) applies for reads.

Any socket created using socket factories returned by NetUtils, must use this interface instead of Socket.getInputStream().

Parameters:
socket -
timeout - timeout in milliseconds. This may not always apply. zero for waiting as long as necessary.
Returns:
InputStream for reading from the socket.
Throws:
IOException
See Also:
Socket.getChannel()

getOutputStream

public static OutputStream getOutputStream(Socket socket)
                                    throws IOException
Same as getOutputStream(socket, 0). Timeout of zero implies write will wait until data is available.

From documentation for getOutputStream(Socket, long) :
Returns OutputStream for the socket. If the socket has an associated SocketChannel then it returns a 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.

Any socket created using socket factories returned by NetUtils, must use this interface instead of Socket.getOutputStream().

Parameters:
socket -
Returns:
OutputStream for writing to the socket.
Throws:
IOException
See Also:
getOutputStream(Socket, long)

getOutputStream

public static OutputStream getOutputStream(Socket socket,
                                           long timeout)
                                    throws IOException
Returns OutputStream for the socket. If the socket has an associated SocketChannel then it returns a 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.

Any socket created using socket factories returned by NetUtils, must use this interface instead of Socket.getOutputStream().

Parameters:
socket -
timeout - timeout in milliseconds. This may not always apply. zero for waiting as long as necessary.
Returns:
OutputStream for writing to the socket.
Throws:
IOException
See Also:
Socket.getChannel()

connect

public static void connect(Socket socket,
                           SocketAddress endpoint,
                           int timeout)
                    throws IOException
This is a drop-in replacement for 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.

Parameters:
socket -
endpoint -
timeout - - timeout in milliseconds
Throws:
IOException
See Also:
Socket.connect(java.net.SocketAddress, int)

normalizeHostName

public static String normalizeHostName(String name)
Given a string representation of a host, return its ip address in textual presentation.

Parameters:
name - a string representation of a host: either a textual representation its IP address or its host name
Returns:
its IP address in the string format

normalizeHostNames

public 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.

Parameters:
names - a collection of string representations of hosts
Returns:
a list of corresponding IP addresses in the string format
See Also:
normalizeHostName(String)

verifyHostnames

public static void verifyHostnames(String[] names)
                            throws UnknownHostException
Performs a sanity check on the list of hostnames/IPs to verify they at least appear to be valid.

Parameters:
names - - List of hostnames/IPs
Throws:
UnknownHostException

getLocalInetAddress

public static InetAddress getLocalInetAddress(String host)
                                       throws SocketException
Checks if host is a local host name and return InetAddress corresponding to that address.

Parameters:
host - the specified host
Returns:
a valid local InetAddress or null
Throws:
SocketException - if an I/O error occurs


Copyright © 2009 The Apache Software Foundation