Package com.google.gerrit.sshd
Class BaseCommand
- java.lang.Object
-
- com.google.gerrit.sshd.BaseCommand
-
- All Implemented Interfaces:
org.apache.sshd.server.command.Command
,org.apache.sshd.server.command.CommandLifecycle
- Direct Known Subclasses:
AbstractGitCommand
,AliasCommand
,SshCommand
,StreamEvents
,SuExec
public abstract class BaseCommand extends Object implements org.apache.sshd.server.command.Command
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
BaseCommand.CommandRunnable
Runnable function which can throw an exception.static class
BaseCommand.Failure
Thrown fromBaseCommand.CommandRunnable.run()
with client message and code.static interface
BaseCommand.ProjectCommandRunnable
Runnable function which can retrieve a project name related to the taskstatic class
BaseCommand.UnloggedFailure
Thrown fromBaseCommand.CommandRunnable.run()
with client message and code.
-
Field Summary
Fields Modifier and Type Field Description protected RequestCleanup
cleanup
static Charset
ENC
protected OutputStream
err
protected org.apache.sshd.server.ExitCallback
exit
protected InputStream
in
protected OutputStream
out
static int
STATUS_NOT_ADMIN
protected CurrentUser
user
-
Constructor Summary
Constructors Constructor Description BaseCommand()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
destroy(org.apache.sshd.server.channel.ChannelSession channel)
protected BaseCommand.UnloggedFailure
die(String msg)
protected BaseCommand.UnloggedFailure
die(String msg, Throwable why)
protected BaseCommand.UnloggedFailure
die(Throwable why)
protected void
enableGracefulStop()
String[]
getArguments()
protected String
getName()
protected String
getPluginName()
protected String
getTaskDescription()
protected String[]
getTrimmedArguments()
Trim the argument if it is spanning multiple lines.protected com.google.gerrit.util.cli.CmdLineParser
newCmdLineParser(Object options)
Construct a new parser for this command's received command line.protected void
onExit(int rc)
Terminate this command and return a result code to the remote client.protected void
parseCommandLine()
Parses the command line argument, injecting parsed values into fields.protected void
parseCommandLine(Object options)
Parses the command line argument, injecting parsed values into fields.protected void
provideStateTo(org.apache.sshd.server.command.Command cmd)
Pass all state into the command, then run its start method.void
setArguments(String[] argv)
void
setErrorStream(OutputStream err)
void
setExitCallback(org.apache.sshd.server.ExitCallback callback)
void
setInputStream(InputStream in)
void
setOutputStream(OutputStream out)
protected void
startThread(BaseCommand.CommandRunnable thunk, AccessPath accessPath)
Spawn a function into its own thread.protected static PrintWriter
toPrintWriter(OutputStream o)
Wrap the supplied output stream in a UTF-8 encoded PrintWriter.protected String
usage()
protected void
writeError(String type, String msg)
-
-
-
Field Detail
-
ENC
public static final Charset ENC
-
STATUS_NOT_ADMIN
public static final int STATUS_NOT_ADMIN
- See Also:
- Constant Field Values
-
in
protected InputStream in
-
out
protected OutputStream out
-
err
protected OutputStream err
-
exit
protected org.apache.sshd.server.ExitCallback exit
-
user
@Inject protected CurrentUser user
-
cleanup
@Inject protected RequestCleanup cleanup
-
-
Method Detail
-
setInputStream
public void setInputStream(InputStream in)
- Specified by:
setInputStream
in interfaceorg.apache.sshd.server.command.Command
-
setOutputStream
public void setOutputStream(OutputStream out)
- Specified by:
setOutputStream
in interfaceorg.apache.sshd.server.command.Command
-
setErrorStream
public void setErrorStream(OutputStream err)
- Specified by:
setErrorStream
in interfaceorg.apache.sshd.server.command.Command
-
setExitCallback
public void setExitCallback(org.apache.sshd.server.ExitCallback callback)
- Specified by:
setExitCallback
in interfaceorg.apache.sshd.server.command.Command
-
getPluginName
protected String getPluginName()
-
getName
protected String getName()
-
getArguments
public String[] getArguments()
-
setArguments
public void setArguments(String[] argv)
-
getTrimmedArguments
protected String[] getTrimmedArguments()
Trim the argument if it is spanning multiple lines.- Returns:
- the arguments where all the multiple-line fields are trimmed.
-
destroy
public void destroy(org.apache.sshd.server.channel.ChannelSession channel)
- Specified by:
destroy
in interfaceorg.apache.sshd.server.command.CommandLifecycle
-
provideStateTo
protected void provideStateTo(org.apache.sshd.server.command.Command cmd)
Pass all state into the command, then run its start method.This method copies all critical state, like the input and output streams, into the supplied command. The caller must still invoke
cmd.start()
if wants to pass control to the command.- Parameters:
cmd
- the command that will receive the current state.
-
parseCommandLine
protected void parseCommandLine() throws BaseCommand.UnloggedFailure
Parses the command line argument, injecting parsed values into fields.This method must be explicitly invoked to cause a parse.
- Throws:
BaseCommand.UnloggedFailure
- if the command line arguments were invalid.- See Also:
Option
,Argument
-
parseCommandLine
protected void parseCommandLine(Object options) throws BaseCommand.UnloggedFailure
Parses the command line argument, injecting parsed values into fields.This method must be explicitly invoked to cause a parse.
- Parameters:
options
- object whose fields declare Option and Argument annotations to describe the parameters of the command. Usuallythis
.- Throws:
BaseCommand.UnloggedFailure
- if the command line arguments were invalid.- See Also:
Option
,Argument
-
usage
protected String usage()
-
newCmdLineParser
protected com.google.gerrit.util.cli.CmdLineParser newCmdLineParser(Object options)
Construct a new parser for this command's received command line.
-
startThread
protected void startThread(BaseCommand.CommandRunnable thunk, AccessPath accessPath)
Spawn a function into its own thread.Typically this should be invoked within
CommandLifecycle.start(ChannelSession, Environment)
, such as:startThread(new CommandRunnable() { public void run() throws Exception { runImp(); } }, accessPath);
If the function throws an exception, it is translated to a simple message for the client, a non-zero exit code, and the stack trace is logged.
- Parameters:
thunk
- the runnable to execute on the thread, performing the command's logic.accessPath
- the path used by the end user for running the SSH command
-
onExit
protected void onExit(int rc)
Terminate this command and return a result code to the remote client.Commands should invoke this at most once. Once invoked, the command may lose access to request based resources as any callbacks previously registered with
RequestCleanup
will fire.- Parameters:
rc
- exit code for the remote client.
-
toPrintWriter
protected static PrintWriter toPrintWriter(OutputStream o)
Wrap the supplied output stream in a UTF-8 encoded PrintWriter.
-
die
protected BaseCommand.UnloggedFailure die(String msg)
-
die
protected BaseCommand.UnloggedFailure die(String msg, Throwable why)
-
die
protected BaseCommand.UnloggedFailure die(Throwable why)
-
enableGracefulStop
protected void enableGracefulStop()
-
getTaskDescription
protected String getTaskDescription()
-
-