View Javadoc

1   package cz.cuni.amis.utils;
2   
3   import java.io.BufferedReader;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.io.InputStreamReader;
7   import java.util.logging.Level;
8   import java.util.logging.Logger;
9   
10  /**
11   * Reads content of the stream and discards it.
12   * <p><p>
13   * Optionally it may log the content of the stream to some logger (i.e., redirects the output of the stream to some log).
14   */
15  public class StreamSink extends Thread {
16  
17      protected InputStream os = null;
18  	protected Logger log;
19  	protected String logId;
20  	protected Level logLevel = Level.INFO;
21  
22  	/**
23  	 * Constructs the sink to silently discard all contents of 'os'.
24  	 * @param name name of the sink thread
25  	 * @param os stream to be sunk
26  	 */
27      public StreamSink(String name, InputStream os) {
28      	this(name, os, null);
29      }
30      
31      /**
32       * Constructs the sink to redirect all output from 'os' into 'log' (used log level is {@link Level#INFO}) as default. May
33       * be changed by {@link StreamSink#setLogLevel(Level)}.
34       * May 
35       * @param name name of the sink thread
36       * @param os
37       * @param log
38       */
39      public StreamSink(String name, InputStream os, Logger log) {
40      	this(name, os, log, null);
41      }
42      
43      /**
44       * Constructs the sink to redirect all output from 'os' into 'log' (used log level is {@link Level#INFO}) as default. May
45       * be changed by {@link StreamSink#setLogLevel(Level)}. Additionally all messages from the 'os' will be prefixed with 'logId+" "'.
46       * 
47       * @param name name of the sink thread
48       * @param os
49       * @param log
50       * @param logId
51       */
52      public StreamSink(String name, InputStream os, Logger log, String logId) {
53      	super(name);
54          this.log = log;
55          this.logId = logId;
56          this.os = os;
57      }
58      
59      public Level getLogLevel() {
60  		return logLevel;
61  	}
62  
63  	public StreamSink setLogLevel(Level logLevel) {
64  		NullCheck.check(logLevel, "logLevel");
65  		this.logLevel = logLevel;
66  		return this;
67  	}
68  	
69  	public Logger getLog() {
70  		return log;
71  	}
72  	
73  	protected StreamSink setLog(Logger log) {
74  		this.log = log;
75  		return this;
76  	}
77  
78  	public String getLogId() {
79  		return logId;
80  	}
81  
82  	protected StreamSink setLogId(String logId) {
83  		this.logId = logId;
84  		return this;
85  	}
86  	
87  	protected void handleInput(String str) {
88          if (log != null && log.isLoggable(Level.INFO)) {
89          	if (logId != null) {
90          		log.info(logId + " " + str);
91          	} else {
92          		log.info(str);
93          	}
94          }
95      }
96  
97      @Override
98      public void run() {
99          BufferedReader stdInput = new BufferedReader(new InputStreamReader(os));
100 
101         String s = null;
102         try {
103             while ((s = stdInput.readLine()) != null) {
104                 handleInput(s);
105             }
106             os.close();
107         } catch (IOException ex) {
108             // the process has been closed so reading the line has failed, 
109             // don't worry about it
110         }
111     }
112 }