Add SignalUtils to handle OS interrupts

Extract into a utility to be shared between Shell and RunMojo

Fixes gh-773
This commit is contained in:
Dave Syer 2014-05-02 19:54:28 +01:00
parent 888703cf26
commit 1b7d8d9ade
4 changed files with 55 additions and 17 deletions

View File

@ -35,11 +35,9 @@ import org.springframework.boot.cli.command.CommandFactory;
import org.springframework.boot.cli.command.CommandRunner;
import org.springframework.boot.cli.command.core.HelpCommand;
import org.springframework.boot.cli.command.core.VersionCommand;
import org.springframework.boot.loader.tools.SignalUtils;
import org.springframework.util.StringUtils;
import sun.misc.Signal;
import sun.misc.SignalHandler;
/**
* A shell for Spring Boot. Drops the user into an event loop (REPL) where command line
* completion and history are available without relying on OS shell features.
@ -48,7 +46,6 @@ import sun.misc.SignalHandler;
* @author Dave Syer
* @author Phillip Webb
*/
@SuppressWarnings("restriction")
public class Shell {
private static final Set<Class<?>> NON_FORKED_COMMANDS;
@ -58,8 +55,6 @@ public class Shell {
NON_FORKED_COMMANDS = Collections.unmodifiableSet(nonForked);
}
private static final Signal SIG_INT = new Signal("INT");
private final ShellCommandRunner commandRunner;
private final ConsoleReader consoleReader;
@ -123,9 +118,8 @@ public class Shell {
}
private void attachSignalHandler() {
Signal.handle(SIG_INT, new SignalHandler() {
@Override
public void handle(sun.misc.Signal signal) {
SignalUtils.attachSignalHandler(new Runnable() {
public void run() {
handleSigInt();
}
});

View File

@ -62,6 +62,12 @@ public class RunProcess {
if (!inheritedIO) {
redirectOutput(this.process);
}
SignalUtils.attachSignalHandler(new Runnable() {
@Override
public void run() {
handleSigInt();
}
});
try {
this.process.waitFor();
}

View File

@ -0,0 +1,39 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.loader.tools;
import sun.misc.Signal;
import sun.misc.SignalHandler;
/**
* @author Dave Syer
*/
@SuppressWarnings("restriction")
public class SignalUtils {
private static final Signal SIG_INT = new Signal("INT");
public static void attachSignalHandler(final Runnable runnable) {
Signal.handle(SIG_INT, new SignalHandler() {
@Override
public void handle(Signal signal) {
runnable.run();
}
});
}
}

View File

@ -48,8 +48,7 @@ import edu.emory.mathcs.backport.java.util.Arrays;
* @author Phillip Webb
* @author Stephane Nicoll
*/
@Mojo(name = "run", requiresProject = true, defaultPhase = LifecyclePhase.VALIDATE,
requiresDependencyResolution = ResolutionScope.TEST)
@Mojo(name = "run", requiresProject = true, defaultPhase = LifecyclePhase.VALIDATE, requiresDependencyResolution = ResolutionScope.TEST)
@Execute(phase = LifecyclePhase.TEST_COMPILE)
public class RunMojo extends AbstractMojo {
@ -64,10 +63,10 @@ public class RunMojo extends AbstractMojo {
/**
* Add maven resources to the classpath directly, this allows live in-place editing or
* resources. Since resources will be added directly, and via the target/classes folder
* they will appear twice if {@code ClassLoader.getResources()} is called. In practice,
* however, most applications call {@code ClassLoader.getResource()} which will always
* return the first resource.
* resources. Since resources will be added directly, and via the target/classes
* folder they will appear twice if {@code ClassLoader.getResources()} is called. In
* practice, however, most applications call {@code ClassLoader.getResource()} which
* will always return the first resource.
* @since 1.0
*/
@Parameter(property = "run.addResources", defaultValue = "true")
@ -104,8 +103,8 @@ public class RunMojo extends AbstractMojo {
private String mainClass;
/**
* Additional folders besides the classes directory that should be added to
* the classpath.
* Additional folders besides the classes directory that should be added to the
* classpath.
* @since 1.0
*/
@Parameter