Skip to content

Commit

Permalink
[FIXED JENKINS-12302] Refactor anonymout class for loading groovy scr…
Browse files Browse the repository at this point in the history
…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 Mar 19, 2012
1 parent e1fe985 commit 99385dc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
22 changes: 2 additions & 20 deletions core/src/main/java/hudson/cli/GroovyCommand.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

47 changes: 47 additions & 0 deletions core/src/main/java/hudson/cli/util/ScriptLoader.java
@@ -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.