From 8b92f50f216a4362c0834635c46f9aa4d8065713 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Fri, 23 Dec 2011 14:24:25 -0800 Subject: [PATCH] replacing the reference to channel via checkChannel() --- core/src/main/java/hudson/cli/CLICommand.java | 16 +++- .../java/hudson/cli/CommandDuringBuild.java | 2 +- .../main/java/hudson/cli/GroovyCommand.java | 2 +- .../java/hudson/cli/InstallPluginCommand.java | 20 ++-- .../java/hudson/cli/InstallToolCommand.java | 4 +- .../main/java/hudson/cli/LoginCommand.java | 2 +- .../main/java/hudson/cli/LogoutCommand.java | 2 +- .../cli/SetBuildDescriptionCommand.java | 92 +++++++++---------- .../cli/SetBuildDisplayNameCommand.java | 6 +- .../hudson/cli/declarative/CLIRegisterer.java | 1 - .../hudson/model/FileParameterDefinition.java | 2 +- .../hudson/model/ParameterDefinition.java | 2 +- 12 files changed, 77 insertions(+), 74 deletions(-) diff --git a/core/src/main/java/hudson/cli/CLICommand.java b/core/src/main/java/hudson/cli/CLICommand.java index 47506a9944..2f7faa2c3d 100644 --- a/core/src/main/java/hudson/cli/CLICommand.java +++ b/core/src/main/java/hudson/cli/CLICommand.java @@ -131,6 +131,10 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable { *

* Starting 1.445, CLI transports are not required to provide a channel * (think of sshd, telnet, etc), so in such a case this field is null. + * + *

+ * See {@link #checkChannel()} to get a channel and throw an user-friendly + * exception */ public transient Channel channel; @@ -214,6 +218,12 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable { sc.setAuthentication(old); // restore } } + + public Channel checkChannel() throws AbortException { + if (channel==null) + throw new AbortException("This command can only run with Jenkins CLI. See https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+CLI"); + return channel; + } /** * Loads the persisted authentication information from {@link ClientAuthenticationCache} @@ -309,7 +319,7 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable { * Convenience method for subtypes to obtain the system property of the client. */ protected String getClientSystemProperty(String name) throws IOException, InterruptedException { - return channel.call(new GetSystemProperty(name)); + return checkChannel().call(new GetSystemProperty(name)); } private static final class GetSystemProperty implements Callable { @@ -327,7 +337,7 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable { } protected Charset getClientCharset() throws IOException, InterruptedException { - String charsetName = channel.call(new GetCharset()); + String charsetName = checkChannel().call(new GetCharset()); try { return Charset.forName(charsetName); } catch (UnsupportedCharsetException e) { @@ -348,7 +358,7 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable { * Convenience method for subtypes to obtain environment variables of the client. */ protected String getClientEnvironmentVariable(String name) throws IOException, InterruptedException { - return channel.call(new GetEnvironmentVariable(name)); + return checkChannel().call(new GetEnvironmentVariable(name)); } private static final class GetEnvironmentVariable implements Callable { diff --git a/core/src/main/java/hudson/cli/CommandDuringBuild.java b/core/src/main/java/hudson/cli/CommandDuringBuild.java index c18b6d040b..7f1320ec8a 100644 --- a/core/src/main/java/hudson/cli/CommandDuringBuild.java +++ b/core/src/main/java/hudson/cli/CommandDuringBuild.java @@ -46,7 +46,7 @@ public abstract class CommandDuringBuild extends CLICommand { try { CLICommand c = CLICommand.getCurrent(); if (c==null) throw new IllegalStateException("Not executing a CLI command"); - String[] envs = c.channel.call(new GetCharacteristicEnvironmentVariables()); + String[] envs = c.checkChannel().call(new GetCharacteristicEnvironmentVariables()); if (envs[0]==null || envs[1]==null) throw new CmdLineException("This CLI command works only when invoked from inside a build"); diff --git a/core/src/main/java/hudson/cli/GroovyCommand.java b/core/src/main/java/hudson/cli/GroovyCommand.java index 4c46e3a855..c27d62cdbb 100644 --- a/core/src/main/java/hudson/cli/GroovyCommand.java +++ b/core/src/main/java/hudson/cli/GroovyCommand.java @@ -103,7 +103,7 @@ public class GroovyCommand extends CLICommand implements Serializable { if (script.equals("=")) return IOUtils.toString(stdin); - return channel.call(new Callable() { + return checkChannel().call(new Callable() { public String call() throws IOException { File f = new File(script); if(f.exists()) diff --git a/core/src/main/java/hudson/cli/InstallPluginCommand.java b/core/src/main/java/hudson/cli/InstallPluginCommand.java index cfa00ef570..1ee9282538 100644 --- a/core/src/main/java/hudson/cli/InstallPluginCommand.java +++ b/core/src/main/java/hudson/cli/InstallPluginCommand.java @@ -76,15 +76,17 @@ public class InstallPluginCommand extends CLICommand { for (String source : sources) { // is this a file? - FilePath f = new FilePath(channel, source); - if (f.exists()) { - stdout.println(Messages.InstallPluginCommand_InstallingPluginFromLocalFile(f)); - if (name==null) - name = f.getBaseName(); - f.copyTo(getTargetFilePath()); - if (dynamicLoad) - pm.dynamicLoad(getTargetFile()); - continue; + if (channel!=null) { + FilePath f = new FilePath(channel, source); + if (f.exists()) { + stdout.println(Messages.InstallPluginCommand_InstallingPluginFromLocalFile(f)); + if (name==null) + name = f.getBaseName(); + f.copyTo(getTargetFilePath()); + if (dynamicLoad) + pm.dynamicLoad(getTargetFile()); + continue; + } } // is this an URL? diff --git a/core/src/main/java/hudson/cli/InstallToolCommand.java b/core/src/main/java/hudson/cli/InstallToolCommand.java index 94846d576d..d9805c5d96 100644 --- a/core/src/main/java/hudson/cli/InstallToolCommand.java +++ b/core/src/main/java/hudson/cli/InstallToolCommand.java @@ -68,7 +68,7 @@ public class InstallToolCommand extends CLICommand { h.checkPermission(Jenkins.READ); // where is this build running? - BuildIDs id = channel.call(new BuildIDs()); + BuildIDs id = checkChannel().call(new BuildIDs()); if (!id.isComplete()) throw new AbortException("This command can be only invoked from a build executing inside Hudson"); @@ -129,7 +129,7 @@ public class InstallToolCommand extends CLICommand { } if (t instanceof EnvironmentSpecific) { EnvironmentSpecific e = (EnvironmentSpecific) t; - t = (ToolInstallation)e.forEnvironment(EnvVars.getRemote(channel)); + t = (ToolInstallation)e.forEnvironment(EnvVars.getRemote(checkChannel())); } stdout.println(t.getHome()); return 0; diff --git a/core/src/main/java/hudson/cli/LoginCommand.java b/core/src/main/java/hudson/cli/LoginCommand.java index 94e3df83b9..c27b09d510 100644 --- a/core/src/main/java/hudson/cli/LoginCommand.java +++ b/core/src/main/java/hudson/cli/LoginCommand.java @@ -33,7 +33,7 @@ public class LoginCommand extends CLICommand { if (a== Jenkins.ANONYMOUS) throw new CmdLineException("No credentials specified."); // this causes CLI to show the command line options. - ClientAuthenticationCache store = new ClientAuthenticationCache(channel); + ClientAuthenticationCache store = new ClientAuthenticationCache(checkChannel()); store.set(a); return 0; diff --git a/core/src/main/java/hudson/cli/LogoutCommand.java b/core/src/main/java/hudson/cli/LogoutCommand.java index c31642da50..80b2acce9b 100644 --- a/core/src/main/java/hudson/cli/LogoutCommand.java +++ b/core/src/main/java/hudson/cli/LogoutCommand.java @@ -17,7 +17,7 @@ public class LogoutCommand extends CLICommand { @Override protected int run() throws Exception { - ClientAuthenticationCache store = new ClientAuthenticationCache(channel); + ClientAuthenticationCache store = new ClientAuthenticationCache(checkChannel()); store.remove(); return 0; } diff --git a/core/src/main/java/hudson/cli/SetBuildDescriptionCommand.java b/core/src/main/java/hudson/cli/SetBuildDescriptionCommand.java index b81c2b6521..0aeedb4a37 100644 --- a/core/src/main/java/hudson/cli/SetBuildDescriptionCommand.java +++ b/core/src/main/java/hudson/cli/SetBuildDescriptionCommand.java @@ -1,48 +1,44 @@ -package hudson.cli; - -import hudson.Extension; -import hudson.model.AbstractProject; -import hudson.model.Run; -import hudson.remoting.Callable; - -import java.io.IOException; -import java.io.Serializable; - -import org.apache.commons.io.IOUtils; -import org.kohsuke.args4j.Argument; - -@Extension -public class SetBuildDescriptionCommand extends CLICommand implements Serializable { - - @Override - public String getShortDescription() { - return Messages.SetBuildDescriptionCommand_ShortDescription(); - } - - @Argument(metaVar="JOB",usage="Name of the job to build",required=true,index=0) - public transient AbstractProject job; - - @Argument(metaVar="BUILD#",usage="Number of the build",required=true,index=1) - public int number; - - @Argument(metaVar="DESCRIPTION",required=true,usage="Description to be set. '=' to read from stdin.", index=2) - public String description; - - protected int run() throws Exception { - Run run = job.getBuildByNumber(number); - run.checkPermission(Run.UPDATE); - - if ("=".equals(description)) { - description = channel.call(new Callable() { - public String call() throws IOException { - return IOUtils.toString(System.in); - } - }); - } - - run.setDescription(description); - - return 0; - } - -} +package hudson.cli; + +import hudson.Extension; +import hudson.model.AbstractProject; +import hudson.model.Run; +import hudson.remoting.Callable; + +import java.io.IOException; +import java.io.Serializable; + +import org.apache.commons.io.IOUtils; +import org.kohsuke.args4j.Argument; + +@Extension +public class SetBuildDescriptionCommand extends CLICommand implements Serializable { + + @Override + public String getShortDescription() { + return Messages.SetBuildDescriptionCommand_ShortDescription(); + } + + @Argument(metaVar="JOB",usage="Name of the job to build",required=true,index=0) + public transient AbstractProject job; + + @Argument(metaVar="BUILD#",usage="Number of the build",required=true,index=1) + public int number; + + @Argument(metaVar="DESCRIPTION",required=true,usage="Description to be set. '=' to read from stdin.", index=2) + public String description; + + protected int run() throws Exception { + Run run = job.getBuildByNumber(number); + run.checkPermission(Run.UPDATE); + + if ("=".equals(description)) { + description = IOUtils.toString(stdin); + } + + run.setDescription(description); + + return 0; + } + +} diff --git a/core/src/main/java/hudson/cli/SetBuildDisplayNameCommand.java b/core/src/main/java/hudson/cli/SetBuildDisplayNameCommand.java index 09b230c938..5233cf7f7f 100644 --- a/core/src/main/java/hudson/cli/SetBuildDisplayNameCommand.java +++ b/core/src/main/java/hudson/cli/SetBuildDisplayNameCommand.java @@ -32,11 +32,7 @@ public class SetBuildDisplayNameCommand extends CLICommand implements Serializab run.checkPermission(Run.UPDATE); if ("-".equals(displayName)) { - displayName = channel.call(new Callable() { - public String call() throws IOException { - return IOUtils.toString(System.in); - } - }); + displayName = IOUtils.toString(stdin); } run.setDisplayName(displayName); diff --git a/core/src/main/java/hudson/cli/declarative/CLIRegisterer.java b/core/src/main/java/hudson/cli/declarative/CLIRegisterer.java index c7908ea199..9cc5448616 100644 --- a/core/src/main/java/hudson/cli/declarative/CLIRegisterer.java +++ b/core/src/main/java/hudson/cli/declarative/CLIRegisterer.java @@ -120,7 +120,6 @@ public class CLIRegisterer extends ExtensionFinder { this.stdout = stdout; this.stderr = stderr; this.locale = locale; - this.channel = Channel.current(); registerOptionHandlers(); CmdLineParser parser = new CmdLineParser(null); diff --git a/core/src/main/java/hudson/model/FileParameterDefinition.java b/core/src/main/java/hudson/model/FileParameterDefinition.java index 0f0876b39d..1ff4e31d14 100644 --- a/core/src/main/java/hudson/model/FileParameterDefinition.java +++ b/core/src/main/java/hudson/model/FileParameterDefinition.java @@ -75,7 +75,7 @@ public class FileParameterDefinition extends ParameterDefinition { @Override public ParameterValue createValue(CLICommand command, String value) throws IOException, InterruptedException { // capture the file to the server - FilePath src = new FilePath(command.channel,value); + FilePath src = new FilePath(command.checkChannel(),value); File local = File.createTempFile("hudson","parameter"); src.copyTo(new FilePath(local)); diff --git a/core/src/main/java/hudson/model/ParameterDefinition.java b/core/src/main/java/hudson/model/ParameterDefinition.java index 1923fc9723..81e04e686d 100644 --- a/core/src/main/java/hudson/model/ParameterDefinition.java +++ b/core/src/main/java/hudson/model/ParameterDefinition.java @@ -173,7 +173,7 @@ public abstract class ParameterDefinition implements * Create a parameter value from the string given in the CLI. * * @param command - * This is the command that got the parameter. You can use its {@link CLICommand#channel} + * This is the command that got the parameter. You can use its {@link CLICommand#checkChannel()} * for interacting with the CLI JVM. * @throws AbortException * If the CLI processing should be aborted. Hudson will report the error message -- GitLab