]> source.dussan.org Git - gitblit.git/commitdiff
DispatchCommand should be the ExtensionPoint
authorJames Moger <james.moger@gitblit.com>
Tue, 18 Mar 2014 21:40:35 +0000 (17:40 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 10 Apr 2014 22:58:09 +0000 (18:58 -0400)
src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
src/main/java/com/gitblit/transport/ssh/commands/PluginDispatchCommand.java [deleted file]
src/main/java/com/gitblit/transport/ssh/commands/RootDispatcher.java
src/main/java/com/gitblit/transport/ssh/commands/SshCommand.java

index 4783c0537c6f7badc81dc6f6f6ef5d653e794ad1..00b79ffe05fee5288f0015d663b71c19b1e3fd41 100644 (file)
@@ -30,6 +30,8 @@ import org.kohsuke.args4j.Argument;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import ro.fortsoft.pf4j.ExtensionPoint;
+
 import com.gitblit.models.UserModel;
 import com.gitblit.utils.StringUtils;
 import com.gitblit.utils.cli.SubcommandHandler;
@@ -37,7 +39,7 @@ import com.google.common.base.Charsets;
 import com.google.common.base.Strings;
 import com.google.common.collect.Maps;
 
-public abstract class DispatchCommand extends BaseCommand {
+public abstract class DispatchCommand extends BaseCommand implements ExtensionPoint {
 
        private Logger log = LoggerFactory.getLogger(getClass());
 
@@ -73,19 +75,29 @@ public abstract class DispatchCommand extends BaseCommand {
        }
 
        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(),
+               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 = cmd.getAnnotation(CommandMetaData.class);
+               CommandMetaData meta = dispatcherClass.getAnnotation(CommandMetaData.class);
                if (meta.admin() && !user.canAdmin()) {
-                       log.debug(MessageFormat.format("excluding admin dispatch command {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 {
-                       DispatchCommand dispatcher = cmd.newInstance();
                        dispatcher.registerCommands(user);
                        dispatchers.put(meta.name(), dispatcher);
                } catch (Exception e) {
@@ -93,8 +105,8 @@ public abstract class DispatchCommand extends BaseCommand {
                }
        }
 
-       protected abstract void registerCommands(UserModel user);
 
+       protected abstract void registerCommands(UserModel user);
 
        /**
         * Registers a command as long as the user is permitted to execute it.
@@ -192,10 +204,8 @@ public abstract class DispatchCommand extends BaseCommand {
                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());
diff --git a/src/main/java/com/gitblit/transport/ssh/commands/PluginDispatchCommand.java b/src/main/java/com/gitblit/transport/ssh/commands/PluginDispatchCommand.java
deleted file mode 100644 (file)
index 27d437f..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-package com.gitblit.transport.ssh.commands;
-
-import com.gitblit.models.UserModel;
-
-public class PluginDispatchCommand extends DispatchCommand {
-
-       @Override
-       protected void registerCommands(UserModel user) {
-               // no op
-       }
-}
index 38fbd2c803343f732b1a79e961e6ae4b1353103b..749d10ea0e9bbea5fca2e79c9e8f63c1e6e312d9 100644 (file)
@@ -17,8 +17,6 @@ package com.gitblit.transport.ssh.commands;
 
 import java.util.List;
 
-import ro.fortsoft.pf4j.PluginWrapper;
-
 import com.gitblit.manager.IGitblit;
 import com.gitblit.models.UserModel;
 import com.gitblit.transport.ssh.SshDaemonClient;
@@ -40,13 +38,9 @@ public class RootDispatcher extends DispatchCommand {
                registerDispatcher(user, GitblitDispatcher.class);
                registerDispatcher(user, GitDispatcher.class);
 
-               List<SshCommand> exts = gitblit.getExtensions(SshCommand.class);
-               for (SshCommand sshCommand : exts) {
-                       PluginDispatchCommand pluginCmd = new PluginDispatchCommand();
-                       PluginWrapper wrapper = gitblit.whichPlugin(sshCommand.getClass());
-                       pluginCmd.registerCommand(user, sshCommand.getClass());
-                       // TODO(davido): add dispatcher registration per plugin name
-                       //registerDispatcher(wrapper.getDescriptor().getPluginId(), pluginCmd);
+               List<DispatchCommand> exts = gitblit.getExtensions(DispatchCommand.class);
+               for (DispatchCommand ext : exts) {
+                       registerDispatcher(user, ext);
                }
        }
 
index 3d2c68b793703cd0606e7238008311d52285c1fb..ee464e7c9c09dda3529a3a068643d5814f05492d 100644 (file)
@@ -19,9 +19,7 @@ import java.io.PrintWriter;
 
 import org.apache.sshd.server.Environment;
 
-import ro.fortsoft.pf4j.ExtensionPoint;
-
-public abstract class SshCommand extends BaseCommand implements ExtensionPoint {
+public abstract class SshCommand extends BaseCommand {
        protected PrintWriter stdout;
        protected PrintWriter stderr;