Refactor read and execute out of run
@@ -341,33 +341,11 @@ public class ConsoleSessionImpl implements Session {
String scriptFileName = System.getProperty(SHELL_INIT_SCRIPT);
executeScript(scriptFileName);
while (running) {
- String command = null;
- reading.set(true);
- try {
- command = reader.readLine(getPrompt(), getRPrompt(), null, null);
- } catch (EndOfFileException e) {
- break;
- } catch (UserInterruptException e) {
- // Ignore, loop again
- continue;
- } catch (Throwable t) {
- ShellUtil.logException(this, t);
- } finally {
- reading.set(false);
- }
+ String command = readCommand(reading);
if (command == null) {
break;
}
- try {
- Object result = session.execute(command);
- if (result != null) {
- session.getConsole().println(session.format(result, Converter.INSPECT));
- }
- } catch (InterruptedException e) {
- LOGGER.debug("Console session is closed");
- } catch (Throwable t) {
- ShellUtil.logException(this, t);
- }
+ execute(command);
}
close();
} finally {
@@ -400,6 +378,36 @@ public class ConsoleSessionImpl implements Session {
return isLocal != null && isLocal;
}
+ private String readCommand(AtomicBoolean reading) throws UserInterruptException {
+ String command = null;
+ reading.set(true);
+ try {
+ command = reader.readLine(getPrompt(), getRPrompt(), null, null);
+ } catch (EndOfFileException e) {
+ command = null;
+ } catch (UserInterruptException e) {
+ command = ""; // Do nothing
+ } catch (Throwable t) {
+ ShellUtil.logException(this, t);
+ } finally {
+ reading.set(false);
+ }
+ return command;
+ }
+
+ private void execute(String command) {
+ try {
+ Object result = session.execute(command);
+ if (result != null) {
+ session.getConsole().println(session.format(result, Converter.INSPECT));
+ }
+ } catch (InterruptedException e) {
+ LOGGER.debug("Console session is closed");
+ } catch (Throwable t) {
+ ShellUtil.logException(this, t);
+ }
+ }
+
private String getStatusLine(Job job, int width, String status) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < width - 1; i++) {
@@ -501,8 +509,6 @@ public class ConsoleSessionImpl implements Session {
}
}
-
-
protected void welcome(Properties brandingProps) {
String welcome = brandingProps.getProperty("welcome");
if (welcome != null && welcome.length() > 0) {
