com.dtolabs.rundeck.core.utils
Class ThreadBoundOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by com.dtolabs.rundeck.core.utils.ThreadBoundOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class ThreadBoundOutputStream
extends java.io.FilterOutputStream

ThreadBoundOutputStream allows a different OutputStream to be used for the current Thread and any child threads when necessary, otherwise the default OutputStream is used. This is useful for replacing System.err or System.out to capture output in a multi-threaded system, allowing using different sink OutputStreams for each Thread (and their sub-threads).
Convenience methods bindSystemOut() and bindSystemErr() allow easy replacement of the System.out and System.err printstreams, and access to the ThreadBoundOutputStream at any time after doing so.
Setting the correct OutputStream should be done with installThreadStream(java.io.OutputStream), and removed with removeThreadStream().
Example code which replaces System.out and sets a two different FileOutputStreams as the sinks for System.out for multpile threads:

 public static void main(String[] args) throws FileNotFoundException {
     //bind to System.out, or retrieve already bound instance
     final ThreadBoundOutputStream newout = ThreadBoundOutputStream.bindSystemOut();

     //start another thread which redirect System.out to a file
     Thread t1 = new Thread(new Runnable(){
         public void run() {
             try {
                 newout.installThreadStream(new FileOutputStream("thread1.output"));
                 method1("thread1"); //writes to the thread1.output file
             } catch (FileNotFoundException e) { }
         }
     });
 
     t1.start();
     method1("main"); //prints to original System.out
 }
 public static void method1(String thread) {
     //pre-existing code which writes to System.out
     System.out.println("This output will be written to the correct outputstream in thread: " + thread);
 }
 


Field Summary
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
ThreadBoundOutputStream(java.io.OutputStream stream)
          Create a ThreadBoundOutputStream with a particular OutputStream as the default write destination.
 
Method Summary
static ThreadBoundOutputStream bindSystemErr()
          Bind the System.err PrintStream to a ThreadBoundOutputStream and return it.
static ThreadBoundOutputStream bindSystemOut()
          Bind the System.out PrintStream to a ThreadBoundOutputStream and return it.
 java.io.OutputStream getSink()
          Return the original OutputStream sink
 java.io.OutputStream getThreadStream()
           
 void installThreadStream(java.io.OutputStream stream)
          Install an outputstream for the current thread and any child threads
 java.io.OutputStream removeThreadStream()
          Remove the custom stream for the current thread.
static void unbindSystemErr()
          Resets the System.out printstream to the original PrintStream prior to the last call to bindSystemOut().
static void unbindSystemOut()
          Resets the System.out printstream to the original PrintStream prior to the last call to bindSystemOut().
 void write(int i)
           
 
Methods inherited from class java.io.FilterOutputStream
close, flush, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadBoundOutputStream

public ThreadBoundOutputStream(java.io.OutputStream stream)
Create a ThreadBoundOutputStream with a particular OutputStream as the default write destination.

Parameters:
stream - default sink
Method Detail

installThreadStream

public void installThreadStream(java.io.OutputStream stream)
Install an outputstream for the current thread and any child threads

Parameters:
stream - the new output stream

removeThreadStream

public java.io.OutputStream removeThreadStream()
Remove the custom stream for the current thread.

Returns:
thread bound OutputStream or null if none exists

getThreadStream

public java.io.OutputStream getThreadStream()

write

public void write(int i)
           throws java.io.IOException
Overrides:
write in class java.io.FilterOutputStream
Throws:
java.io.IOException

bindSystemOut

public static ThreadBoundOutputStream bindSystemOut()
Bind the System.out PrintStream to a ThreadBoundOutputStream and return it. If System.out is already bound, returns the pre-bound ThreadBoundOutputStream. The binding can be removed with unbindSystemOut().

Returns:
A ThreadBoundOutputStream bound to System.out

unbindSystemOut

public static void unbindSystemOut()
Resets the System.out printstream to the original PrintStream prior to the last call to bindSystemOut(). WARNING: you should only call unbindSystemOut if you know that no other threads are depending on the System.out to be bound.Use the removeThreadStream() method to remove any OutputStream bound to a particular thread with a ThreadBoundOutputStream.


bindSystemErr

public static ThreadBoundOutputStream bindSystemErr()
Bind the System.err PrintStream to a ThreadBoundOutputStream and return it. If System.err is already bound, returns the pre-bound ThreadBoundOutputStream. The binding can be removed with unbindSystemErr().

Returns:
A ThreadBoundOutputStream bound to System.err

unbindSystemErr

public static void unbindSystemErr()
Resets the System.out printstream to the original PrintStream prior to the last call to bindSystemOut(). WARNING: you should only call unbindSystemOut if you know that no other threads are depending on the System.out to be bound. Use the removeThreadStream() method to remove any OutputStream bound to a particular thread with a ThreadBoundOutputStream.


getSink

public java.io.OutputStream getSink()
Return the original OutputStream sink

Returns:
the OutputStream