Coverage Report - org.apache.maven.wagon.providers.ssh.ShellCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
ShellCommand
0 %
0/45
0 %
0/2
1,375
 
 1  
 package org.apache.maven.wagon.providers.ssh;
 2  
 /*
 3  
  * Licensed to the Apache Software Foundation (ASF) under one
 4  
  * or more contributor license agreements.  See the NOTICE file
 5  
  * distributed with this work for additional information
 6  
  * regarding copyright ownership.  The ASF licenses this file
 7  
  * to you under the Apache License, Version 2.0 (the
 8  
  * "License"); you may not use this file except in compliance
 9  
  * with the License.  You may obtain a copy of the License at
 10  
  *
 11  
  *   http://www.apache.org/licenses/LICENSE-2.0
 12  
  *
 13  
  * Unless required by applicable law or agreed to in writing,
 14  
  * software distributed under the License is distributed on an
 15  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 16  
  * KIND, either express or implied.  See the License for the
 17  
  * specific language governing permissions and limitations
 18  
  * under the License.
 19  
  */
 20  
 
 21  
 import org.apache.sshd.server.Command;
 22  
 import org.apache.sshd.server.Environment;
 23  
 import org.apache.sshd.server.ExitCallback;
 24  
 import org.codehaus.plexus.util.FileUtils;
 25  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 26  
 import org.codehaus.plexus.util.cli.Commandline;
 27  
 
 28  
 import java.io.File;
 29  
 import java.io.IOException;
 30  
 import java.io.InputStream;
 31  
 import java.io.OutputStream;
 32  
 
 33  
 /**
 34  
  * @author Olivier Lamy
 35  
  */
 36  
 public class ShellCommand implements Command
 37  
 {
 38  
 protected static final int OK = 0;
 39  
 
 40  
         protected static final int WARNING = 1;
 41  
 
 42  
         protected static final int ERROR = 2;
 43  
 
 44  
         private InputStream in;
 45  
 
 46  
         private OutputStream out;
 47  
 
 48  
         private OutputStream err;
 49  
 
 50  
         private ExitCallback callback;
 51  
 
 52  
         private Thread thread;
 53  
 
 54  
         private String commandLine;
 55  
 
 56  
         public ShellCommand( String commandLine )
 57  0
         {
 58  0
             this.commandLine = commandLine;
 59  0
         }
 60  
 
 61  
         public void setInputStream( InputStream in )
 62  
         {
 63  0
             this.in = in;
 64  0
         }
 65  
 
 66  
         public void setOutputStream( OutputStream out )
 67  
         {
 68  0
             this.out = out;
 69  0
         }
 70  
 
 71  
         public void setErrorStream( OutputStream err )
 72  
         {
 73  0
             this.err = err;
 74  0
         }
 75  
 
 76  
         public void setExitCallback( ExitCallback callback )
 77  
         {
 78  0
             this.callback = callback;
 79  0
         }
 80  
 
 81  
         public void start( Environment env )
 82  
             throws IOException
 83  
         {
 84  0
             File tmpFile = File.createTempFile( "wagon", "test-sh" );
 85  0
             tmpFile.deleteOnExit();
 86  0
             int exitValue = 0;
 87  0
             CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
 88  0
             CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
 89  
             try
 90  
             {
 91  
 
 92  
                 // hackhish defaut commandline tools not support ; or && so write a file with the script
 93  
                 // and "/bin/sh -e " + tmpFile.getPath();
 94  0
                 FileUtils.fileWrite( tmpFile, commandLine );
 95  
 
 96  0
                 Commandline cl = new Commandline();
 97  0
                 cl.setExecutable( "/bin/sh" );
 98  
                 //cl.createArg().setValue( "-e" );
 99  
                 //cl.createArg().setValue( tmpFile.getPath() );
 100  0
                 cl.createArg().setFile( tmpFile );
 101  
 
 102  0
                 exitValue = CommandLineUtils.executeCommandLine( cl, stdout, stderr );
 103  0
                 System.out.println( "exit value " + exitValue );
 104  
                 /*
 105  
                 if ( exitValue == 0 )
 106  
                 {
 107  
                     out.write( stdout.getOutput().getBytes() );
 108  
                     out.write( '\n' );
 109  
                     out.flush();
 110  
 
 111  
                 }
 112  
                 else
 113  
                 {
 114  
                     out.write( stderr.getOutput().getBytes() );
 115  
                     out.write( '\n' );
 116  
                     out.flush();
 117  
 
 118  
                 }*/
 119  
 
 120  0
             }
 121  0
             catch ( Exception e )
 122  
             {
 123  0
                 exitValue = ERROR;
 124  0
                 e.printStackTrace();
 125  0
             }
 126  
             finally
 127  
             {
 128  0
                 deleteQuietly( tmpFile );
 129  0
                 if ( exitValue != 0 )
 130  
                 {
 131  0
                     err.write( stderr.getOutput().getBytes() );
 132  0
                     err.write( '\n' );
 133  0
                     err.flush();
 134  0
                     callback.onExit( exitValue, stderr.getOutput() );
 135  
                 }
 136  
                 else
 137  
                 {
 138  0
                     out.write( stdout.getOutput().getBytes() );
 139  0
                     out.write( '\n' );
 140  0
                     out.flush();
 141  0
                     callback.onExit( exitValue, stdout.getOutput() );
 142  
                 }
 143  
 
 144  0
             }
 145  
             /*
 146  
             out.write( exitValue );
 147  
             out.write( '\n' );
 148  
 
 149  
             */
 150  0
             out.flush();
 151  0
         }
 152  
 
 153  
         public void destroy()
 154  
         {
 155  
 
 156  0
         }
 157  
 
 158  
         private void deleteQuietly( File f )
 159  
         {
 160  
 
 161  
             try
 162  
             {
 163  0
                 f.delete();
 164  
             }
 165  0
             catch ( Exception e )
 166  
             {
 167  
                 // ignore
 168  0
             }
 169  0
         }
 170  
 }