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())
  • Loading branch information
vjuranek committed Feb 28, 2012
1 parent 707490a commit 7bdc179
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();
}
}

}

2 comments on commit 7bdc179

@ingorichter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, according to all other commands, it would be a good idea if GroovyCommand doesn't implement Serializable anymore, since this is a bit misleading now.

@vjuranek
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, thanks for spotting it. Removed in a77011e

Please sign in to comment.