Skip to content

Commit 7bdc179

Browse files
committedFeb 28, 2012
[FIXED JENKINS-12302] Refactor anonymout class for loading groovy script 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())
1 parent 707490a commit 7bdc179

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed
 

‎core/src/main/java/hudson/cli/GroovyCommand.java

+2-20
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

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

106-
return checkChannel().call(new Callable<String,IOException>() {
107-
public String call() throws IOException {
108-
File f = new File(script);
109-
if(f.exists())
110-
return FileUtils.readFileToString(f);
111-
112-
URL url;
113-
try {
114-
url = new URL(script);
115-
} catch (MalformedURLException e) {
116-
throw new AbortException("Unable to find a script "+script);
117-
}
118-
InputStream s = url.openStream();
119-
try {
120-
return IOUtils.toString(s);
121-
} finally {
122-
s.close();
123-
}
124-
}
125-
});
107+
return checkChannel().call(new ScriptLoader(script));
126108
}
127109
}
128110

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package hudson.cli.util;
2+
3+
import hudson.AbortException;
4+
import hudson.remoting.Callable;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.net.MalformedURLException;
10+
import java.net.URL;
11+
12+
import org.apache.commons.io.FileUtils;
13+
import org.apache.commons.io.IOUtils;
14+
15+
/**
16+
*
17+
* @author vjuranek
18+
*
19+
*/
20+
public class ScriptLoader implements Callable<String,IOException> {
21+
22+
private final String script;
23+
24+
public ScriptLoader(String script){
25+
this.script = script;
26+
}
27+
28+
public String call() throws IOException {
29+
File f = new File(script);
30+
if(f.exists())
31+
return FileUtils.readFileToString(f);
32+
33+
URL url;
34+
try {
35+
url = new URL(script);
36+
} catch (MalformedURLException e) {
37+
throw new AbortException("Unable to find a script "+script);
38+
}
39+
InputStream s = url.openStream();
40+
try {
41+
return IOUtils.toString(s);
42+
} finally {
43+
s.close();
44+
}
45+
}
46+
47+
}

0 commit comments

Comments
 (0)