Skip to content
Permalink
Browse files
Fix JENKINS-13542
  • Loading branch information
gboissinot committed Apr 25, 2012
1 parent 51f4cd1 commit 60c3a2407be16529b4da0c7dc18d62e96a7960fe
Showing with 34 additions and 27 deletions.
  1. +34 −27 src/main/java/org/jenkinsci/plugins/scripttrigger/groovy/GroovyScriptTriggerExecutor.java
@@ -1,23 +1,22 @@
package org.jenkinsci.plugins.scripttrigger.groovy;

import groovy.lang.GroovyShell;
import hudson.Util;
import hudson.PluginManager;
import hudson.Util;
import hudson.model.AbstractProject;
import hudson.model.Hudson;
import hudson.model.Node;
import hudson.remoting.Callable;
import hudson.util.IOUtils;
import org.jenkinsci.lib.xtrigger.XTriggerLog;
import org.jenkinsci.plugins.scripttrigger.ScriptTriggerException;
import org.jenkinsci.plugins.scripttrigger.ScriptTriggerExecutor;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;

import org.jenkinsci.lib.xtrigger.XTriggerLog;
import org.jenkinsci.plugins.scripttrigger.ScriptTriggerException;
import org.jenkinsci.plugins.scripttrigger.ScriptTriggerExecutor;

/**
* @author Gregory Boissinot
*/
@@ -37,7 +36,7 @@ public boolean evaluateGroovyScript(Node executingNode, final AbstractProject pr
log.info("Running as system script");
return evaluateGroovyScript(proj, scriptContent, envVars);
}

return executingNode.getRootPath().act(new Callable<Boolean, ScriptTriggerException>() {
public Boolean call() throws ScriptTriggerException {
log.info("Running as node script");
@@ -65,94 +64,102 @@ private boolean evaluateGroovyScript(final AbstractProject proj, final String sc
envDebug.append("\n\t").append(envEntry.getKey()).append("=").append(envEntry.getValue());
}
log.info(envDebug.toString());

final String groovyExpressionResolved = Util.replaceMacro(scriptContent, envVars);

log.info("Evaluating the groovy script:");
log.info("---------- Base Script -----------------");
log.info(scriptContent);

String groovyExpressionResolved = Util.replaceMacro(scriptContent, envVars);
groovyExpressionResolved = processPath(groovyExpressionResolved);

log.info("---------- Resolved Script -------------");
log.info(groovyExpressionResolved);
log.info("----------------------------------------\n");

final ClassLoader cl = getClassLoader();

GroovyShell shell = new GroovyShell(cl);

shell.setVariable("log", log);
shell.setVariable("out", log.getListener().getLogger());
if (proj != null) {
shell.setVariable("project", proj);
}

//Evaluate the new script content
Object result = shell.evaluate(groovyExpressionResolved);
//Return the evaluated result
return Boolean.valueOf(String.valueOf(result));
}


private String processPath(String content) {
if (content == null) {
return null;
}
return content.replace("\\", "\\\\");
}

protected ClassLoader getClassLoader() {
final Hudson instance = Hudson.getInstance();
if (instance == null) {
log.info("No Hudson Instance available, returning thread context classloader");
return Thread.currentThread().getContextClassLoader();

}

final PluginManager pluginManager = instance.getPluginManager();
if (pluginManager == null) {
log.info("No PluginManager available, returning thread context classloader");
return Thread.currentThread().getContextClassLoader();
}

final ClassLoader cl = pluginManager.uberClassLoader;
if (cl == null) {
log.info("No uberClassLoader available, returning thread context classloader");
return Thread.currentThread().getContextClassLoader();
}

return cl;
}

public boolean evaluateGroovyScriptFilePath(Node executingNode, AbstractProject proj, String scriptFilePath, Map<String, String> envVars, boolean groovySystemScript) throws ScriptTriggerException {

if (scriptFilePath == null) {
throw new NullPointerException("The scriptFilePath object must be set.");
}

final String scriptContent;
if (groovySystemScript) {
String expandedScriptFile = Util.replaceMacro(scriptFilePath, envVars);

final File file = new File(expandedScriptFile);
final String scriptPath = file.getAbsolutePath();

if (!file.exists()) {
log.info(String.format("Can't load the file '%s'. It doesn't exist.", scriptPath));
return false;
}

log.info("Reading script from: " + file.getAbsolutePath());
try {
final FileInputStream fis = new FileInputStream(file);
try {
scriptContent = IOUtils.toString(fis);
}
finally {
} finally {
fis.close();
}
log.info("Read " + scriptContent.length() + " character long script from: " + scriptPath);
}
catch (IOException e) {
log.info("Read " + scriptContent.length() + " character long script from: " + scriptPath);
} catch (IOException e) {
final String msg = "Failed to read system groovy script file '" + scriptFilePath + "' from '" + scriptPath + "'";
log.info(msg);
e.printStackTrace(log.getListener().getLogger());
throw new RuntimeException(msg, e);
}
}
else {
} else {
if (!existsScript(executingNode, scriptFilePath)) {
return false;
}

scriptContent = getStringContent(executingNode, scriptFilePath);
}

0 comments on commit 60c3a24

Please sign in to comment.