James Moger
2014-03-18 991857ff2de203699c9592cccfb9e742d16d02cd
DispatchCommand should be the ExtensionPoint
1 files deleted
3 files modified
59 ■■■■■ changed files
src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java 32 ●●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/commands/PluginDispatchCommand.java 11 ●●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/commands/RootDispatcher.java 12 ●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/commands/SshCommand.java 4 ●●● patch | view | raw | blame | history
src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
@@ -30,6 +30,8 @@
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.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 @@
    }
    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 @@
        }
    }
    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 @@
        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());
src/main/java/com/gitblit/transport/ssh/commands/PluginDispatchCommand.java
File was deleted
src/main/java/com/gitblit/transport/ssh/commands/RootDispatcher.java
@@ -17,8 +17,6 @@
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 @@
        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);
        }
    }
src/main/java/com/gitblit/transport/ssh/commands/SshCommand.java
@@ -19,9 +19,7 @@
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;