From 57b043b7624e21b3f44331e173124cb0a7423d9b Mon Sep 17 00:00:00 2001 From: kohsuke Date: Sat, 11 Apr 2009 22:59:51 +0000 Subject: [PATCH] added a method to compute the host name. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@17083 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/model/Computer.java | 56 ++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/model/Computer.java b/core/src/main/java/hudson/model/Computer.java index b648d33653..eb95dd6868 100644 --- a/core/src/main/java/hudson/model/Computer.java +++ b/core/src/main/java/hudson/model/Computer.java @@ -57,12 +57,17 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Enumeration; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.logging.LogRecord; +import java.util.logging.Level; +import java.util.logging.Logger; import java.nio.charset.Charset; +import java.net.InetAddress; +import java.net.NetworkInterface; /** * Represents the running state of a remote computer that holds {@link Executor}s. @@ -552,6 +557,55 @@ public abstract class Computer extends AbstractModelObject implements AccessCont return RemotingDiagnostics.getThreadDump(getChannel()); } + /** + * This method tries to compute the name of the host that's reachable by all the other nodes. + * + *

+ * Since it's possible that the slave is not reachable from the master (it may be behind a firewall, + * connecting to master via JNLP), in which case this method returns 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. + * + *

+ * So the technique here is to compute possible interfaces and names on the slave, + * then try to ping them from the master, and pick the one that worked. + * + * @since 1.300 + */ + public String getHostName() throws IOException, InterruptedException { + for( String address : getChannel().call(new ListPossibleNames())) { + try { + InetAddress ia = InetAddress.getByName(address); + if(ia.isReachable(500)) + return ia.getCanonicalHostName(); + } catch (IOException e) { + // if a given name fails to parse on this host, we get this error + LOGGER.log(Level.FINE, "Failed to parse "+address,e); + } + } + return null; + } + + private static class ListPossibleNames implements Callable,IOException> { + public List call() throws IOException { + List names = new ArrayList(); + + Enumeration nis = NetworkInterface.getNetworkInterfaces(); + while (nis.hasMoreElements()) { + NetworkInterface ni = nis.nextElement(); + Enumeration e = ni.getInetAddresses(); + while (e.hasMoreElements()) { + InetAddress ia = e.nextElement(); + if(ia.isLoopbackAddress()) continue; + names.add(ia.getHostAddress()); + } + } + return names; + } + private static final long serialVersionUID = 1L; + } + public static final ExecutorService threadPoolForRemoting = Executors.newCachedThreadPool(new ExceptionCatchingThreadFactory(new DaemonThreadFactory())); // @@ -718,5 +772,5 @@ public abstract class Computer extends AbstractModelObject implements AccessCont public static final Permission CONFIGURE = new Permission(PERMISSIONS,"Configure", Messages._Computer_ConfigurePermission_Description(), Permission.CONFIGURE); public static final Permission DELETE = new Permission(PERMISSIONS,"Delete", Messages._Computer_DeletePermission_Description(), Permission.DELETE); - + private static final Logger LOGGER = Logger.getLogger(Computer.class.getName()); } -- GitLab