Example usage for java.net InetAddress getCanonicalHostName

List of usage examples for java.net InetAddress getCanonicalHostName

Introduction

In this page you can find the example usage for java.net InetAddress getCanonicalHostName.

Prototype

public String getCanonicalHostName() 

Source Link

Document

Gets the fully qualified domain name for this IP address.

Usage

From source file:org.blue.star.plugins.check_ping.java

public boolean execute_check() {

    for (String hostname : addresses) {

        try {/*from w w  w .  jav a 2  s. com*/

            InetAddress inet = InetAddress.getByName(hostname);

            long execute_time = 0;
            long packet_loss = 0;
            for (int pings = 0; pings < max_packets; pings++) {
                boolean reachable = false;
                long ping_time = System.currentTimeMillis();
                if (network_interface != null) {
                    reachable = inet.isReachable(network_interface, 0, utils_h.timeout_interval);
                } else {
                    reachable = inet.isReachable(utils_h.timeout_interval);
                }
                execute_time += (System.currentTimeMillis() - ping_time);
                if (!reachable)
                    packet_loss++;
            }
            rta = execute_time / max_packets;
            pl = (int) packet_loss / max_packets * 100;

            if (verbose > 0) {
                System.out.println("rta = " + rta);
                System.out.println("pl = " + pl);
            }

            if (verbose > 1) {
                System.out.println("isAnyLocalAddress = " + inet.isAnyLocalAddress());
                System.out.println("isLinkLocalAddress = " + inet.isLinkLocalAddress());
                System.out.println("isLoopbackAddress = " + inet.isLoopbackAddress());
                System.out.println("isMCGlobal = " + inet.isMCGlobal());
                System.out.println("isMCLinkLocal = " + inet.isMCLinkLocal());
                System.out.println("isMCNodeLocal = " + inet.isMCNodeLocal());
                System.out.println("isMCOrgLocal = " + inet.isMCOrgLocal());
                System.out.println("isMCSiteLocal = " + inet.isMCSiteLocal());
                System.out.println("isMulticastAddress = " + inet.isMulticastAddress());
                System.out.println("isSiteLocalAddress = " + inet.isSiteLocalAddress());
                System.out.println("isReachable = " + inet.isReachable(utils_h.timeout_interval));
                System.out.println("getCanonicalHostName = " + inet.getCanonicalHostName());
                System.out.println("getHostAddress = " + inet.getHostAddress());
                System.out.println("getHostName = " + inet.getHostName());
                System.out.println("getClass.getName = " + inet.getClass().getName());
            }

            /* The list is only used to check alternatives, if we pass don't do more */
            if (packet_loss != max_packets)
                break;

        } catch (Exception e) {
            warn_text = e.getMessage();
            e.printStackTrace();
        }

    }

    if (pl >= cpl || rta >= crta || rta < 0)
        check_state = common_h.STATE_CRITICAL;
    else if (pl >= wpl || rta >= wrta)
        check_state = common_h.STATE_WARNING;
    else if (pl >= 0 && rta >= 0)
        check_state = common_h.STATE_OK;

    return true;
}

From source file:tachyon.master.MasterInfo.java

/**
 * Get the address of a worker.//from   w  w w .ja  v  a  2s.  c om
 *
 * @param random If true, select a random worker
 * @param host If <code>random</code> is false, select a worker on this host
 * @return the address of the selected worker, or null if no address could be found
 */
public NetAddress getWorker(boolean random, String host) throws UnknownHostException {
    synchronized (mWorkers) {
        if (mWorkerAddressToId.isEmpty()) {
            return null;
        }
        if (random) {
            int index = new Random(mWorkerAddressToId.size()).nextInt(mWorkerAddressToId.size());
            for (NetAddress address : mWorkerAddressToId.keySet()) {
                if (index == 0) {
                    LOG.debug("getRandomWorker: {}", address);
                    return address;
                }
                index--;
            }
            for (NetAddress address : mWorkerAddressToId.keySet()) {
                LOG.debug("getRandomWorker: {}", address);
                return address;
            }
        } else {
            for (NetAddress address : mWorkerAddressToId.keySet()) {
                InetAddress inetAddress = InetAddress.getByName(address.getMHost());
                if (inetAddress.getHostName().equals(host) || inetAddress.getHostAddress().equals(host)
                        || inetAddress.getCanonicalHostName().equals(host)) {
                    LOG.debug("getLocalWorker: {}" + address);
                    return address;
                }
            }
        }
    }
    LOG.info("getLocalWorker: no local worker on " + host);
    return null;
}

From source file:org.apache.geode.distributed.ServerLauncher.java

/**
 * 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./*from www . j  a va  2  s.  c om*/
 *
 * @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 handled) {
        // 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";
    }
}

From source file:org.apache.manifoldcf.crawler.connectors.webcrawler.WebcrawlerConnector.java

/** Look up an ipaddress given a non-canonical host name.
*@return appropriate status.//  w  ww . j a v  a 2  s  . c o m
*/
protected int lookupIPAddress(String documentIdentifier, IProcessActivity activities, String hostName,
        long currentTime, StringBuilder ipAddressBuffer) throws ManifoldCFException, ServiceInterruption {
    String eventName = makeDNSEventName(activities, hostName);
    DNSManager.DNSInfo info = dnsManager.lookup(hostName, currentTime);
    if (info != null) {
        String ipAddress = info.getIPAddress();
        if (ipAddress == null)
            return RESULTSTATUS_FALSE;
        ipAddressBuffer.append(ipAddress);
        return RESULTSTATUS_TRUE;
    }
    if (activities.beginEventSequence(eventName)) {
        //  We uniquely can do the lookup.
        try {
            // Fetch it using InetAddress
            InetAddress ip = null;
            try {
                ip = InetAddress.getByName(hostName);
            } catch (UnknownHostException e) {
                // Host is unknown, so leave ipAddress as null.
            }
            String fqdn = null;
            String ipAddress = null;
            if (ip != null) {
                fqdn = ip.getCanonicalHostName();
                ipAddress = ip.getHostAddress();
            }
            // Write this to the cache - expiration time 6 hours
            dnsManager.writeDNSData(hostName, fqdn, ipAddress, currentTime + 1000 * 60 * 60 * 6);
            if (ipAddress == null)
                return RESULTSTATUS_FALSE;
            ipAddressBuffer.append(ipAddress);
            return RESULTSTATUS_TRUE;
        } finally {
            activities.completeEventSequence(eventName);
        }
    } else {
        // Abort this fetch, since it's blocked.
        return RESULTSTATUS_NOTYETDETERMINED;
    }
}