James Moger
2014-03-14 503a853acad49ac6da7f520c26b3b27942dbfec5
src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
@@ -27,7 +27,12 @@
import org.apache.sshd.server.Environment;
import org.kohsuke.args4j.Argument;
import com.gitblit.git.GitblitReceivePackFactory;
import com.gitblit.git.GitblitUploadPackFactory;
import com.gitblit.git.RepositoryResolver;
import com.gitblit.transport.ssh.CommandMetaData;
import com.gitblit.transport.ssh.PublicKeyAuthenticator;
import com.gitblit.transport.ssh.SshDaemonClient;
import com.gitblit.utils.cli.SubcommandHandler;
import com.google.common.base.Charsets;
import com.google.common.base.Strings;
@@ -87,6 +92,12 @@
      }
      Command cmd = getCommand();
      if (cmd.getClass().isAnnotationPresent(CommandMetaData.class)) {
         CommandMetaData meta = cmd.getClass().getAnnotation(CommandMetaData.class);
         if (meta.admin() && !ctx.getClient().getUser().canAdmin()) {
            throw new UnloggedFailure(1, MessageFormat.format("{0} requires admin permissions", commandName));
         }
      }
      if (cmd instanceof BaseCommand) {
        BaseCommand bc = (BaseCommand) cmd;
        if (getName().isEmpty()) {
@@ -95,11 +106,11 @@
          bc.setName(getName() + " " + commandName);
        }
        bc.setArguments(args.toArray(new String[args.size()]));
      } else if (!args.isEmpty()) {
        throw new UnloggedFailure(1, commandName + " does not take arguments");
      }
      provideStateTo(cmd);
      provideBaseStateTo(cmd);
      provideGitState(cmd);
      reset();
      //atomicCmd.set(cmd);
      cmd.start(env);
@@ -136,7 +147,7 @@
  }
  @Override
protected String usage() {
  protected String usage() {
    final StringBuilder usage = new StringBuilder();
    usage.append("Available commands");
    if (!getName().isEmpty()) {
@@ -154,9 +165,15 @@
    String format = "%-" + maxLength + "s   %s";
    for (String name : Sets.newTreeSet(m.keySet())) {
      final Class<? extends Command> c = m.get(name);
      usage.append("   ");
      CommandMetaData meta = c.getAnnotation(CommandMetaData.class);
      if (meta != null) {
        if (meta.admin() && !ctx.getClient().getUser().canAdmin()) {
           continue;
        }
        if (meta.hidden()) {
           continue;
        }
        usage.append("   ");
        usage.append(String.format(format, name,
            Strings.nullToEmpty(meta.description())));
      }
@@ -173,4 +190,48 @@
    usage.append("\n");
    return usage.toString();
  }
  // This is needed because we are not using provider or
  // clazz.newInstance() for DispatchCommand
  private void reset() {
     args = new ArrayList<String>();
  }
  private void provideGitState(Command cmd) {
     if (cmd instanceof AbstractGitCommand) {
      AbstractGitCommand a = (AbstractGitCommand) cmd;
      a.setRepositoryResolver(repositoryResolver);
      a.setUploadPackFactory(gitblitUploadPackFactory);
      a.setReceivePackFactory(gitblitReceivePackFactory);
     } else if (cmd instanceof DispatchCommand) {
      DispatchCommand d = (DispatchCommand)cmd;
      d.setRepositoryResolver(repositoryResolver);
      d.setUploadPackFactory(gitblitUploadPackFactory);
      d.setReceivePackFactory(gitblitReceivePackFactory);
      d.setAuthenticator(authenticator);
     } else if (cmd instanceof BaseKeyCommand) {
        BaseKeyCommand k = (BaseKeyCommand)cmd;
        k.setAuthenticator(authenticator);
     }
  }
  private RepositoryResolver<SshDaemonClient> repositoryResolver;
  public void setRepositoryResolver(RepositoryResolver<SshDaemonClient> repositoryResolver) {
     this.repositoryResolver = repositoryResolver;
  }
  private GitblitUploadPackFactory<SshDaemonClient> gitblitUploadPackFactory;
  public void setUploadPackFactory(GitblitUploadPackFactory<SshDaemonClient> gitblitUploadPackFactory) {
     this.gitblitUploadPackFactory = gitblitUploadPackFactory;
  }
  private GitblitReceivePackFactory<SshDaemonClient> gitblitReceivePackFactory;
  public void setReceivePackFactory(GitblitReceivePackFactory<SshDaemonClient> gitblitReceivePackFactory) {
     this.gitblitReceivePackFactory = gitblitReceivePackFactory;
  }
  private PublicKeyAuthenticator authenticator;
  public void setAuthenticator(PublicKeyAuthenticator authenticator) {
   this.authenticator = authenticator;
  }
}