From f81ed74578eb1d0fed26c56ddcfbb39f08a4e47c Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Thu, 10 Apr 2014 18:58:10 -0400
Subject: [PATCH] Support querying in tickets ls and accept Object[][] for table data

---
 src/main/java/com/gitblit/transport/ssh/commands/BaseCommand.java |   75 +++++++++++++++++++++----------------
 1 files changed, 42 insertions(+), 33 deletions(-)

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 baa892c..6a190df 100644
--- a/src/main/java/com/gitblit/transport/ssh/commands/BaseCommand.java
+++ b/src/main/java/com/gitblit/transport/ssh/commands/BaseCommand.java
@@ -37,7 +37,6 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.gitblit.transport.ssh.SshCommandContext;
 import com.gitblit.utils.IdGenerator;
 import com.gitblit.utils.WorkQueue;
 import com.gitblit.utils.WorkQueue.CancelableRunnable;
@@ -49,8 +48,13 @@
 
 	private static final Logger log = LoggerFactory.getLogger(BaseCommand.class);
 
-	/** Ssh context */
-	protected SshCommandContext ctx;
+	private static final int PRIVATE_STATUS = 1 << 30;
+
+	public final static int STATUS_CANCEL = PRIVATE_STATUS | 1;
+
+	public final static int STATUS_NOT_FOUND = PRIVATE_STATUS | 2;
+
+	public final static int STATUS_NOT_ADMIN = PRIVATE_STATUS | 3;
 
 	protected InputStream in;
 
@@ -61,6 +65,9 @@
 	protected ExitCallback exit;
 
 	protected ServerSession session;
+
+	/** Ssh command context */
+	private SshCommandContext ctx;
 
 	/** Text of the command line which lead up to invoking this instance. */
 	private String commandName = "";
@@ -87,6 +94,9 @@
 
 	@Override
 	public void destroy() {
+		log.debug("destroying " + getClass().getName());
+		session = null;
+		ctx = null;
 	}
 
 	protected static PrintWriter toPrintWriter(final OutputStream o) {
@@ -96,8 +106,20 @@
 	@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
@@ -118,16 +140,6 @@
 	@Override
 	public void setExitCallback(final ExitCallback callback) {
 		this.exit = callback;
-	}
-
-	protected void provideBaseStateTo(final Command cmd) {
-		if (cmd instanceof BaseCommand) {
-			((BaseCommand) cmd).setContext(ctx);
-		}
-		cmd.setInputStream(in);
-		cmd.setOutputStream(out);
-		cmd.setErrorStream(err);
-		cmd.setExitCallback(exit);
 	}
 
 	protected String getName() {
@@ -200,8 +212,13 @@
 		return new CmdLineParser(options);
 	}
 
-	protected String usage() {
+	public String usage() {
 		return "";
+	}
+
+	protected void showHelp() throws UnloggedFailure {
+		argv = new String [] { "--help" };
+		parseCommandLine();
 	}
 
 	private final class TaskThunk implements CancelableRunnable {
@@ -220,7 +237,7 @@
 		public void cancel() {
 			synchronized (this) {
 				try {
-					// onExit(/*STATUS_CANCEL*/);
+					onExit(STATUS_CANCEL);
 				} finally {
 					ctx = null;
 				}
@@ -305,18 +322,12 @@
 	/**
 	 * Terminate this command and return a result code to the remote client.
 	 * <p>
-	 * Commands should invoke this at most once. Once invoked, the command may
-	 * lose access to request based resources as any callbacks previously
-	 * registered with {@link RequestCleanup} will fire.
+	 * Commands should invoke this at most once.
 	 *
-	 * @param rc
-	 *            exit code for the remote client.
+	 * @param rc exit code for the remote client.
 	 */
 	protected void onExit(final int rc) {
 		exit.onExit(rc);
-		// if (cleanup != null) {
-		// cleanup.run();
-		// }
 	}
 
 	private int handleError(final Throwable e) {
@@ -334,16 +345,14 @@
 		} else {
 			final StringBuilder m = new StringBuilder();
 			m.append("Internal server error");
-			// if (userProvider.get().isIdentifiedUser()) {
-			// final IdentifiedUser u = (IdentifiedUser) userProvider.get();
-			// m.append(" (user ");
-			// m.append(u.getAccount().getUserName());
-			// m.append(" account ");
-			// m.append(u.getAccountId());
-			// m.append(")");
-			// }
-			// m.append(" during ");
-			// m.append(contextProvider.get().getCommandLine());
+			String user = ctx.getClient().getUsername();
+			if (user != null) {
+				m.append(" (user ");
+				m.append(user);
+				m.append(")");
+			}
+			m.append(" during ");
+			m.append(ctx.getCommandLine());
 			log.error(m.toString(), e);
 		}
 

--
Gitblit v1.9.1