DIRKRB-693/DIRKRB-695 - Switching to use JLine for the Kadmin tool
diff --git a/kerby-dist/kdc-dist/LICENSE b/kerby-dist/kdc-dist/LICENSE
index aa7b845..a5f4f98 100644
--- a/kerby-dist/kdc-dist/LICENSE
+++ b/kerby-dist/kdc-dist/LICENSE
@@ -210,3 +210,8 @@
 
 See licenses/LICENSE-slf4j.txt
 
+===============================================================================
+This project bundles the JLine library (BSD license):
+
+See licenses/LICENSE.jline.txt
+
diff --git a/kerby-dist/kdc-dist/licenses/LICENSE.jline.txt b/kerby-dist/kdc-dist/licenses/LICENSE.jline.txt
new file mode 100644
index 0000000..4ebd976
--- /dev/null
+++ b/kerby-dist/kdc-dist/licenses/LICENSE.jline.txt
@@ -0,0 +1,35 @@
+Copyright (c) 2002-2017, the original author or authors.
+All rights reserved.
+
+http://www.opensource.org/licenses/bsd-license.php
+
+Redistribution and use in source and binary forms, with or
+without modification, are permitted provided that the following
+conditions are met:
+
+Redistributions of source code must retain the above copyright
+notice, this list of conditions and the following disclaimer.
+
+Redistributions in binary form must reproduce the above copyright
+notice, this list of conditions and the following disclaimer
+in the documentation and/or other materials provided with
+the distribution.
+
+Neither the name of JLine nor the names of its contributors
+may be used to endorse or promote products derived from this
+software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
+BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
+OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+OF THE POSSIBILITY OF SUCH DAMAGE.
+
diff --git a/kerby-tool/kdc-tool/pom.xml b/kerby-tool/kdc-tool/pom.xml
index c997982..69ab83d 100644
--- a/kerby-tool/kdc-tool/pom.xml
+++ b/kerby-tool/kdc-tool/pom.xml
@@ -56,6 +56,11 @@
         <artifactId>kerb-admin-server</artifactId>
         <version>${project.version}</version>
       </dependency>
+      <dependency>
+        <groupId>org.jline</groupId>
+        <artifactId>jline</artifactId>
+        <version>${jline.version}</version>
+      </dependency>
 
     </dependencies>
 
diff --git a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/KadminTool.java b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/KadminTool.java
index bc3b2e1..82f49b9 100644
--- a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/KadminTool.java
+++ b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/KadminTool.java
@@ -36,6 +36,14 @@
 import org.apache.kerby.kerberos.tool.kadmin.command.RenamePrincipalCommand;
 import org.apache.kerby.kerberos.tool.kadmin.command.AddPrincipalsCommand;
 import org.apache.kerby.util.OSUtil;
+import org.jline.reader.Completer;
+import org.jline.reader.EndOfFileException;
+import org.jline.reader.LineReader;
+import org.jline.reader.LineReaderBuilder;
+import org.jline.reader.UserInterruptException;
+import org.jline.reader.impl.completer.StringsCompleter;
+import org.jline.terminal.Terminal;
+import org.jline.terminal.TerminalBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -43,7 +51,6 @@
 import java.io.File;
 import java.io.IOException;
 import java.util.Map;
-import java.util.Scanner;
 
 /**
  * Ref. MIT kadmin command tool usage.
@@ -185,7 +192,7 @@
         return confDir;
     }
 
-    public static void main(String[] args) throws KrbException {
+    public static void main(String[] args) throws KrbException, IOException {
 
         if (args.length < 2) {
             System.err.println(USAGE);
@@ -249,16 +256,23 @@
             String query = kOptions.getStringOption(KadminOption.QUERY);
             execute(kadmin, query);
         } else {
-            System.out.print(PROMPT + ": ");
 
-            try (Scanner scanner = new Scanner(System.in, "UTF-8")) {
-                String input = scanner.nextLine();
+            Completer completer = new StringsCompleter("add_principal", "batch_anks", "ktadd", "ktremove",
+                                                       "delete_principal", "modify_principal", "rename_principal",
+                                                       "change_password", "list_principals", "get_principal");
 
-                while (!(input.equals("quit") || input.equals("exit")
-                        || input.equals("q"))) {
-                    execute(kadmin, input);
-                    System.out.print(PROMPT + ": ");
-                    input = scanner.nextLine();
+            Terminal terminal = TerminalBuilder.terminal();
+            LineReader lineReader = LineReaderBuilder.builder().completer(completer).terminal(terminal).build();
+
+            while (true) {
+                try {
+                    String line = lineReader.readLine(PROMPT + ": ");
+                    if ("quit".equals(line) || "exit".equals(line) || "q".equals(line)) {
+                        break;
+                    }
+                    execute(kadmin, line);
+                } catch (UserInterruptException | EndOfFileException ex) {
+                    break;
                 }
             }
         }
diff --git a/pom.xml b/pom.xml
index 89b8c79..9029972 100644
--- a/pom.xml
+++ b/pom.xml
@@ -59,6 +59,7 @@
     <findbugs.version>3.0.4</findbugs.version>
     <buildtools.dir>${basedir}/build-tools</buildtools.dir>
     <skipTests>false</skipTests>
+    <jline.version>3.6.0</jline.version>
     <maven-javadoc-plugin.version>2.10.4</maven-javadoc-plugin.version>
     <maven-jxr-plugin.version>2.5</maven-jxr-plugin.version>
     <mockito.version>2.7.22</mockito.version>