Class DefaultConsoleOutputInterceptor
- java.lang.Object
-
- de.gebit.integrity.runner.console.intercept.DefaultConsoleOutputInterceptor
-
- All Implemented Interfaces:
ConsoleOutputInterceptor
public class DefaultConsoleOutputInterceptor extends java.lang.Object implements ConsoleOutputInterceptor
Default implementation of a console output interceptor. Hooks the System.out and System.err streams to do its work, thus it is critical that there's only one instance of this service in a given JVM. This imlementation automatically hooks the streams when at least one target is registered, and unhooks them when the last target unregisters.- Author:
- Rene Schneider - initial API and implementation
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
DefaultConsoleOutputInterceptor.InterceptPrintStream
This stream is the core of this service: it captures all data being printed through it, splits it into single lines and forwards the lines to all targets.
-
Field Summary
Fields Modifier and Type Field Description protected DefaultConsoleOutputInterceptor.InterceptPrintStream
interceptStderr
The interceptor stream for STDERR.protected DefaultConsoleOutputInterceptor.InterceptPrintStream
interceptStdout
The interceptor stream for STDOUT.protected java.io.PrintStream
stderr
The original STDERR stream.protected java.io.PrintStream
stdout
The original STDOUT stream.protected java.util.List<ConsoleInterceptorTarget>
targets
The interception targets.protected java.lang.Object
targetSync
A sync object used to synchronize interception target access.
-
Constructor Summary
Constructors Constructor Description DefaultConsoleOutputInterceptor()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
printlnStdErr(java.lang.String aLine)
Prints a line on the original System.err stream, including an added newline.void
printlnStdOut(java.lang.String aLine)
Prints a line on the original System.out stream, including an added newline.void
printStdErr(java.lang.String aText)
Prints text on the original System.err stream.void
printStdOut(java.lang.String aText)
Prints text on the original System.out stream.void
registerTarget(ConsoleInterceptorTarget aTarget)
Registers a target for intercepted data.void
unregisterTarget(ConsoleInterceptorTarget aTarget)
Unregisters a registered target.protected void
updateStreamCapture()
Reevaluates whether the streams are to be captured at the time of calling, and hooks or unhooks the streams as necessary.
-
-
-
Field Detail
-
targetSync
protected final java.lang.Object targetSync
A sync object used to synchronize interception target access.
-
targets
protected java.util.List<ConsoleInterceptorTarget> targets
The interception targets.
-
stdout
protected final java.io.PrintStream stdout
The original STDOUT stream.
-
interceptStdout
protected DefaultConsoleOutputInterceptor.InterceptPrintStream interceptStdout
The interceptor stream for STDOUT.
-
stderr
protected final java.io.PrintStream stderr
The original STDERR stream.
-
interceptStderr
protected DefaultConsoleOutputInterceptor.InterceptPrintStream interceptStderr
The interceptor stream for STDERR.
-
-
Method Detail
-
updateStreamCapture
protected void updateStreamCapture()
Reevaluates whether the streams are to be captured at the time of calling, and hooks or unhooks the streams as necessary.
-
registerTarget
public void registerTarget(ConsoleInterceptorTarget aTarget)
Description copied from interface:ConsoleOutputInterceptor
Registers a target for intercepted data.- Specified by:
registerTarget
in interfaceConsoleOutputInterceptor
- Parameters:
aTarget
- the target to register
-
unregisterTarget
public void unregisterTarget(ConsoleInterceptorTarget aTarget)
Description copied from interface:ConsoleOutputInterceptor
Unregisters a registered target.- Specified by:
unregisterTarget
in interfaceConsoleOutputInterceptor
- Parameters:
aTarget
- the target to unregister
-
printlnStdErr
public void printlnStdErr(java.lang.String aLine)
Description copied from interface:ConsoleOutputInterceptor
Prints a line on the original System.err stream, including an added newline. Automatically flushes the stream.- Specified by:
printlnStdErr
in interfaceConsoleOutputInterceptor
- Parameters:
aLine
- the line- See Also:
PrintWriter.println(String)
-
printStdErr
public void printStdErr(java.lang.String aText)
Description copied from interface:ConsoleOutputInterceptor
Prints text on the original System.err stream.- Specified by:
printStdErr
in interfaceConsoleOutputInterceptor
- Parameters:
aText
- the text to print- See Also:
PrintWriter.print(String)
-
printlnStdOut
public void printlnStdOut(java.lang.String aLine)
Description copied from interface:ConsoleOutputInterceptor
Prints a line on the original System.out stream, including an added newline. Automatically flushes the stream.- Specified by:
printlnStdOut
in interfaceConsoleOutputInterceptor
- Parameters:
aLine
- the line- See Also:
PrintWriter.println(String)
-
printStdOut
public void printStdOut(java.lang.String aText)
Description copied from interface:ConsoleOutputInterceptor
Prints text on the original System.out stream.- Specified by:
printStdOut
in interfaceConsoleOutputInterceptor
- Parameters:
aText
- the text to print- See Also:
PrintWriter.print(String)
-
-