From d2a2ec589f4728fa2cfa35371155bce7cc95a0a7 Mon Sep 17 00:00:00 2001 From: christ66 Date: Fri, 28 Feb 2014 14:46:28 -0800 Subject: [PATCH] [FIXED JENKINS-21999] If a slave node does not exist it will throw a null pointer exception. Instead we create a dummy launcher and return that if it is unable to get the slave node. --- core/src/main/java/hudson/Launcher.java | 25 ++++++++++++++++++++++ core/src/main/java/hudson/model/Slave.java | 14 +++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/Launcher.java b/core/src/main/java/hudson/Launcher.java index ba599e1753..82ee689002 100644 --- a/core/src/main/java/hudson/Launcher.java +++ b/core/src/main/java/hudson/Launcher.java @@ -841,6 +841,31 @@ public abstract class Launcher { } } + public static class DummyLauncher extends Launcher { + + public DummyLauncher(TaskListener listener) { + super(listener, null); + } + + @Override + public Proc launch(ProcStarter starter) throws IOException { + listener.error("Can not call launch on a dummy launcher."); + return null; + } + + @Override + public Channel launchChannel(String[] cmd, OutputStream out, FilePath workDir, Map envVars) throws IOException, InterruptedException { + listener.error("Can not call launchChannel on a dummy launcher."); + return null; + } + + @Override + public void kill(Map modelEnvVars) throws IOException, InterruptedException { + // Kill method should do nothing. + } + } + + /** * Launches processes remotely by using the given channel. */ diff --git a/core/src/main/java/hudson/model/Slave.java b/core/src/main/java/hudson/model/Slave.java index 5e9204f0a0..e52a6f8af5 100644 --- a/core/src/main/java/hudson/model/Slave.java +++ b/core/src/main/java/hudson/model/Slave.java @@ -343,9 +343,21 @@ public abstract class Slave extends Node implements Serializable { } + /** + * Creates a launcher for the slave. + * + * @return + * If there is no computer it will return a {@link hudson.Launcher.DummyLauncher}, otherwise it + * will return a {@link hudson.Launcher.RemoteLauncher} instead. + */ public Launcher createLauncher(TaskListener listener) { SlaveComputer c = getComputer(); - return new RemoteLauncher(listener, c.getChannel(), c.isUnix()).decorateFor(this); + if (c == null) { + listener.error("Issue with creating launcher for slave " + name + "."); + return new Launcher.DummyLauncher(listener); + } else { + return new RemoteLauncher(listener, c.getChannel(), c.isUnix()).decorateFor(this); + } } /**