public final class Host extends Object
This class attempts to resolve all representations of the local host to the
same object. The canonical local host object is the one returned by
localhost()
. Factory methods that perform resolution return this
object when given any loopback IP address or the string "localhost".
Modifier and Type | Method and Description |
---|---|
static void |
addLocalUriScheme(String scheme)
Adds a URI scheme to the set used by
fromUri(URI) to identify
the localhost URIs. |
boolean |
equals(Object obj)
Determines if this
Host is equal to another Host . |
static Host |
fromHostname(String name)
Creates a
Host from a valid hostname. |
static Host |
fromHostnameUnresolved(String name)
Creates a
Host with the given literal hostname. |
static Host |
fromInetAddress(InetAddress ia)
|
static Host |
fromUri(URI uri)
|
String |
getCanonicalHostname()
Gets this host's canonical hostname by performing a reverse DNS lookup
on this host's IP address.
|
String |
getHostname()
Returns the hostname of this host.
|
InetAddress |
getInetAddress()
Resolves this host's hostname to an IP
address.
|
int |
hashCode() |
static Host |
localhost()
Gets the
Host representing the local host. |
boolean |
resolvesToHost(Host other)
Determines if this host has the same canonical name as another host.
|
String |
toString()
Returns the hostname of this host.
|
public static void addLocalUriScheme(String scheme)
fromUri(URI)
to identify
the localhost URIs.
By default, the file
scheme is included.
scheme
- the scheme to addpublic static Host fromHostname(String name)
Host
from a valid hostname. The hostname is resolved to
an InetAddress
, which is used to
create the host.name
- the hostname to resolveInetAddressUnresolvableException
- if the hostname cannot be
resolvedpublic static Host fromUri(URI uri)
Host
from a URI
.
If the URI's scheme identifies resources on this host or if the host component is "localhost", returns the local host object. Otherwise, use the host component of the URI.
uri
- the URI from which to extract the hostIllegalArgumentException
- if the URI has no host component and
does not have a known schemepublic static Host localhost()
Host
representing the local host.InetAddressUnresolvableException
- if "localhost" cannot be
resolvedpublic static Host fromInetAddress(InetAddress ia)
ia
- the InetAddress
public static Host fromHostnameUnresolved(String name)
Host
with the given literal hostname. This method does
not perform localhost resolution and
getHostname()
always returns the given hostname.name
- the hostnamepublic String getHostname()
getCanonicalHostname()
public InetAddress getInetAddress()
InetAddress
for this hostInetAddressUnresolvableException
- if the hostname cannot be
resolvedpublic String getCanonicalHostname()
InetAddress.getCanonicalHostName()
public boolean resolvesToHost(Host other)
other
- the other hosttrue
if the hosts have the same canonical hostnameInetAddressUnresolvableException
- if either hostname cannot be
resolvedpublic boolean equals(Object obj)
Host
is equal to another Host
. Two
Host
s are equal if they have the same hostname.