1 package org.codehaus.plexus.util.cli;
2
3 /*
4 * Copyright The Codehaus Foundation.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 /********************************************************************************
20 * CruiseControl, a Continuous Integration Toolkit
21 * Copyright (c) 2001-2003, ThoughtWorks, Inc.
22 * 651 W Washington Ave. Suite 500
23 * Chicago, IL 60661 USA
24 * All rights reserved.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 *
30 * + Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 *
33 * + Redistributions in binary form must reproduce the above
34 * copyright notice, this list of conditions and the following
35 * disclaimer in the documentation and/or other materials provided
36 * with the distribution.
37 *
38 * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
39 * names of its contributors may be used to endorse or promote
40 * products derived from this software without specific prior
41 * written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
47 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
48 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
49 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
50 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
51 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
52 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
53 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 ********************************************************************************/
55
56 /* ====================================================================
57 * Copyright 2003-2004 The Apache Software Foundation.
58 *
59 * Licensed under the Apache License, Version 2.0 (the "License");
60 * you may not use this file except in compliance with the License.
61 * You may obtain a copy of the License at
62 *
63 * http://www.apache.org/licenses/LICENSE-2.0
64 *
65 * Unless required by applicable law or agreed to in writing, software
66 * distributed under the License is distributed on an "AS IS" BASIS,
67 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
68 * See the License for the specific language governing permissions and
69 * limitations under the License.
70 * ====================================================================
71 */
72
73 import org.codehaus.plexus.util.IOUtil;
74
75 import java.io.BufferedReader;
76 import java.io.IOException;
77 import java.io.InputStream;
78 import java.io.InputStreamReader;
79 import java.io.PrintWriter;
80
81 /**
82 * Class to pump the error stream during Process's runtime. Copied from the Ant
83 * built-in task.
84 *
85 * @author <a href="mailto:fvancea@maxiq.com">Florin Vancea </a>
86 * @author <a href="mailto:pj@thoughtworks.com">Paul Julius </a>
87 * @version $Id$
88 * @since June 11, 2001
89 */
90 public class StreamPumper
91 extends AbstractStreamHandler
92 {
93 private final BufferedReader in;
94
95 private final StreamConsumer consumer;
96
97 private final PrintWriter out;
98
99 private volatile Exception exception = null;
100
101 private static final int SIZE = 1024;
102
103 public StreamPumper( InputStream in )
104 {
105 this( in, (StreamConsumer) null );
106 }
107
108 public StreamPumper( InputStream in, StreamConsumer consumer )
109 {
110 this( in, null, consumer );
111 }
112
113 public StreamPumper( InputStream in, PrintWriter writer )
114 {
115 this( in, writer, null );
116 }
117
118 public StreamPumper( InputStream in, PrintWriter writer, StreamConsumer consumer )
119 {
120 this.in = new BufferedReader( new InputStreamReader( in ), SIZE );
121 this.out = writer;
122 this.consumer = consumer;
123 }
124
125 public void run()
126 {
127 try
128 {
129 for ( String line = in.readLine(); line != null; line = in.readLine() )
130 {
131 try
132 {
133 if ( exception == null )
134 {
135 consumeLine( line );
136 }
137 }
138 catch ( Exception t )
139 {
140 exception = t;
141 }
142
143 if ( out != null )
144 {
145 out.println( line );
146
147 out.flush();
148 }
149 }
150 }
151 catch ( IOException e )
152 {
153 exception = e;
154 }
155 finally
156 {
157 IOUtil.close( in );
158
159 synchronized ( this )
160 {
161 setDone();
162
163 this.notifyAll();
164 }
165 }
166 }
167
168 public void flush()
169 {
170 if ( out != null )
171 {
172 out.flush();
173 }
174 }
175
176 public void close()
177 {
178 IOUtil.close( out );
179 }
180
181 public Exception getException()
182 {
183 return exception;
184 }
185
186 private void consumeLine( String line )
187 {
188 if ( consumer != null && !isDisabled() )
189 {
190 consumer.consumeLine( line );
191 }
192 }
193 }