]> source.dussan.org Git - gitblit.git/commitdiff
Get the command context with an accessor instead of direct field access
authorJames Moger <james.moger@gitblit.com>
Tue, 18 Mar 2014 21:25:17 +0000 (17:25 -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/BaseCommand.java
src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
src/main/java/com/gitblit/transport/ssh/git/BaseGitCommand.java
src/main/java/com/gitblit/transport/ssh/git/Receive.java
src/main/java/com/gitblit/transport/ssh/git/Upload.java
src/main/java/com/gitblit/transport/ssh/gitblit/AddKeyCommand.java
src/main/java/com/gitblit/transport/ssh/gitblit/BaseKeyCommand.java
src/main/java/com/gitblit/transport/ssh/gitblit/LsCommand.java
src/main/java/com/gitblit/transport/ssh/gitblit/LsUsersCommand.java
src/main/java/com/gitblit/transport/ssh/gitblit/RemoveKeyCommand.java

index 7088fefaa4c660c6d924ce7cbc5a399b404c923f..7c71ffa7d0e798303860abfa695b557dd7d8d22b 100644 (file)
@@ -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;
index f7c78d28ad3ea821a995d301234ac8a7f2cdb0fb..779f0b00405ca23841e3c3ac8aa95797c802564e 100644 (file)
@@ -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);
-       }
 }
index b203d476ad7c1a4301125ead7014e258d6874eda..2e4fda5f25bbb81ae5f58717e6210e9f8d1d174f 100644 (file)
@@ -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);
                }
index 9597eb4d599f31dcd5878576bbddf79e27a27c49..36bf27199f52fc701618b0f7cb8399af7bf452c2 100644 (file)
@@ -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);
index 5de6b4d28f5426ea3d6e66626b1c396103cf2a73..1a85368c9495f932f00e791a2c7cf0702f07429f 100644 (file)
@@ -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);
index ae24dfb3a301287c55f33779c8f6dd3b61b6794b..6980834bc0c1a8e590bcccfee8acb806f0a08148 100644 (file)
@@ -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<String> keys = readKeys(addKeys);
                for (String key : keys) {
                        getKeyManager().addKey(username, key);
index 1b7bac11f0c4f95465c6f251e7ad69cd59790a1e..23e1dfccae0ae0c67530a1682439d2a3cb6503f6 100644 (file)
@@ -53,6 +53,6 @@ abstract class BaseKeyCommand extends SshCommand {
        }
 
        protected IPublicKeyManager getKeyManager() {
-               return ctx.getGitblit().getPublicKeyManager();
+               return getContext().getGitblit().getPublicKeyManager();
        }
 }
index 75cb2d83e83477f7a8b4a9bd7a0070c694823c34..1f0d06c6d38b8e9c0ac11967d24c8bfc46723f64 100644 (file)
@@ -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<ProjectModel> 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<RepositoryModel> repositories = gitblit.getRepositoryModels(user);
index 752afaf626126236100877b72a56d96b2281ad51..e59e8b9730cee9543735daed4640450cf5b563a7 100644 (file)
@@ -35,7 +35,7 @@ public class LsUsersCommand extends SshCommand {
 
        @Override
        public void run() {
-               IGitblit gitblit = ctx.getGitblit();
+               IGitblit gitblit = getContext().getGitblit();
                List<UserModel> users = gitblit.getAllUsers();
                int displaynameLen = 0;
                int usernameLen = 0;
index af3073033b9d7ebf53357460ab7f5ae69a502eb1..915dbc030545f605cc3b6ea40330762d24bffbc2 100644 (file)
@@ -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<String> keys = readKeys(removeKeys);
                if (keys.contains(ALL)) {
                        getKeyManager().removeAllKeys(username);