Allow ExitCodeGenerator to be used on Exceptions

Update exit code support to allow the ExitCodeGenerator interface to
be placed on an Exception. Any uncaught exception implementing the
interface and returning a non `0` status will now trigger a System.exit
with the code.

Fixes gh-4803
This commit is contained in:
Phillip Webb
2016-01-13 11:56:24 +00:00
parent d2fed8bb07
commit 7397dbaf57
10 changed files with 186 additions and 40 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
@@ -53,11 +53,11 @@ public class WarCommandIT {
.start();
invocation = new Invocation(process);
invocation.await();
assertThat(invocation.getErrorOutput(), containsString("onStart error"));
assertThat(invocation.getStandardOutput(), containsString("Tomcat started"));
assertThat(invocation.getStandardOutput(),
assertThat(invocation.getOutput(), containsString("onStart error"));
assertThat(invocation.getOutput(), containsString("Tomcat started"));
assertThat(invocation.getOutput(),
containsString("/WEB-INF/lib-provided/tomcat-embed-core"));
assertThat(invocation.getStandardOutput(),
assertThat(invocation.getOutput(),
containsString("/WEB-INF/lib-provided/tomcat-embed-core"));
process.destroy();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
@@ -93,21 +93,27 @@ public final class CommandLineInvoker {
private final StringBuffer out = new StringBuffer();
private final StringBuffer combined = new StringBuffer();
private final Process process;
private final List<Thread> streamReaders = new ArrayList<Thread>();
public Invocation(Process process) {
this.process = process;
this.streamReaders.add(new Thread(
new StreamReadingRunnable(this.process.getErrorStream(), this.err)));
this.streamReaders.add(new Thread(
new StreamReadingRunnable(this.process.getInputStream(), this.out)));
this.streamReaders.add(new Thread(new StreamReadingRunnable(
this.process.getErrorStream(), this.err, this.combined)));
this.streamReaders.add(new Thread(new StreamReadingRunnable(
this.process.getInputStream(), this.out, this.combined)));
for (Thread streamReader : this.streamReaders) {
streamReader.start();
}
}
public String getOutput() {
return postProcessLines(getLines(this.combined));
}
public String getErrorOutput() {
return postProcessLines(getLines(this.err));
}
@@ -161,13 +167,13 @@ public final class CommandLineInvoker {
private final InputStream stream;
private final StringBuffer output;
private final StringBuffer[] outputs;
private final byte[] buffer = new byte[4096];
private StreamReadingRunnable(InputStream stream, StringBuffer buffer) {
private StreamReadingRunnable(InputStream stream, StringBuffer... outputs) {
this.stream = stream;
this.output = buffer;
this.outputs = outputs;
}
@Override
@@ -175,7 +181,9 @@ public final class CommandLineInvoker {
int read;
try {
while ((read = this.stream.read(this.buffer)) > 0) {
this.output.append(new String(this.buffer, 0, read));
for (StringBuffer output : this.outputs) {
output.append(new String(this.buffer, 0, read));
}
}
}
catch (IOException ex) {