James Moger
2014-03-27 8d96b960e472433d2b4a5b71df7000bf1fbde648
src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
@@ -84,16 +84,54 @@
      dispatchers.clear();
   }
   protected void registerDispatcher(UserModel user, Class<? extends DispatchCommand> cmd) {
   /**
    * Setup this dispatcher. Commands and nested dispatchers are normally
    * registered within this method.
    *
    * @param user
    */
   protected abstract void setup(UserModel user);
   /**
    * Register a command or a dispatcher by it's class.
    *
    * @param user
    * @param clazz
    */
   @SuppressWarnings("unchecked")
   protected final void register(UserModel user, Class<? extends BaseCommand> clazz) {
      if (DispatchCommand.class.isAssignableFrom(clazz)) {
         registerDispatcher(user, (Class<? extends DispatchCommand>) clazz);
         return;
      }
      registerCommand(user, clazz);
   }
   /**
    * Register a command or a dispatcher instance.
    *
    * @param user
    * @param cmd
    */
   protected final void register(UserModel user, BaseCommand cmd) {
      if (cmd instanceof DispatchCommand) {
         registerDispatcher(user, (DispatchCommand) cmd);
         return;
      }
      registerCommand(user, cmd);
   }
   private void registerDispatcher(UserModel user, Class<? extends DispatchCommand> clazz) {
      try {
         DispatchCommand dispatcher = cmd.newInstance();
         DispatchCommand dispatcher = clazz.newInstance();
         registerDispatcher(user, dispatcher);
      } catch (Exception e) {
         log.error("failed to instantiate {}", cmd.getName());
         log.error("failed to instantiate {}", clazz.getName());
      }
   }
   protected void registerDispatcher(UserModel user, DispatchCommand dispatcher) {
   private 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(),
@@ -102,19 +140,20 @@
      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));
         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);
         dispatcher.setup(user);
         if (dispatcher.commands.isEmpty() && dispatcher.dispatchers.isEmpty()) {
            // exclude because there are no commands available to the user
            log.debug(MessageFormat.format("excluding dispatcher {0} for {1}", meta.name(), user.username));
            log.debug(MessageFormat.format("excluding empty dispatcher {0} for {1}",
                  meta.name(), user.username));
            return;
         }
         log.debug("registering {} dispatcher", meta.name());
         dispatchers.put(meta.name(), dispatcher);
         for (String alias : meta.aliases()) {
            aliasToCommand.put(alias, meta.name());
@@ -128,26 +167,23 @@
      }
   }
   protected abstract void registerCommands(UserModel user);
   /**
    * Registers a command as long as the user is permitted to execute it.
    *
    * @param user
    * @param cmd
    * @param clazz
    */
   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(),
   private void registerCommand(UserModel user, Class<? extends BaseCommand> clazz) {
      if (!clazz.isAnnotationPresent(CommandMetaData.class)) {
         throw new RuntimeException(MessageFormat.format("{0} must be annotated with {1}!", clazz.getName(),
               CommandMetaData.class.getName()));
      }
      CommandMetaData meta = cmd.getAnnotation(CommandMetaData.class);
      CommandMetaData meta = clazz.getAnnotation(CommandMetaData.class);
      if (meta.admin() && !user.canAdmin()) {
         log.debug(MessageFormat.format("excluding admin command {0} for {1}", meta.name(), user.username));
         return;
      }
      commands.add(cmd);
      commands.add(clazz);
   }
   /**
@@ -156,7 +192,7 @@
    * @param user
    * @param cmd
    */
   protected void registerCommand(UserModel user, BaseCommand cmd) {
   private void registerCommand(UserModel user, BaseCommand cmd) {
      if (!cmd.getClass().isAnnotationPresent(CommandMetaData.class)) {
         throw new RuntimeException(MessageFormat.format("{0} must be annotated with {1}!", cmd.getName(),
               CommandMetaData.class.getName()));
@@ -194,7 +230,6 @@
         }
         for (Map.Entry<String, DispatchCommand> entry : dispatchers.entrySet()) {
            DispatchCommand dispatcher = entry.getValue();
            map.put(entry.getKey(), entry.getValue().getClass());
         }
      }
@@ -297,9 +332,9 @@
            continue;
         }
         String displayName = name;
         String displayName = name  + (meta.admin() ? "*" : "");
         if (commandToAliases.containsKey(meta.name())) {
            displayName = name + " (" + Joiner.on(',').join(commandToAliases.get(meta.name())) + ")";
            displayName = name  + (meta.admin() ? "*" : "")+ " (" + Joiner.on(',').join(commandToAliases.get(meta.name())) + ")";
         }
         displayNames.put(name, displayName);