View Javadoc
1   package org.apache.maven.wagon.providers.ssh;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.wagon.Streams;
23  
24  import java.io.BufferedReader;
25  import java.io.IOException;
26  
27  /**
28   * CommandExecutorStreamProcessor
29   *
30   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
31   *
32   */
33  public class CommandExecutorStreamProcessor
34  {
35      private CommandExecutorStreamProcessor()
36      {
37          // shoo!
38      }
39  
40      public static Streams processStreams( BufferedReader stderrReader, BufferedReader stdoutReader )
41          throws IOException
42      {
43          Streams streams = new Streams();
44          while ( true )
45          {
46              String line = stdoutReader.readLine();
47  
48              if ( line == null )
49              {
50                  break;
51              }
52  
53              streams.setOut( streams.getOut() + line + "\n" );
54          }
55  
56          // drain the output stream.
57          // TODO: we'll save this for the 1.0-alpha-8 line, so we can test it more. the -q arg in the
58          // unzip command should keep us until then...
59  //            int avail = in.available();
60  //            byte[] trashcan = new byte[1024];
61  //
62  //            while( ( avail = in.available() ) > 0 )
63  //            {
64  //                in.read( trashcan, 0, avail );
65  //            }
66  
67          // drain stderr next, if stream size is more than the allowed buffer size
68          // ( ie jsch has a hardcoded 32K size), the remote shell may be blocked. See WAGON-431
69          while ( true )
70          {
71              String line = stderrReader.readLine();
72  
73              if ( line == null )
74              {
75                  break;
76              }
77  
78              // TODO: I think we need to deal with exit codes instead, but IIRC there are some cases of errors that
79              // don't have exit codes ignore this error. TODO: output a warning
80              if ( !line.startsWith( "Could not chdir to home directory" )
81                   && !line.endsWith( "ttyname: Operation not supported" ) )
82              {
83                  streams.setErr( streams.getErr() + line + "\n" );
84              }
85          }
86  
87          return streams;
88      }
89  }