Skip to content
Permalink
Browse files

[FIXED JENKINS-12302] Refactor anonymout class for loading groovy scr…

…ipt into separate class as static block (due to inheritance of outer class from CLICommand) initialization leads to NPE (namely in Jenkins.getInstance Jenkins.getInstance().getPluginManager())

(cherry picked from commit 7bdc179)
  • Loading branch information...
vjuranek authored and kohsuke committed Feb 28, 2012
1 parent e1fe985 commit 99385dcd1c8a2dc84d51133f1f4b06d4bc1cf653
@@ -25,6 +25,7 @@

import groovy.lang.GroovyShell;
import groovy.lang.Binding;
import hudson.cli.util.ScriptLoader;
import hudson.model.AbstractProject;
import jenkins.model.Jenkins;
import hudson.model.Item;
@@ -103,26 +104,7 @@ private String loadScript() throws CmdLineException, IOException, InterruptedExc
if (script.equals("="))
return IOUtils.toString(stdin);

return checkChannel().call(new Callable<String,IOException>() {
public String call() throws IOException {
File f = new File(script);
if(f.exists())
return FileUtils.readFileToString(f);

URL url;
try {
url = new URL(script);
} catch (MalformedURLException e) {
throw new AbortException("Unable to find a script "+script);
}
InputStream s = url.openStream();
try {
return IOUtils.toString(s);
} finally {
s.close();
}
}
});
return checkChannel().call(new ScriptLoader(script));
}
}

@@ -0,0 +1,47 @@
package hudson.cli.util;

import hudson.AbortException;
import hudson.remoting.Callable;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

/**
*
* @author vjuranek
*
*/
public class ScriptLoader implements Callable<String,IOException> {

private final String script;

public ScriptLoader(String script){
this.script = script;
}

public String call() throws IOException {
File f = new File(script);
if(f.exists())
return FileUtils.readFileToString(f);

URL url;
try {
url = new URL(script);
} catch (MalformedURLException e) {
throw new AbortException("Unable to find a script "+script);
}
InputStream s = url.openStream();
try {
return IOUtils.toString(s);
} finally {
s.close();
}
}

}

0 comments on commit 99385dc

Please sign in to comment.
You can’t perform that action at this time.