]> source.dussan.org Git - gitblit.git/commitdiff
Fix command dispatching
authorDavid Ostrovsky <david@ostrovsky.org>
Wed, 26 Feb 2014 07:19:44 +0000 (08:19 +0100)
committerJames Moger <james.moger@gitblit.com>
Thu, 10 Apr 2014 22:58:07 +0000 (18:58 -0400)
DispatchCommand is supposed to be nested:

  ssh server gitblit version --verbose --format json

means that first the command that is seen by dispatching process is
"gitblit". Dispatch command look in its commands map for this command
and dispatch the rest of the command and options and arguments to this
command, version in this example.

Change-Id: I8ef8e0e369922c793ca7ad36c1a8f76b0206baa7

src/main/java/com/gitblit/transport/ssh/SshDaemon.java
src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java

index 42ee67abbc3a554c3976e63096e5772b6f295903..dd4a2d8e4c82ece1928a9047ab1549ee5c3f1d6d 100644 (file)
@@ -102,9 +102,12 @@ public class SshDaemon {
                sshd.setFileSystemFactory(new DisabledFilesystemFactory());
                sshd.setForwardingFilter(new NonForwardingFilter());
 
+               DispatchCommand gitblitCmd = new DispatchCommand();
+               gitblitCmd.registerCommand(CreateRepository.class);
+               gitblitCmd.registerCommand(VersionCommand.class);
+
                DispatchCommand dispatcher = new DispatchCommand();
-               dispatcher.registerCommand(CreateRepository.class);
-               dispatcher.registerCommand(VersionCommand.class);
+               dispatcher.registerDispatcher("gitblit", gitblitCmd);
 
                SshCommandFactory commandFactory = new SshCommandFactory(
                                new RepositoryResolver<SshSession>(gitblit),
index 0d614a0fbd284724556354dc875db05d1ab9bfcc..b6944eaf513f14cc05a633e26df50d2d66f688cb 100644 (file)
@@ -44,11 +44,19 @@ public class DispatchCommand extends BaseCommand {
 
   private Set<Class<? extends Command>> commands;
   private Map<String, Class<? extends Command>> map;
+  private Map<String, Command> root;
 
   public DispatchCommand() {
          commands = new HashSet<Class<? extends Command>>();
   }
 
+  public void registerDispatcher(String name, Command cmd) {
+         if (root == null) {
+                 root = Maps.newHashMap();
+         }
+         root.put(name, cmd);
+  }
+
   public void registerCommand(Class<? extends Command> cmd) {
          if (!cmd.isAnnotationPresent(CommandMetaData.class)) {
                  throw new RuntimeException(MessageFormat.format("{0} must be annotated with {1}!",
@@ -78,20 +86,7 @@ public class DispatchCommand extends BaseCommand {
         throw new UnloggedFailure(1, msg.toString());
       }
 
-      final Class<? extends Command> c = getMap().get(commandName);
-      if (c == null) {
-        String msg =
-            (getName().isEmpty() ? "Gitblit" : getName()) + ": "
-                + commandName + ": not found";
-        throw new UnloggedFailure(1, msg);
-      }
-
-      Command cmd = null;
-      try {
-         cmd = c.newInstance();
-      } catch (Exception e) {
-         throw new UnloggedFailure(1, MessageFormat.format("Failed to instantiate {0} command", commandName));
-      }
+      Command cmd = getCommand();
       if (cmd instanceof BaseCommand) {
         BaseCommand bc = (BaseCommand) cmd;
         if (getName().isEmpty()) {
@@ -119,6 +114,27 @@ public class DispatchCommand extends BaseCommand {
     }
   }
 
+  private Command getCommand() throws UnloggedFailure {
+       if (root != null && root.containsKey(commandName)) {
+               return root.get(commandName);
+       }
+       final Class<? extends Command> c = getMap().get(commandName);
+      if (c == null) {
+        String msg =
+            (getName().isEmpty() ? "Gitblit" : getName()) + ": "
+                + commandName + ": not found";
+        throw new UnloggedFailure(1, msg);
+      }
+
+      Command cmd = null;
+      try {
+         cmd = c.newInstance();
+      } catch (Exception e) {
+         throw new UnloggedFailure(1, MessageFormat.format("Failed to instantiate {0} command", commandName));
+      }
+       return cmd;
+  }
+
   @Override
 protected String usage() {
     final StringBuilder usage = new StringBuilder();