From: James Moger Date: Tue, 18 Mar 2014 21:25:17 +0000 (-0400) Subject: Get the command context with an accessor instead of direct field access X-Git-Tag: v1.5.0~68^2~66 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8f6d5356f3cb7f8e800541fe6d46911bd46c0247;p=gitblit.git Get the command context with an accessor instead of direct field access --- diff --git a/src/main/java/com/gitblit/transport/ssh/commands/BaseCommand.java b/src/main/java/com/gitblit/transport/ssh/commands/BaseCommand.java index 7088fefa..7c71ffa7 100644 --- a/src/main/java/com/gitblit/transport/ssh/commands/BaseCommand.java +++ b/src/main/java/com/gitblit/transport/ssh/commands/BaseCommand.java @@ -48,9 +48,6 @@ public abstract class BaseCommand implements Command, SessionAware { private static final Logger log = LoggerFactory.getLogger(BaseCommand.class); - /** Ssh context */ - protected SshCommandContext ctx; - protected InputStream in; protected OutputStream out; @@ -61,6 +58,9 @@ public abstract class BaseCommand implements Command, SessionAware { protected ServerSession session; + /** Ssh command context */ + private SshCommandContext ctx; + /** Text of the command line which lead up to invoking this instance. */ private String commandName = ""; @@ -96,10 +96,22 @@ public abstract class BaseCommand implements Command, SessionAware { @Override public abstract void start(Environment env) throws IOException; + protected void provideStateTo(final BaseCommand cmd) { + cmd.setContext(ctx); + cmd.setInputStream(in); + cmd.setOutputStream(out); + cmd.setErrorStream(err); + cmd.setExitCallback(exit); + } + public void setContext(SshCommandContext ctx) { this.ctx = ctx; } + public SshCommandContext getContext() { + return ctx; + } + @Override public void setInputStream(final InputStream in) { this.in = in; 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 f7c78d28..779f0b00 100644 --- a/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java +++ b/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java @@ -239,14 +239,4 @@ public abstract class DispatchCommand extends BaseCommand { usage.append("\n"); return usage.toString(); } - - protected void provideStateTo(final BaseCommand cmd) { - if (cmd instanceof BaseCommand) { - cmd.setContext(ctx); - } - cmd.setInputStream(in); - cmd.setOutputStream(out); - cmd.setErrorStream(err); - cmd.setExitCallback(exit); - } } diff --git a/src/main/java/com/gitblit/transport/ssh/git/BaseGitCommand.java b/src/main/java/com/gitblit/transport/ssh/git/BaseGitCommand.java index b203d476..2e4fda5f 100644 --- a/src/main/java/com/gitblit/transport/ssh/git/BaseGitCommand.java +++ b/src/main/java/com/gitblit/transport/ssh/git/BaseGitCommand.java @@ -84,7 +84,7 @@ abstract class BaseGitCommand extends BaseCommand { } repository = repository.substring(1); try { - return repositoryResolver.open(ctx.getClient(), repository); + return repositoryResolver.open(getContext().getClient(), repository); } catch (Exception e) { throw new Failure(1, "fatal: '" + repository + "': not a git archive", e); } diff --git a/src/main/java/com/gitblit/transport/ssh/git/Receive.java b/src/main/java/com/gitblit/transport/ssh/git/Receive.java index 9597eb4d..36bf2719 100644 --- a/src/main/java/com/gitblit/transport/ssh/git/Receive.java +++ b/src/main/java/com/gitblit/transport/ssh/git/Receive.java @@ -24,7 +24,7 @@ public class Receive extends BaseGitCommand { @Override protected void runImpl() throws Failure { try { - ReceivePack rp = receivePackFactory.create(ctx.getClient(), repo); + ReceivePack rp = receivePackFactory.create(getContext().getClient(), repo); rp.receive(in, out, null); } catch (Exception e) { throw new Failure(1, "fatal: Cannot receive pack: ", e); diff --git a/src/main/java/com/gitblit/transport/ssh/git/Upload.java b/src/main/java/com/gitblit/transport/ssh/git/Upload.java index 5de6b4d2..1a85368c 100644 --- a/src/main/java/com/gitblit/transport/ssh/git/Upload.java +++ b/src/main/java/com/gitblit/transport/ssh/git/Upload.java @@ -24,7 +24,7 @@ public class Upload extends BaseGitCommand { @Override protected void runImpl() throws Failure { try { - UploadPack up = uploadPackFactory.create(ctx.getClient(), repo); + UploadPack up = uploadPackFactory.create(getContext().getClient(), repo); up.upload(in, out, null); } catch (Exception e) { throw new Failure(1, "fatal: Cannot upload pack: ", e); diff --git a/src/main/java/com/gitblit/transport/ssh/gitblit/AddKeyCommand.java b/src/main/java/com/gitblit/transport/ssh/gitblit/AddKeyCommand.java index ae24dfb3..6980834b 100644 --- a/src/main/java/com/gitblit/transport/ssh/gitblit/AddKeyCommand.java +++ b/src/main/java/com/gitblit/transport/ssh/gitblit/AddKeyCommand.java @@ -41,7 +41,7 @@ public class AddKeyCommand extends BaseKeyCommand { @Override public void run() throws IOException, UnloggedFailure { - String username = ctx.getClient().getUsername(); + String username = getContext().getClient().getUsername(); List keys = readKeys(addKeys); for (String key : keys) { getKeyManager().addKey(username, key); diff --git a/src/main/java/com/gitblit/transport/ssh/gitblit/BaseKeyCommand.java b/src/main/java/com/gitblit/transport/ssh/gitblit/BaseKeyCommand.java index 1b7bac11..23e1dfcc 100644 --- a/src/main/java/com/gitblit/transport/ssh/gitblit/BaseKeyCommand.java +++ b/src/main/java/com/gitblit/transport/ssh/gitblit/BaseKeyCommand.java @@ -53,6 +53,6 @@ abstract class BaseKeyCommand extends SshCommand { } protected IPublicKeyManager getKeyManager() { - return ctx.getGitblit().getPublicKeyManager(); + return getContext().getGitblit().getPublicKeyManager(); } } diff --git a/src/main/java/com/gitblit/transport/ssh/gitblit/LsCommand.java b/src/main/java/com/gitblit/transport/ssh/gitblit/LsCommand.java index 75cb2d83..1f0d06c6 100644 --- a/src/main/java/com/gitblit/transport/ssh/gitblit/LsCommand.java +++ b/src/main/java/com/gitblit/transport/ssh/gitblit/LsCommand.java @@ -49,8 +49,8 @@ public class LsCommand extends SshCommand { } protected void listProjects() { - IGitblit gitblit = ctx.getGitblit(); - UserModel user = ctx.getClient().getUser(); + IGitblit gitblit = getContext().getGitblit(); + UserModel user = getContext().getClient().getUser(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List projects = gitblit.getProjectModels(user, false); @@ -85,8 +85,8 @@ public class LsCommand extends SshCommand { } protected void listRepositories() { - IGitblit gitblit = ctx.getGitblit(); - UserModel user = ctx.getClient().getUser(); + IGitblit gitblit = getContext().getGitblit(); + UserModel user = getContext().getClient().getUser(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List repositories = gitblit.getRepositoryModels(user); diff --git a/src/main/java/com/gitblit/transport/ssh/gitblit/LsUsersCommand.java b/src/main/java/com/gitblit/transport/ssh/gitblit/LsUsersCommand.java index 752afaf6..e59e8b97 100644 --- a/src/main/java/com/gitblit/transport/ssh/gitblit/LsUsersCommand.java +++ b/src/main/java/com/gitblit/transport/ssh/gitblit/LsUsersCommand.java @@ -35,7 +35,7 @@ public class LsUsersCommand extends SshCommand { @Override public void run() { - IGitblit gitblit = ctx.getGitblit(); + IGitblit gitblit = getContext().getGitblit(); List users = gitblit.getAllUsers(); int displaynameLen = 0; int usernameLen = 0; diff --git a/src/main/java/com/gitblit/transport/ssh/gitblit/RemoveKeyCommand.java b/src/main/java/com/gitblit/transport/ssh/gitblit/RemoveKeyCommand.java index af307303..915dbc03 100644 --- a/src/main/java/com/gitblit/transport/ssh/gitblit/RemoveKeyCommand.java +++ b/src/main/java/com/gitblit/transport/ssh/gitblit/RemoveKeyCommand.java @@ -44,7 +44,7 @@ public class RemoveKeyCommand extends BaseKeyCommand { @Override public void run() throws IOException, UnloggedFailure { - String username = ctx.getClient().getUsername(); + String username = getContext().getClient().getUsername(); List keys = readKeys(removeKeys); if (keys.contains(ALL)) { getKeyManager().removeAllKeys(username);