diff --git a/shell/src/main/java/org/apache/sqoop/shell/SqoopShell.java b/shell/src/main/java/org/apache/sqoop/shell/SqoopShell.java index 7e4a7df3..4aabfccd 100644 --- a/shell/src/main/java/org/apache/sqoop/shell/SqoopShell.java +++ b/shell/src/main/java/org/apache/sqoop/shell/SqoopShell.java @@ -22,6 +22,9 @@ import java.util.HashSet; import java.util.Iterator; +import jline.console.history.History; +import jline.console.history.PersistentHistory; + import org.apache.sqoop.shell.core.Constants; import org.apache.sqoop.shell.utils.ThrowableDisplayer; import org.codehaus.groovy.runtime.MethodClosure; @@ -56,8 +59,8 @@ public final class SqoopShell { static { commandsToKeep = new HashSet(); - commandsToKeep.add("exit"); - commandsToKeep.add("history"); + commandsToKeep.add(":exit"); + commandsToKeep.add(":history"); } /** @@ -68,11 +71,26 @@ public final class SqoopShell { */ public static void main (String[] args) throws Exception { System.setProperty("groovysh.prompt", Constants.SQOOP_PROMPT); - Groovysh shell = new Groovysh(); + final Groovysh shell = new Groovysh(); // Install our error hook (exception handling) shell.setErrorHook(new MethodClosure(ThrowableDisplayer.class, "errorHook")); + // Install shutdown hook + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + History history = shell.getRunner().getReader().getHistory(); + if (history instanceof PersistentHistory) { + try { + ((PersistentHistory)history).flush(); + } catch (IOException e) { + // Ignore this exception as it only affects history + } + } + } + }); + CommandRegistry registry = shell.getRegistry(); @SuppressWarnings("unchecked") Iterator iterator = registry.iterator();