View Javadoc

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