001package org.apache.maven.wagon.providers.ssh;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.wagon.Streams;
023
024import java.io.BufferedReader;
025import java.io.IOException;
026
027/**
028 * CommandExecutorStreamProcessor
029 *
030 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
031 *
032 */
033public class CommandExecutorStreamProcessor
034{
035    private CommandExecutorStreamProcessor()
036    {
037        // shoo!
038    }
039
040    public static Streams processStreams( BufferedReader stderrReader, BufferedReader stdoutReader )
041        throws IOException
042    {
043        Streams streams = new Streams();
044        while ( true )
045        {
046            String line = stdoutReader.readLine();
047
048            if ( line == null )
049            {
050                break;
051            }
052
053            streams.setOut( streams.getOut() + line + "\n" );
054        }
055
056        // drain the output stream.
057        // TODO: we'll save this for the 1.0-alpha-8 line, so we can test it more. the -q arg in the
058        // unzip command should keep us until then...
059//            int avail = in.available();
060//            byte[] trashcan = new byte[1024];
061//
062//            while( ( avail = in.available() ) > 0 )
063//            {
064//                in.read( trashcan, 0, avail );
065//            }
066
067        // drain stderr next, if stream size is more than the allowed buffer size
068        // ( ie jsch has a hardcoded 32K size), the remote shell may be blocked. See WAGON-431
069        while ( true )
070        {
071            String line = stderrReader.readLine();
072
073            if ( line == null )
074            {
075                break;
076            }
077
078            // TODO: I think we need to deal with exit codes instead, but IIRC there are some cases of errors that
079            // don't have exit codes ignore this error. TODO: output a warning
080            if ( !line.startsWith( "Could not chdir to home directory" )
081                 && !line.endsWith( "ttyname: Operation not supported" ) )
082            {
083                streams.setErr( streams.getErr() + line + "\n" );
084            }
085        }
086
087        return streams;
088    }
089}