Skip to content

Commit

Permalink
PAYARA-3492-Upgrade-JLine-version-to-v3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcaliskan committed Mar 14, 2019
1 parent f7fa26c commit d98c1b8
Show file tree
Hide file tree
Showing 7 changed files with 421 additions and 414 deletions.
2 changes: 1 addition & 1 deletion nucleus/admin/cli/pom.xml
Expand Up @@ -166,7 +166,7 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>jline</groupId>
<groupId>org.jline</groupId>
<artifactId>jline</artifactId>
</dependency>
</dependencies>
Expand Down
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2018] Payara Foundation and/or affiliates
// Portions Copyright [2018-2019] Payara Foundation and/or affiliates

package com.sun.enterprise.admin.cli;

Expand All @@ -46,7 +46,9 @@
import java.lang.annotation.Annotation;
import java.util.logging.*;

import jline.console.ConsoleReader;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.terminal.TerminalBuilder;
import org.jvnet.hk2.annotations.Contract;
import org.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.config.InjectionManager;
Expand Down Expand Up @@ -912,10 +914,12 @@ protected void prevalidate() throws CommandException {
/*
* Check for missing options and operands.
*/
ConsoleReader cons = null;
LineReader reader = null;
if (programOpts.isInteractive()) {
try {
cons = new ConsoleReader(System.in, System.out, null);
reader = LineReaderBuilder.builder()
.terminal(TerminalBuilder.builder().streams(System.in, System.out).build())
.build();
} catch (IOException ioe) {
logger.log(Level.WARNING, "Error instantiating console", ioe);
}
Expand All @@ -935,15 +939,10 @@ protected void prevalidate() throws CommandException {
continue;
}
// if option isn't set, prompt for it (if interactive)
if (getOption(opt.getName()) == null && cons != null && !missingOption) {
cons.setPrompt(strings.get("optionPrompt", lc(opt.getName())));
try {
String val = cons.readLine();
if (ok(val)) {
options.set(opt.getName(), val);
}
} catch (IOException ioe) {
logger.log(Level.WARNING, "Error reading input", ioe);
if (getOption(opt.getName()) == null && reader != null && !missingOption) {
String val = reader.readLine(strings.get("optionPrompt", lc(opt.getName())));
if (ok(val)) {
options.set(opt.getName(), val);
}
}
// if it's still not set, that's an error
Expand All @@ -967,16 +966,11 @@ protected void prevalidate() throws CommandException {
operandMax = operandParam.getParam().multiple() ? Integer.MAX_VALUE : 1;
}

if (operands.size() < operandMin && cons != null) {
cons.setPrompt(strings.get("operandPrompt", operandParam.getName()));
try {
String val = cons.readLine();
if (ok(val)) {
operands = new ArrayList<String>();
operands.add(val);
}
} catch (IOException ioe) {
throw new CommandValidationException("Error reading input", ioe);
if (operands.size() < operandMin && reader != null) {
String val = reader.readLine(strings.get("operandPrompt", operandParam.getName()));
if (ok(val)) {
operands = new ArrayList<>();
operands.add(val);
}
}
if (operands.size() < operandMin) {
Expand Down Expand Up @@ -1209,15 +1203,26 @@ private String passwordName(ParamModel opt) {
protected char[] readPassword(String prompt) {
char[] pc = null;

try (ConsoleReader consoleReader = new ConsoleReader(System.in, System.out, null)) {
LineReader lineReader = null;
try {
lineReader = LineReaderBuilder.builder()
.terminal(TerminalBuilder.builder()
.streams(System.in, System.out).build()).build();
// Don't echo anything when reading
char echoCharacter = 0;
consoleReader.setEchoCharacter(echoCharacter);
lineReader.getTerminal().echo(false);

String line = consoleReader.readLine(prompt);
String line = lineReader.readLine(prompt);
pc = line.toCharArray();
} catch (IOException ioe) {
logger.log(Level.WARNING, "IOException reading password.", ioe);
} finally {
if (lineReader != null && lineReader.getTerminal() != null) {
try {
lineReader.getTerminal().close();
} catch (IOException ioe) {
logger.log(Level.WARNING, "IOException reading password.", ioe);
}
}
}

return pc;
Expand Down

0 comments on commit d98c1b8

Please sign in to comment.