source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/PumpStreamHandler.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 6.3 KB
Line 
1/*
2 * Copyright 2000-2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.taskdefs;
19
20import java.io.IOException;
21import java.io.InputStream;
22import java.io.OutputStream;
23
24/**
25 * Copies standard output and error of subprocesses to standard output and
26 * error of the parent process.
27 *
28 * @since Ant 1.2
29 */
30public class PumpStreamHandler implements ExecuteStreamHandler {
31
32 private Thread outputThread;
33 private Thread errorThread;
34 private StreamPumper inputPump;
35
36 private OutputStream out;
37 private OutputStream err;
38 private InputStream input;
39
40 /**
41 * Construct a new <CODE>PumpStreamHandler</CODE>.
42 * @param out the output <CODE>OutputStream</CODE>.
43 * @param err the error <CODE>OutputStream</CODE>.
44 * @param input the input <CODE>InputStream</CODE>.
45 */
46 public PumpStreamHandler(OutputStream out, OutputStream err,
47 InputStream input) {
48 this.out = out;
49 this.err = err;
50 this.input = input;
51 }
52
53 /**
54 * Construct a new <CODE>PumpStreamHandler</CODE>.
55 * @param out the output <CODE>OutputStream</CODE>.
56 * @param err the error <CODE>OutputStream</CODE>.
57 */
58 public PumpStreamHandler(OutputStream out, OutputStream err) {
59 this(out, err, null);
60 }
61
62 /**
63 * Construct a new <CODE>PumpStreamHandler</CODE>.
64 * @param outAndErr the output/error <CODE>OutputStream</CODE>.
65 */
66 public PumpStreamHandler(OutputStream outAndErr) {
67 this(outAndErr, outAndErr);
68 }
69
70 /**
71 * Construct a new <CODE>PumpStreamHandler</CODE>.
72 */
73 public PumpStreamHandler() {
74 this(System.out, System.err);
75 }
76
77 /**
78 * Set the <CODE>InputStream</CODE> from which to read the
79 * standard output of the process.
80 * @param is the <CODE>InputStream</CODE>.
81 */
82 public void setProcessOutputStream(InputStream is) {
83 createProcessOutputPump(is, out);
84 }
85
86 /**
87 * Set the <CODE>InputStream</CODE> from which to read the
88 * standard error of the process.
89 * @param is the <CODE>InputStream</CODE>.
90 */
91 public void setProcessErrorStream(InputStream is) {
92 if (err != null) {
93 createProcessErrorPump(is, err);
94 }
95 }
96
97 /**
98 * Set the <CODE>OutputStream</CODE> by means of which
99 * input can be sent to the process.
100 * @param os the <CODE>OutputStream</CODE>.
101 */
102 public void setProcessInputStream(OutputStream os) {
103 if (input != null) {
104 inputPump = createInputPump(input, os, true);
105 } else {
106 try {
107 os.close();
108 } catch (IOException e) {
109 //ignore
110 }
111 }
112 }
113
114 /**
115 * Start the <CODE>Thread</CODE>s.
116 */
117 public void start() {
118 outputThread.start();
119 errorThread.start();
120 if (inputPump != null) {
121 Thread inputThread = new Thread(inputPump);
122 inputThread.setDaemon(true);
123 inputThread.start();
124 }
125 }
126
127 /**
128 * Stop pumping the streams.
129 */
130 public void stop() {
131 try {
132 outputThread.join();
133 } catch (InterruptedException e) {
134 // ignore
135 }
136 try {
137 errorThread.join();
138 } catch (InterruptedException e) {
139 // ignore
140 }
141
142 if (inputPump != null) {
143 inputPump.stop();
144 }
145
146 try {
147 err.flush();
148 } catch (IOException e) {
149 // ignore
150 }
151 try {
152 out.flush();
153 } catch (IOException e) {
154 // ignore
155 }
156 }
157
158 /**
159 * Get the error stream.
160 * @return <CODE>OutputStream</CODE>.
161 */
162 protected OutputStream getErr() {
163 return err;
164 }
165
166 /**
167 * Get the output stream.
168 * @return <CODE>OutputStream</CODE>.
169 */
170 protected OutputStream getOut() {
171 return out;
172 }
173
174 /**
175 * Create the pump to handle process output.
176 * @param is the <CODE>InputStream</CODE>.
177 * @param os the <CODE>OutputStream</CODE>.
178 */
179 protected void createProcessOutputPump(InputStream is, OutputStream os) {
180 outputThread = createPump(is, os);
181 }
182
183 /**
184 * Create the pump to handle error output.
185 * @param is the <CODE>InputStream</CODE>.
186 * @param os the <CODE>OutputStream</CODE>.
187 */
188 protected void createProcessErrorPump(InputStream is, OutputStream os) {
189 errorThread = createPump(is, os);
190 }
191
192 /**
193 * Creates a stream pumper to copy the given input stream to the
194 * given output stream.
195 */
196 protected Thread createPump(InputStream is, OutputStream os) {
197 return createPump(is, os, false);
198 }
199
200 /**
201 * Creates a stream pumper to copy the given input stream to the
202 * given output stream.
203 */
204 protected Thread createPump(InputStream is, OutputStream os,
205 boolean closeWhenExhausted) {
206 final Thread result
207 = new Thread(new StreamPumper(is, os, closeWhenExhausted));
208 result.setDaemon(true);
209 return result;
210 }
211
212 /**
213 * Creates a stream pumper to copy the given input stream to the
214 * given output stream. Used for standard input.
215 * @since Ant 1.6.3
216 */
217 /*protected*/ StreamPumper createInputPump(InputStream is, OutputStream os,
218 boolean closeWhenExhausted) {
219 StreamPumper pumper = new StreamPumper(is, os, closeWhenExhausted);
220 pumper.setAutoflush(true);
221 return pumper;
222 }
223
224}
Note: See TracBrowser for help on using the repository browser.