Refactor SSH commands with SshCommand base class

The SshCommand base class extends BaseCommand and provides the common
pattern of calling startThread with a CommandRunnable that parses
argments and then invokes the real logic.

The new annotation @RequiresCapability can be declared on any concrete
implementation of SshCommand to name a capability the caller must have
before they can run the contained command. This is enforced inside of
the DispatchCommand, which is what handles creating and delegating to
any paticular command implementation.

@AdminCommand is replaced by the new @RequriesCapability annotation,
which is more explicitly declaring the administrate server dependency.

Most existing commands have been ported to this SshCommand base class,
cleaning up a lot of the code. A few special cases still exist such as
StreamEvents or ScpCommand that are not trivial to port, as their start
implementation is well outside of the common pattern.

Plugin authors should be encouraged to extend from SshCommand for the
foreseeable future. I eventually would like to get away from needing
to extend this class, and instead use a simpler interface declaration,
but that is a much bigger change to make with how DispatchCommand and
BaseCommand are connected together.

Change-Id: I4f1de60c6fdeb207197dfccc135b4d532443d5b2
This commit is contained in:
Shawn O. Pearce
2012-05-07 22:42:42 -07:00
parent 68e49477c4
commit e3f6a0444b
23 changed files with 341 additions and 547 deletions

View File

@@ -22,20 +22,16 @@ import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.VisibleGroups;
import com.google.gerrit.server.project.ProjectControl;
import com.google.gerrit.sshd.BaseCommand;
import com.google.gerrit.sshd.SshCommand;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import org.apache.sshd.server.Environment;
import org.kohsuke.args4j.Option;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class ListGroupsCommand extends BaseCommand {
public class ListGroupsCommand extends SshCommand {
@Inject
private VisibleGroups.Factory visibleGroupsFactory;
@@ -57,18 +53,7 @@ public class ListGroupsCommand extends BaseCommand {
private Account.Id user;
@Override
public void start(final Environment env) throws IOException {
startThread(new CommandRunnable() {
@Override
public void run() throws Exception {
parseCommandLine();
ListGroupsCommand.this.display();
}
});
}
private void display() throws Failure {
final PrintWriter stdout = toPrintWriter(out);
protected void run() throws Failure {
try {
if (user != null && !projects.isEmpty()) {
throw new UnloggedFailure(1, "fatal: --user and --project options are not compatible.");
@@ -92,8 +77,6 @@ public class ListGroupsCommand extends BaseCommand {
throw die(e);
} catch (NoSuchGroupException e) {
throw die(e);
} finally {
stdout.flush();
}
}
}