Java Code Examples for java.net.InetAddress#getCanonicalHostName()
The following examples show how to use
java.net.InetAddress#getCanonicalHostName() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpnegoHandler.java From presto with Apache License 2.0 | 6 votes |
private static String canonicalizeServiceHostName(String hostName) { try { InetAddress address = InetAddress.getByName(hostName); String fullHostName; if ("localhost".equalsIgnoreCase(address.getHostName())) { fullHostName = InetAddress.getLocalHost().getCanonicalHostName(); } else { fullHostName = address.getCanonicalHostName(); } if (fullHostName.equalsIgnoreCase("localhost")) { throw new ClientException("Fully qualified name of localhost should not resolve to 'localhost'. System configuration error?"); } return fullHostName; } catch (UnknownHostException e) { throw new ClientException("Failed to resolve host: " + hostName, e); } }
Example 2
Source File: ServerLauncher.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Gets the host, as either hostname or IP address, on which the Server was bound and running. An attempt is made * to get the canonical hostname for IP address to which the Server was bound for accepting client requests. If * the server bind address is null or localhost is unknown, then a default String value of "localhost/127.0.0.1" * is returned. * * Note, this information is purely information and should not be used to re-construct state or for * other purposes. * * @return the hostname or IP address of the host running the Server, based on the bind-address, or * 'localhost/127.0.0.1' if the bind address is null and localhost is unknown. * @see java.net.InetAddress * @see #getServerBindAddress() */ public String getServerBindAddressAsString() { try { if (getServerBindAddress() != null) { return getServerBindAddress().getCanonicalHostName(); } final InetAddress localhost = SocketCreator.getLocalHost(); return localhost.getCanonicalHostName(); } catch (UnknownHostException ignore) { // TODO determine a better value for the host on which the Server is running to return here... // NOTE returning localhost/127.0.0.1 implies the serverBindAddress was null and no IP address for localhost // could be found return "localhost/127.0.0.1"; } }
Example 3
Source File: LocatorLauncher.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Gets the host, as either hostname or IP address, on which the Locator was bound and running. An attempt is made * to get the canonical hostname for IP address to which the Locator was bound for accepting client requests. If * the bind address is null or localhost is unknown, then a default String value of "localhost/127.0.0.1" is returned. * * Note, this information is purely information and should not be used to re-construct state or for * other purposes. * * @return the hostname or IP address of the host running the Locator, based on the bind-address, or * 'localhost/127.0.0.1' if the bind address is null and localhost is unknown. * @see java.net.InetAddress * @see #getBindAddress() */ protected String getBindAddressAsString() { try { if (getBindAddress() != null) { return getBindAddress().getCanonicalHostName(); } final InetAddress localhost = SocketCreator.getLocalHost(); return localhost.getCanonicalHostName(); } catch (UnknownHostException ignore) { // TODO determine a better value for the host on which the Locator is running to return here... // NOTE returning localhost/127.0.0.1 implies the bindAddress was null and no IP address for localhost // could be found return "localhost/127.0.0.1"; } }
Example 4
Source File: HostInterceptor.java From mt-flume with Apache License 2.0 | 6 votes |
/** * Only {@link HostInterceptor.Builder} can build me */ private HostInterceptor(boolean preserveExisting, boolean useIP, String header) { this.preserveExisting = preserveExisting; this.header = header; InetAddress addr; try { addr = InetAddress.getLocalHost(); if (useIP) { host = addr.getHostAddress(); } else { host = addr.getCanonicalHostName(); } } catch (UnknownHostException e) { logger.warn("Could not get local host address. Exception follows.", e); } }
Example 5
Source File: StreamUtil.java From hadoop with Apache License 2.0 | 5 votes |
static URL qualifyHost(URL url) { try { InetAddress a = InetAddress.getByName(url.getHost()); String qualHost = a.getCanonicalHostName(); URL q = new URL(url.getProtocol(), qualHost, url.getPort(), url.getFile()); return q; } catch (IOException io) { return url; } }
Example 6
Source File: HostStatHelper.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * @return this machine's fully qualified hostname * or "unknownHostName" if one cannot be found. */ private static String getHostSystemName() { String hostname = "unknownHostName"; try { InetAddress addr = SocketCreator.getLocalHost(); hostname = addr.getCanonicalHostName(); } catch (UnknownHostException uhe) { } return hostname; }
Example 7
Source File: InstanceConnectionInfo.java From stratosphere with Apache License 2.0 | 5 votes |
/** * Constructs a new instance connection info object. The constructor will attempt to retrieve the instance's * hostname and domain name through the operating system's lookup mechanisms. * * @param inetAddress * the network address the instance's task manager binds its sockets to * @param ipcPort * the port instance's task manager runs its IPC service on * @param dataPort * the port instance's task manager expects to receive transfer envelopes on */ public InstanceConnectionInfo(InetAddress inetAddress, int ipcPort, int dataPort) { if (inetAddress == null) { throw new IllegalArgumentException("Argument inetAddress must not be null"); } if (ipcPort <= 0) { throw new IllegalArgumentException("Argument ipcPort must be greater than zero"); } if (dataPort <= 0) { throw new IllegalArgumentException("Argument dataPort must be greater than zero"); } this.inetAddress = inetAddress; final String hostAddStr = inetAddress.getHostAddress(); final String fqdn = inetAddress.getCanonicalHostName(); if (hostAddStr.equals(fqdn)) { this.hostName = fqdn; this.domainName = null; } else { // Look for the first dot in the FQDN final int firstDot = fqdn.indexOf('.'); if (firstDot == -1) { this.hostName = fqdn; this.domainName = null; } else { this.hostName = fqdn.substring(0, firstDot); this.domainName = fqdn.substring(firstDot + 1); } } this.ipcPort = ipcPort; this.dataPort = dataPort; }
Example 8
Source File: StramClientUtilsTest.java From attic-apex-core with Apache License 2.0 | 5 votes |
private String getHostString(String host) throws UnknownHostException { InetAddress address = InetAddress.getByName(host); if (address.isAnyLocalAddress() || address.isLoopbackAddress()) { return address.getCanonicalHostName(); } else { return address.getHostName(); } }
Example 9
Source File: NetworkUtils.java From jim-framework with Apache License 2.0 | 5 votes |
/** * 获取主机名 * @return 主机名 */ public static String getHostName() { String name = null; try { Enumeration<NetworkInterface> infs = NetworkInterface.getNetworkInterfaces(); while (infs.hasMoreElements() && (name == null)) { NetworkInterface net = infs.nextElement(); if (net.isLoopback()) { continue; } Enumeration<InetAddress> addr = net.getInetAddresses(); while (addr.hasMoreElements()) { InetAddress inet = addr.nextElement(); if (inet.isSiteLocalAddress()) { name = inet.getHostAddress(); } if (!inet.getCanonicalHostName().equalsIgnoreCase(inet.getHostAddress())) { name = inet.getCanonicalHostName(); break; } } } } catch (SocketException e) { name = "localhost"; } return name; }
Example 10
Source File: Utils.java From vertx-mail-client with Apache License 2.0 | 5 votes |
/** * get the hostname by resolving our own address * * this method is not async due to possible dns call, we run this with executeBlocking * * @return the hostname */ public static String getHostname() { try { InetAddress ip = InetAddress.getLocalHost(); return ip.getCanonicalHostName(); } catch (UnknownHostException e) { // as a last resort, use localhost // another common convention would be to use the clients ip address // like [192.168.1.1] or [127.0.0.1] return "localhost"; } }
Example 11
Source File: NetworkUtils.java From pinpoint with Apache License 2.0 | 5 votes |
@Deprecated public static String getMachineName() { try { Enumeration<NetworkInterface> enet = NetworkInterface.getNetworkInterfaces(); while (enet.hasMoreElements()) { NetworkInterface net = enet.nextElement(); if (net.isLoopback()) { continue; } Enumeration<InetAddress> eaddr = net.getInetAddresses(); while (eaddr.hasMoreElements()) { InetAddress inet = eaddr.nextElement(); final String canonicalHostName = inet.getCanonicalHostName(); if (!canonicalHostName.equalsIgnoreCase(inet.getHostAddress())) { return canonicalHostName; } } } return ERROR_HOST_NAME; } catch (SocketException e) { CommonLogger logger = getLogger(); logger.warn(e.getMessage()); return ERROR_HOST_NAME; } }
Example 12
Source File: InetNameLookup.java From ghidra with Apache License 2.0 | 5 votes |
/** * Gets the fully qualified domain name for this IP address or hostname. * Best effort method, meaning we may not be able to return * the FQDN depending on the underlying system configuration. * * @param host IP address or hostname * * @return the fully qualified domain name for this IP address, * or if the operation is not allowed/fails * the original host name specified. * * @throws UnknownHostException the forward lookup of the specified address * failed */ public static String getCanonicalHostName(String host) throws UnknownHostException { String bestGuess = host; if (lookupEnabled) { // host may have multiple IP addresses boolean found = false; long fastest = Long.MAX_VALUE; for (InetAddress addr : InetAddress.getAllByName(host)) { long startTime = System.currentTimeMillis(); String name = addr.getCanonicalHostName(); long elapsedTime = System.currentTimeMillis() - startTime; if (!name.equals(addr.getHostAddress())) { if (host.equalsIgnoreCase(name)) { return name; // name found matches original - use it } bestGuess = name; // name found - update best guess found = true; } else { // keep fastest reverse lookup time fastest = Math.min(fastest, elapsedTime); } } if (!found) { // if lookup failed to produce a name - log warning Msg.warn(InetNameLookup.class, "Failed to resolve IP Address: " + host + " (Reverse DNS may not be properly configured or you may have a network problem)"); if (disableOnFailure && fastest > MAX_TIME_MS) { // if lookup failed and was slow - disable future lookups if disableOnFailure is true Msg.warn(InetNameLookup.class, "Reverse network name lookup has been disabled automatically due to lookup failure."); lookupEnabled = false; } } } return bestGuess; }
Example 13
Source File: NodeUtil.java From kylin-on-parquet-v2 with Apache License 2.0 | 5 votes |
private static String getLocalhostName() { String host; try { InetAddress addr = InetAddress.getLocalHost(); host = addr.getCanonicalHostName(); } catch (UnknownHostException e) { logger.error("Fail to get local ip address", e); host = "UNKNOWN"; } return host; }
Example 14
Source File: DnsStrategy.java From primecloud-controller with GNU General Public License v2.0 | 5 votes |
protected String getHostName(String ipAddress) { byte[] addr = new byte[4]; String[] octets = StringUtils.split(ipAddress, ".", 4); for (int i = 0; i < 4; i++) { addr[i] = (byte) Integer.parseInt(octets[i]); } InetAddress address; try { address = InetAddress.getByAddress(addr); return address.getCanonicalHostName(); } catch (UnknownHostException e) { return null; } }
Example 15
Source File: SimpleServiceIdentityProvider.java From athenz with Apache License 2.0 | 5 votes |
String getServerHostName() { String urlhost; try { InetAddress localhost = getLocalHost(); urlhost = localhost.getCanonicalHostName(); } catch (java.net.UnknownHostException e) { urlhost = "localhost"; } return urlhost; }
Example 16
Source File: SpnegoAuthInterceptor.java From knox with Apache License 2.0 | 5 votes |
private static String getCanonicalHostName(String hostName) { String canonicalHostName; try { InetAddress address = InetAddress.getByName(hostName); if ("localhost".equalsIgnoreCase(address.getHostName())) { canonicalHostName = InetAddress.getLocalHost().getCanonicalHostName(); } else { canonicalHostName = address.getCanonicalHostName(); } } catch (UnknownHostException e) { throw new RuntimeException("Failed to resolve host: " + hostName, e); } return canonicalHostName; }
Example 17
Source File: GFBasicAdapterImpl.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
@Override public String getHostName(InetAddress ip_addr) { return ip_addr.getCanonicalHostName(); }
Example 18
Source File: Network.java From directory-ldap-api with Apache License 2.0 | 4 votes |
private static String getLoopbackHostName() { InetAddress loopbackAddress = InetAddress.getLoopbackAddress(); return loopbackAddress.getCanonicalHostName(); }
Example 19
Source File: AbstractDNSServiceTest.java From james-project with Apache License 2.0 | 4 votes |
@Override public String getHostName(InetAddress inet) { return inet.getCanonicalHostName(); }
Example 20
Source File: RuleBasedLocationResolver.java From carbon-commons with Apache License 2.0 | 4 votes |
public List<Integer> evaluate(TaskServiceContext ctx, TaskInfo taskInfo) { List<Integer> result = new ArrayList<Integer>(); if (ctx.getTaskType().matches(this.getTaskTypePattern())) { if (taskInfo.getName().matches(this.getTaskNamePattern())) { int count = ctx.getServerCount(); InetSocketAddress sockAddr; InetAddress inetAddr; if (log.isDebugEnabled()) { log.debug("Task server count : " + count); log.debug("Address pattern : " + this.addressPattern); } String ip = null, host1, host2 = null, identifier = null; for (int i = 0; i < count; i++) { sockAddr = ctx.getServerAddress(i); identifier = ctx.getServerIdentifier(i); if (sockAddr != null) { host1 = sockAddr.getHostName(); if (log.isDebugEnabled()) { log.debug("Hostname 1 : " + host1); } inetAddr = sockAddr.getAddress(); if (inetAddr != null) { ip = inetAddr.getHostAddress(); host2 = inetAddr.getCanonicalHostName(); if (log.isDebugEnabled()) { log.debug("IP address : " + ip); log.debug("Hostname 1 : " + host2); } } if (host1.matches(this.getAddressPattern())) { if (log.isDebugEnabled()) { log.debug("Hostname 1 matched"); } result.add(i); } else if (ip != null && ip.matches(this.getAddressPattern())) { if (log.isDebugEnabled()) { log.debug("IP address matched"); } result.add(i); } else if (!host1.equals(host2) && host2 != null && host2.matches(this.getAddressPattern())) { if (log.isDebugEnabled()) { log.debug("Hostname 2 matched"); } result.add(i); } else if (identifier != null && identifier.matches(this.getAddressPattern())) { if (log.isDebugEnabled()) { log.debug("localMemberIdentifier : " + identifier); log.debug("localMemberIdentifier matched"); } result.add(i); } } else { log.warn("RuleBasedLocationResolver: cannot find the host address for node: " + i); } } } } return result; }