James Moger
2014-03-18 23c416f30f4a1e69e76b70d71f6a9a7da4a020f1
src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
@@ -31,9 +31,6 @@
import org.slf4j.LoggerFactory;
import com.gitblit.models.UserModel;
import com.gitblit.transport.ssh.CommandMetaData;
import com.gitblit.transport.ssh.CachingPublicKeyAuthenticator;
import com.gitblit.transport.ssh.gitblit.BaseKeyCommand;
import com.gitblit.utils.StringUtils;
import com.gitblit.utils.cli.SubcommandHandler;
import com.google.common.base.Charsets;
@@ -50,21 +47,35 @@
   @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(UserModel user, Class<? extends DispatchCommand> cmd) {
   @Override
   public void destroy() {
      super.destroy();
      commands.clear();
      map = null;
      for (BaseCommand command : instantiated) {
         command.destroy();
      }
      for (DispatchCommand dispatcher : dispatchers.values()) {
         dispatcher.destroy();
      }
   }
   protected void registerDispatcher(UserModel user, Class<? extends DispatchCommand> cmd) {
      if (!cmd.isAnnotationPresent(CommandMetaData.class)) {
         throw new RuntimeException(MessageFormat.format("{0} must be annotated with {1}!", cmd.getName(),
               CommandMetaData.class.getName()));
      }
      if (dispatchers == null) {
         dispatchers = Maps.newHashMap();
      }
      CommandMetaData meta = cmd.getAnnotation(CommandMetaData.class);
@@ -82,8 +93,7 @@
      }
   }
   protected void registerCommands(UserModel user) {
   }
   protected abstract void registerCommands(UserModel user);
   /**
@@ -92,7 +102,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()));
@@ -112,10 +122,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;
@@ -167,6 +176,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));
      }
@@ -242,26 +252,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 BaseKeyCommand) {
         BaseKeyCommand k = (BaseKeyCommand) cmd;
         k.setAuthenticator(authenticator);
      }
   }
   private CachingPublicKeyAuthenticator authenticator;
   public void setAuthenticator(CachingPublicKeyAuthenticator authenticator) {
      this.authenticator = authenticator;
   }
}