KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > PumpStreamHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24
25 /**
26  * Copies standard output and error of subprocesses to standard output and
27  * error of the parent process.
28  *
29  * @since Ant 1.2
30  */

31 public class PumpStreamHandler implements ExecuteStreamHandler {
32
33     private Thread JavaDoc outputThread;
34     private Thread JavaDoc errorThread;
35     private StreamPumper inputPump;
36
37     private OutputStream JavaDoc out;
38     private OutputStream JavaDoc err;
39     private InputStream JavaDoc input;
40
41     /**
42      * Construct a new <code>PumpStreamHandler</code>.
43      * @param out the output <code>OutputStream</code>.
44      * @param err the error <code>OutputStream</code>.
45      * @param input the input <code>InputStream</code>.
46      */

47     public PumpStreamHandler(OutputStream JavaDoc out, OutputStream JavaDoc err,
48                              InputStream JavaDoc input) {
49         this.out = out;
50         this.err = err;
51         this.input = input;
52     }
53
54     /**
55      * Construct a new <code>PumpStreamHandler</code>.
56      * @param out the output <code>OutputStream</code>.
57      * @param err the error <code>OutputStream</code>.
58      */

59     public PumpStreamHandler(OutputStream JavaDoc out, OutputStream JavaDoc err) {
60         this(out, err, null);
61     }
62
63     /**
64      * Construct a new <code>PumpStreamHandler</code>.
65      * @param outAndErr the output/error <code>OutputStream</code>.
66      */

67     public PumpStreamHandler(OutputStream JavaDoc outAndErr) {
68         this(outAndErr, outAndErr);
69     }
70
71     /**
72      * Construct a new <code>PumpStreamHandler</code>.
73      */

74     public PumpStreamHandler() {
75         this(System.out, System.err);
76     }
77
78     /**
79      * Set the <code>InputStream</code> from which to read the
80      * standard output of the process.
81      * @param is the <code>InputStream</code>.
82      */

83     public void setProcessOutputStream(InputStream JavaDoc is) {
84         createProcessOutputPump(is, out);
85     }
86
87     /**
88      * Set the <code>InputStream</code> from which to read the
89      * standard error of the process.
90      * @param is the <code>InputStream</code>.
91      */

92     public void setProcessErrorStream(InputStream JavaDoc is) {
93         if (err != null) {
94             createProcessErrorPump(is, err);
95         }
96     }
97
98     /**
99      * Set the <code>OutputStream</code> by means of which
100      * input can be sent to the process.
101      * @param os the <code>OutputStream</code>.
102      */

103     public void setProcessInputStream(OutputStream JavaDoc os) {
104         if (input != null) {
105             inputPump = createInputPump(input, os, true);
106         } else {
107             try {
108                 os.close();
109             } catch (IOException JavaDoc e) {
110                 //ignore
111
}
112         }
113     }
114
115     /**
116      * Start the <code>Thread</code>s.
117      */

118     public void start() {
119         outputThread.start();
120         errorThread.start();
121         if (inputPump != null) {
122             Thread JavaDoc inputThread = new Thread JavaDoc(inputPump);
123             inputThread.setDaemon(true);
124             inputThread.start();
125         }
126     }
127
128     /**
129      * Stop pumping the streams.
130      */

131     public void stop() {
132         try {
133             outputThread.join();
134         } catch (InterruptedException JavaDoc e) {
135             // ignore
136
}
137         try {
138             errorThread.join();
139         } catch (InterruptedException JavaDoc e) {
140             // ignore
141
}
142
143         if (inputPump != null) {
144             inputPump.stop();
145         }
146
147         try {
148             err.flush();
149         } catch (IOException JavaDoc e) {
150             // ignore
151
}
152         try {
153             out.flush();
154         } catch (IOException JavaDoc e) {
155             // ignore
156
}
157     }
158
159     /**
160      * Get the error stream.
161      * @return <code>OutputStream</code>.
162      */

163     protected OutputStream JavaDoc getErr() {
164         return err;
165     }
166
167     /**
168      * Get the output stream.
169      * @return <code>OutputStream</code>.
170      */

171     protected OutputStream JavaDoc getOut() {
172         return out;
173     }
174
175     /**
176      * Create the pump to handle process output.
177      * @param is the <code>InputStream</code>.
178      * @param os the <code>OutputStream</code>.
179      */

180     protected void createProcessOutputPump(InputStream JavaDoc is, OutputStream JavaDoc os) {
181         outputThread = createPump(is, os);
182     }
183
184     /**
185      * Create the pump to handle error output.
186      * @param is the input stream to copy from.
187      * @param os the output stream to copy to.
188      */

189     protected void createProcessErrorPump(InputStream JavaDoc is, OutputStream JavaDoc os) {
190         errorThread = createPump(is, os);
191     }
192
193     /**
194      * Creates a stream pumper to copy the given input stream to the
195      * given output stream.
196      * @param is the input stream to copy from.
197      * @param os the output stream to copy to.
198      * @return a thread object that does the pumping.
199      */

200     protected Thread JavaDoc createPump(InputStream JavaDoc is, OutputStream JavaDoc os) {
201         return createPump(is, os, false);
202     }
203
204     /**
205      * Creates a stream pumper to copy the given input stream to the
206      * given output stream.
207      * @param is the input stream to copy from.
208      * @param os the output stream to copy to.
209      * @param closeWhenExhausted if true close the inputstream.
210      * @return a thread object that does the pumping.
211      */

212     protected Thread JavaDoc createPump(InputStream JavaDoc is, OutputStream JavaDoc os,
213                                 boolean closeWhenExhausted) {
214         final Thread JavaDoc result
215             = new Thread JavaDoc(new StreamPumper(is, os, closeWhenExhausted));
216         result.setDaemon(true);
217         return result;
218     }
219
220     /**
221      * Creates a stream pumper to copy the given input stream to the
222      * given output stream. Used for standard input.
223      * @since Ant 1.6.3
224      */

225     /*protected*/ StreamPumper createInputPump(InputStream JavaDoc is, OutputStream JavaDoc os,
226                                 boolean closeWhenExhausted) {
227         StreamPumper pumper = new StreamPumper(is, os, closeWhenExhausted);
228         pumper.setAutoflush(true);
229         return pumper;
230     }
231
232 }
233
Popular Tags