提交 8b92f50f 编写于 作者: K Kohsuke Kawaguchi

replacing the reference to channel via checkChannel()

上级 e45f734e
......@@ -131,6 +131,10 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable {
* <p>
* 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.
*
* <p>
* 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<String, IOException> {
......@@ -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<String, IOException> {
......
......@@ -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");
......
......@@ -103,7 +103,7 @@ public class GroovyCommand extends CLICommand implements Serializable {
if (script.equals("="))
return IOUtils.toString(stdin);
return channel.call(new Callable<String,IOException>() {
return checkChannel().call(new Callable<String,IOException>() {
public String call() throws IOException {
File f = new File(script);
if(f.exists())
......
......@@ -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?
......
......@@ -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;
......
......@@ -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;
......
......@@ -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;
}
......
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<String,IOException>() {
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;
}
}
......@@ -32,11 +32,7 @@ public class SetBuildDisplayNameCommand extends CLICommand implements Serializab
run.checkPermission(Run.UPDATE);
if ("-".equals(displayName)) {
displayName = channel.call(new Callable<String, IOException>() {
public String call() throws IOException {
return IOUtils.toString(System.in);
}
});
displayName = IOUtils.toString(stdin);
}
run.setDisplayName(displayName);
......
......@@ -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);
......
......@@ -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));
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册