Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle JLine's UserInterruptException #108

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/org/springframework/shell/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public Bootstrap(String[] args, String[] contextPath) {
scanner.scan("org.springframework.shell.commands", "org.springframework.shell.converters",
"org.springframework.shell.plugin.support");
}
scanner.scan("org.springframework.shell.config");
// user contributed commands
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(ctx);
reader.loadBeanDefinitions(contextPath);
Expand Down Expand Up @@ -140,6 +141,8 @@ private void setupLogging() {

public ExitShellRequest run() {
StopWatch sw = new StopWatch("Spring Shell");
sw.start();

String[] commandsToExecuteAndThenQuit = commandLine.getShellCommandsToExecute();
// The shell is used
JLineShellComponent shell = ctx.getBean("shell", JLineShellComponent.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.springframework.shell.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
* @author nmeierling
*/
@Component
public class ApplicationContextProvider implements ApplicationContextAware {

private static ApplicationContext applicationContext;

public static ApplicationContext getApplicationContext() {
return applicationContext;
}

@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
applicationContext = ctx;
}
}
20 changes: 20 additions & 0 deletions src/main/java/org/springframework/shell/config/JLineConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.springframework.shell.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

/**
* @author nmeierling
*/
@Configuration
@PropertySource("classpath:application.properties")
public class JLineConfig {
@Autowired
Environment env;

public boolean isHandleUserInterrupt() {
return Boolean.parseBoolean(env.getProperty("jline.handleuserinterrupt"));
}
}
11 changes: 11 additions & 0 deletions src/main/java/org/springframework/shell/core/JLineShell.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import jline.WindowsTerminal;
import jline.console.ConsoleReader;
import jline.console.UserInterruptException;
import jline.console.history.History;
import jline.console.history.MemoryHistory;

Expand All @@ -49,6 +50,8 @@
import org.fusesource.jansi.Ansi.Color;
import org.fusesource.jansi.Ansi.Erase;
import org.fusesource.jansi.AnsiConsole;
import org.springframework.shell.config.ApplicationContextProvider;
import org.springframework.shell.config.JLineConfig;
import org.springframework.shell.event.ShellStatus;
import org.springframework.shell.event.ShellStatus.Status;
import org.springframework.shell.event.ShellStatusListener;
Expand Down Expand Up @@ -126,6 +129,11 @@ public void run() {
reader.setBellEnabled(false);
}

final JLineConfig config = ApplicationContextProvider.getApplicationContext().getBean(JLineConfig.class);
if (config.isHandleUserInterrupt()){
reader.setHandleUserInterrupt(true);
}

// reader.setDebug(new PrintWriter(new FileWriter("writer.debug", true)));

openFileLogIfPossible();
Expand Down Expand Up @@ -540,6 +548,9 @@ public void promptLoop() {
catch (IOException ioe) {
throw new IllegalStateException("Shell line reading failure", ioe);
}
catch (UserInterruptException e){ // only thrown if jline.handleuserinterrupt=true
promptLoop();
}
setShellStatus(Status.SHUTTING_DOWN);
}

Expand Down