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.sshd.server.Command;
23  import org.apache.sshd.server.Environment;
24  import org.apache.sshd.server.ExitCallback;
25  import org.codehaus.plexus.util.FileUtils;
26  import org.codehaus.plexus.util.cli.CommandLineUtils;
27  import org.codehaus.plexus.util.cli.Commandline;
28  
29  import java.io.File;
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  
34  /**
35   * @author Olivier Lamy
36   */
37  public class ShellCommand implements Command
38  {
39  protected static final int OK = 0;
40  
41          protected static final int WARNING = 1;
42  
43          protected static final int ERROR = 2;
44  
45          private InputStream in;
46  
47          private OutputStream out;
48  
49          private OutputStream err;
50  
51          private ExitCallback callback;
52  
53          private Thread thread;
54  
55          private String commandLine;
56  
57          public ShellCommand( String commandLine )
58          {
59              this.commandLine = commandLine;
60          }
61  
62          public void setInputStream( InputStream in )
63          {
64              this.in = in;
65          }
66  
67          public void setOutputStream( OutputStream out )
68          {
69              this.out = out;
70          }
71  
72          public void setErrorStream( OutputStream err )
73          {
74              this.err = err;
75          }
76  
77          public void setExitCallback( ExitCallback callback )
78          {
79              this.callback = callback;
80          }
81  
82          public void start( Environment env )
83              throws IOException
84          {
85              File tmpFile = File.createTempFile( "wagon", "test-sh" );
86              tmpFile.deleteOnExit();
87              int exitValue = 0;
88              CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
89              CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
90              try
91              {
92  
93                  // hackhish defaut commandline tools not support ; or && so write a file with the script
94                  // and "/bin/sh -e " + tmpFile.getPath();
95                  FileUtils.fileWrite( tmpFile, commandLine );
96  
97                  Commandline cl = new Commandline();
98                  cl.setExecutable( "/bin/sh" );
99                  //cl.createArg().setValue( "-e" );
100                 //cl.createArg().setValue( tmpFile.getPath() );
101                 cl.createArg().setFile( tmpFile );
102 
103                 exitValue = CommandLineUtils.executeCommandLine( cl, stdout, stderr );
104                 System.out.println( "exit value " + exitValue );
105                 /*
106                 if ( exitValue == 0 )
107                 {
108                     out.write( stdout.getOutput().getBytes() );
109                     out.write( '\n' );
110                     out.flush();
111 
112                 }
113                 else
114                 {
115                     out.write( stderr.getOutput().getBytes() );
116                     out.write( '\n' );
117                     out.flush();
118 
119                 }*/
120 
121             }
122             catch ( Exception e )
123             {
124                 exitValue = ERROR;
125                 e.printStackTrace();
126             }
127             finally
128             {
129                 deleteQuietly( tmpFile );
130                 if ( exitValue != 0 )
131                 {
132                     err.write( stderr.getOutput().getBytes() );
133                     err.write( '\n' );
134                     err.flush();
135                     callback.onExit( exitValue, stderr.getOutput() );
136                 }
137                 else
138                 {
139                     out.write( stdout.getOutput().getBytes() );
140                     out.write( '\n' );
141                     out.flush();
142                     callback.onExit( exitValue, stdout.getOutput() );
143                 }
144 
145             }
146             /*
147             out.write( exitValue );
148             out.write( '\n' );
149 
150             */
151             out.flush();
152         }
153 
154         public void destroy()
155         {
156 
157         }
158 
159         private void deleteQuietly( File f )
160         {
161 
162             try
163             {
164                 f.delete();
165             }
166             catch ( Exception e )
167             {
168                 // ignore
169             }
170         }
171 }