Skip to content

Commit

Permalink
fix JENKINS-13979
Browse files Browse the repository at this point in the history
  • Loading branch information
hayato1980 committed Jun 1, 2012
1 parent 159b018 commit e00d375
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
Expand Up @@ -13,6 +13,7 @@
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;

import java.io.PrintWriter;
import java.io.Serializable;
import java.util.HashSet;
import java.util.LinkedList;
Expand Down Expand Up @@ -84,9 +85,13 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
for (Parameter parameter : parameters) {
expandedParams.add(new Parameter(parameter.getName(), TokenMacro.expandAll(build, listener, parameter.getValue())));
}
final String output = launcher.getChannel().call(new GroovyScript(script.script, expandedParams.toArray(new Parameter[expandedParams.size()]), true));
listener.getLogger().print(output);
isOk = true;
PrintWriter pw = new PrintWriter(listener.getLogger());
final Object output = launcher.getChannel().call(new GroovyScript(script.script, expandedParams.toArray(new Parameter[expandedParams.size()]), true, pw));
if (output instanceof Boolean && Boolean.FALSE.equals(output)) {
isOk = false;
} else {
isOk = true;
}
} catch (Exception e) {
listener.getLogger().print(Messages.scriptExecutionFailed(scriptId) + " - " + e.getMessage());
}
Expand Down
Expand Up @@ -3,8 +3,8 @@
import groovy.lang.GroovyShell;
import hudson.remoting.DelegatingCallable;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;

import jenkins.model.Jenkins;

Expand All @@ -14,36 +14,35 @@
/**
* Inspired by hudson.util.RemotingDiagnostics.Script, but adding parameters.
*/
public final class GroovyScript implements DelegatingCallable<String, RuntimeException> {
public class GroovyScript implements DelegatingCallable<Object, RuntimeException> {
private static final long serialVersionUID = 1L;
private final String script;
private final Parameter[] parameters;
private final boolean failWithException;
private final PrintWriter pw;
private transient ClassLoader cl;

private static final String PW_PARAM_VARIABLE = "out";

public GroovyScript(String script, Parameter[] parameters, boolean failWithException) {
public GroovyScript(String script, Parameter[] parameters, boolean failWithException, PrintWriter pw) {
this.script = script;
this.parameters = parameters;
this.failWithException = failWithException;
this.pw = pw;
cl = getClassLoader();
}

public ClassLoader getClassLoader() {
return Jenkins.getInstance().getPluginManager().uberClassLoader;
}

public String call() throws RuntimeException {
public Object call() throws RuntimeException {
// if we run locally, cl!=null. Otherwise the delegating classloader will be available as context classloader.
if (cl == null) {
cl = Thread.currentThread().getContextClassLoader();
}
GroovyShell shell = new GroovyShell(cl);

StringWriter out = new StringWriter();
PrintWriter pw = new PrintWriter(out);

for (Parameter param : parameters) {
final String paramName = param.getName();
if (PW_PARAM_VARIABLE.equals(paramName)) {
Expand All @@ -57,14 +56,17 @@ public String call() throws RuntimeException {
Object output = shell.evaluate(script);
if (output != null) {
pw.println(Messages.resultPrefix() + " " + output);
return output;
} else {
return "";
}
} catch (Throwable t) {
if (failWithException) {
throw new ScriptlerExecutionException(t);
}
t.printStackTrace(pw);
return Boolean.FALSE;
}
return out.toString();
}

private static final class ScriptlerExecutionException extends RuntimeException {
Expand Down
Expand Up @@ -2,12 +2,15 @@

import hudson.model.Computer;
import hudson.model.Hudson;
import hudson.util.RemotingDiagnostics;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -84,15 +87,17 @@ public static String runScript(String[] slaves, String scriptTxt, Parameter[] pa
*/
public static String runScript(String node, String scriptTxt, Parameter[] parameters) throws IOException, ServletException {

String output = "[no output]";
Object output = "[no output]";
if (node != null && scriptTxt != null) {

try {

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
Computer comp = Hudson.getInstance().getComputer(node);
if (comp == null && "(master)".equals(node)) {
// output = RemotingDiagnostics.executeGroovy(scriptTxt, MasterComputer.localChannel);
output = MasterComputer.localChannel.call(new GroovyScript(scriptTxt, parameters, false));
output = MasterComputer.localChannel.call(new GroovyScript(scriptTxt, parameters, false, pw));
} else if (comp == null) {
output = Messages.node_not_found(node) + "\n";
} else {
Expand All @@ -101,15 +106,15 @@ public static String runScript(String node, String scriptTxt, Parameter[] parame
}

else {
output = comp.getChannel().call(new GroovyScript(scriptTxt, parameters, false));
output = comp.getChannel().call(new GroovyScript(scriptTxt, parameters, false, pw));
}
}

} catch (InterruptedException e) {
throw new ServletException(e);
}
}
return output;
return output.toString();
}

}

0 comments on commit e00d375

Please sign in to comment.