From: James Moger Date: Tue, 18 Mar 2014 21:40:35 +0000 (-0400) Subject: DispatchCommand should be the ExtensionPoint X-Git-Tag: v1.5.0~68^2~64 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=991857ff2de203699c9592cccfb9e742d16d02cd;p=gitblit.git DispatchCommand should be the ExtensionPoint --- diff --git a/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java b/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java index 4783c053..00b79ffe 100644 --- a/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java +++ b/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java @@ -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 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 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 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 index 27d437f6..00000000 --- a/src/main/java/com/gitblit/transport/ssh/commands/PluginDispatchCommand.java +++ /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 - } -} diff --git a/src/main/java/com/gitblit/transport/ssh/commands/RootDispatcher.java b/src/main/java/com/gitblit/transport/ssh/commands/RootDispatcher.java index 38fbd2c8..749d10ea 100644 --- a/src/main/java/com/gitblit/transport/ssh/commands/RootDispatcher.java +++ b/src/main/java/com/gitblit/transport/ssh/commands/RootDispatcher.java @@ -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 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 exts = gitblit.getExtensions(DispatchCommand.class); + for (DispatchCommand ext : exts) { + registerDispatcher(user, ext); } } diff --git a/src/main/java/com/gitblit/transport/ssh/commands/SshCommand.java b/src/main/java/com/gitblit/transport/ssh/commands/SshCommand.java index 3d2c68b7..ee464e7c 100644 --- a/src/main/java/com/gitblit/transport/ssh/commands/SshCommand.java +++ b/src/main/java/com/gitblit/transport/ssh/commands/SshCommand.java @@ -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;