David Ostrovsky
2014-03-19 633d4c7a0cdf0b5eb2b01abfc6a058ac27e86d21
src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
@@ -30,21 +30,16 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.gitblit.git.GitblitReceivePackFactory;
import com.gitblit.git.GitblitUploadPackFactory;
import com.gitblit.git.RepositoryResolver;
import ro.fortsoft.pf4j.ExtensionPoint;
import com.gitblit.models.UserModel;
import com.gitblit.transport.ssh.CommandMetaData;
import com.gitblit.transport.ssh.CachingPublicKeyAuthenticator;
import com.gitblit.transport.ssh.SshDaemonClient;
mport com.gitblit.utils.StringUtils;
import com.gitblit.utils.StringUtils;
import com.gitblit.utils.cli.SubcommandHandler;
import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
public class DispatchCommand extends BaseCommand {
public abstract class DispatchCommand extends BaseCommand implements ExtensionPoint {
   private Logger log = LoggerFactory.getLogger(getClass());
@@ -54,21 +49,64 @@
   @Argument(index = 1, multiValued = true, metaVar = "ARG")
   private List<String> args = new ArrayList<String>();
   private Set<Class<? extends BaseCommand>> commands;
   private final Set<Class<? extends BaseCommand>> commands;
   private final Map<String, DispatchCommand> dispatchers;
   private final List<BaseCommand> instantiated;
   private Map<String, Class<? extends BaseCommand>> map;
   private Map<String, BaseCommand> dispatchers;
   public DispatchCommand() {
   protected DispatchCommand() {
      commands = new HashSet<Class<? extends BaseCommand>>();
      dispatchers = Maps.newHashMap();
      instantiated = new ArrayList<BaseCommand>();
   }
   public void registerDispatcher(String name, Command cmd) {
      if (dispatchers == null) {
         dispatchers = Maps.newHashMap();
   @Override
   public void destroy() {
      super.destroy();
      commands.clear();
      map = null;
      for (BaseCommand command : instantiated) {
         command.destroy();
      }
      dispatchers.put(name, cmd);
      for (DispatchCommand dispatcher : dispatchers.values()) {
         dispatcher.destroy();
      }
   }
   protected void registerDispatcher(UserModel user, Class<? extends DispatchCommand> cmd) {
      try {
         DispatchCommand dispatcher = cmd.newInstance();
         registerDispatcher(user, dispatcher);
      } catch (Exception e) {
         log.error("failed to instantiate {}", cmd.getName());
      }
   }
   protected void registerDispatcher(UserModel user, DispatchCommand dispatcher) {
      Class<? extends DispatchCommand> dispatcherClass = dispatcher.getClass();
      if (!dispatcherClass.isAnnotationPresent(CommandMetaData.class)) {
         throw new RuntimeException(MessageFormat.format("{0} must be annotated with {1}!", dispatcher.getName(),
               CommandMetaData.class.getName()));
      }
      CommandMetaData meta = dispatcherClass.getAnnotation(CommandMetaData.class);
      if (meta.admin() && !user.canAdmin()) {
         log.debug(MessageFormat.format("excluding admin dispatcher {0} for {1}", meta.name(), user.username));
         return;
      }
      log.debug("registering {} dispatcher", meta.name());
      try {
         dispatcher.registerCommands(user);
         dispatchers.put(meta.name(), dispatcher);
      } catch (Exception e) {
         log.error("failed to register {} dispatcher", meta.name());
      }
   }
   protected abstract void registerCommands(UserModel user);
   /**
    * Registers a command as long as the user is permitted to execute it.
@@ -76,7 +114,7 @@
    * @param user
    * @param cmd
    */
   public void registerCommand(UserModel user, Class<? extends BaseCommand> cmd) {
   protected void registerCommand(UserModel user, Class<? extends BaseCommand> cmd) {
      if (!cmd.isAnnotationPresent(CommandMetaData.class)) {
         throw new RuntimeException(MessageFormat.format("{0} must be annotated with {1}!", cmd.getName(),
               CommandMetaData.class.getName()));
@@ -96,10 +134,9 @@
            CommandMetaData meta = cmd.getAnnotation(CommandMetaData.class);
            map.put(meta.name(), cmd);
         }
         if (dispatchers != null) {
            for (Map.Entry<String, BaseCommand> entry : dispatchers.entrySet()) {
               map.put(entry.getKey(), entry.getValue().getClass());
            }
         for (Map.Entry<String, DispatchCommand> entry : dispatchers.entrySet()) {
            map.put(entry.getKey(), entry.getValue().getClass());
         }
      }
      return map;
@@ -151,6 +188,7 @@
      BaseCommand cmd = null;
      try {
         cmd = c.newInstance();
         instantiated.add(cmd);
      } catch (Exception e) {
         throw new UnloggedFailure(1, MessageFormat.format("Failed to instantiate {0} command", commandName));
      }
@@ -166,10 +204,8 @@
      for (String name : m.keySet()) {
         Class<? extends BaseCommand> c = m.get(name);
         CommandMetaData meta = c.getAnnotation(CommandMetaData.class);
         if (meta != null) {
            if (meta.hidden()) {
               continue;
            }
         if (meta.hidden()) {
            continue;
         }
         maxLength = Math.max(maxLength, name.length());
@@ -226,55 +262,5 @@
      usage.append("COMMAND --help' for more information.\n");
      usage.append("\n");
      return usage.toString();
   }
   protected void provideStateTo(final BaseCommand cmd) {
      if (cmd instanceof BaseCommand) {
         cmd.setContext(ctx);
      }
      cmd.setInputStream(in);
      cmd.setOutputStream(out);
      cmd.setErrorStream(err);
      cmd.setExitCallback(exit);
      if (cmd instanceof BaseGitCommand) {
         BaseGitCommand a = (BaseGitCommand) 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 CachingPublicKeyAuthenticator authenticator;
   public void setAuthenticator(CachingPublicKeyAuthenticator authenticator) {
      this.authenticator = authenticator;
   }
}