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

do not allow individual commands to close the top-level OutputStream (System.out) #2999

Merged
merged 2 commits into from
Jun 23, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import liquibase.configuration.*;
import liquibase.exception.CommandExecutionException;
import liquibase.exception.CommandValidationException;
import liquibase.io.UnclosableOutputStream;
import liquibase.util.StringUtil;

import java.io.OutputStream;
Expand Down Expand Up @@ -127,7 +128,12 @@ public <T> T getArgumentValue(CommandArgumentDefinition<T> argument) {
* Think "what would be piped out", not "what the user is told about what is happening".
*/
public CommandScope setOutput(OutputStream outputStream) {
this.outputStream = outputStream;
/*
This is an UncloseableOutputStream because we do not want individual command steps to inadvertently (or
intentionally) close the System.out OutputStream. Closing System.out renders it unusable for other command
steps which expect it to still be open.
*/
this.outputStream = new UnclosableOutputStream(outputStream);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package liquibase.io;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
* This class is a wrapper around OutputStreams, and makes them impossible for callers to close.
*/
public class UnclosableOutputStream extends FilterOutputStream {
public UnclosableOutputStream(OutputStream out) {
super(out);
}

/**
* This method does not actually close the underlying stream, but rather only flushes it. Callers should not be
* closing the stream they are given.
*/
@Override
public void close() throws IOException {
out.flush();
}
}