Example usage for java.util.logging LogRecord LogRecord

List of usage examples for java.util.logging LogRecord LogRecord

Introduction

In this page you can find the example usage for java.util.logging LogRecord LogRecord.

Prototype

public LogRecord(Level level, String msg) 

Source Link

Document

Construct a LogRecord with the given level and message values.

Usage

From source file:hudson.model.Computer.java

/**
 * This method tries to compute the name of the host that's reachable by all the other nodes.
 *
 * <p>//from w ww  .ja  v  a 2 s . c o m
 * Since it's possible that the agent is not reachable from the master (it may be behind a firewall,
 * connecting to master via JNLP), this method may return null.
 *
 * It's surprisingly tricky for a machine to know a name that other systems can get to,
 * especially between things like DNS search suffix, the hosts file, and YP.
 *
 * <p>
 * So the technique here is to compute possible interfaces and names on the agent,
 * then try to ping them from the master, and pick the one that worked.
 *
 * <p>
 * The computation may take some time, so it employs caching to make the successive lookups faster.
 *
 * @since 1.300
 * @return
 *      null if the host name cannot be computed (for example because this computer is offline,
 *      because the agent is behind the firewall, etc.)
 */
public String getHostName() throws IOException, InterruptedException {
    if (hostNameCached)
        // in the worst case we end up having multiple threads computing the host name simultaneously, but that's not harmful, just wasteful.
        return cachedHostName;

    VirtualChannel channel = getChannel();
    if (channel == null)
        return null; // can't compute right now

    for (String address : channel.call(new ListPossibleNames())) {
        try {
            InetAddress ia = InetAddress.getByName(address);
            if (!(ia instanceof Inet4Address)) {
                LOGGER.log(Level.FINE, "{0} is not an IPv4 address", address);
                continue;
            }
            if (!ComputerPinger.checkIsReachable(ia, 3)) {
                LOGGER.log(Level.FINE, "{0} didn't respond to ping", address);
                continue;
            }
            cachedHostName = ia.getCanonicalHostName();
            hostNameCached = true;
            return cachedHostName;
        } catch (IOException e) {
            // if a given name fails to parse on this host, we get this error
            LogRecord lr = new LogRecord(Level.FINE, "Failed to parse {0}");
            lr.setThrown(e);
            lr.setParameters(new Object[] { address });
            LOGGER.log(lr);
        }
    }

    // allow the administrator to manually specify the host name as a fallback. HUDSON-5373
    cachedHostName = channel.call(new GetFallbackName());
    hostNameCached = true;
    return cachedHostName;
}