View Javadoc

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          {
58              this.commandLine = commandLine;
59          }
60  
61          public void setInputStream( InputStream in )
62          {
63              this.in = in;
64          }
65  
66          public void setOutputStream( OutputStream out )
67          {
68              this.out = out;
69          }
70  
71          public void setErrorStream( OutputStream err )
72          {
73              this.err = err;
74          }
75  
76          public void setExitCallback( ExitCallback callback )
77          {
78              this.callback = callback;
79          }
80  
81          public void start( Environment env )
82              throws IOException
83          {
84              File tmpFile = File.createTempFile( "wagon", "test-sh" );
85              tmpFile.deleteOnExit();
86              int exitValue = 0;
87              CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
88              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                  FileUtils.fileWrite( tmpFile, commandLine );
95  
96                  Commandline cl = new Commandline();
97                  cl.setExecutable( "/bin/sh" );
98                  //cl.createArg().setValue( "-e" );
99                  //cl.createArg().setValue( tmpFile.getPath() );
100                 cl.createArg().setFile( tmpFile );
101 
102                 exitValue = CommandLineUtils.executeCommandLine( cl, stdout, stderr );
103                 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             }
121             catch ( Exception e )
122             {
123                 exitValue = ERROR;
124                 e.printStackTrace();
125             }
126             finally
127             {
128                 deleteQuietly( tmpFile );
129                 if ( exitValue != 0 )
130                 {
131                     err.write( stderr.getOutput().getBytes() );
132                     err.write( '\n' );
133                     err.flush();
134                     callback.onExit( exitValue, stderr.getOutput() );
135                 }
136                 else
137                 {
138                     out.write( stdout.getOutput().getBytes() );
139                     out.write( '\n' );
140                     out.flush();
141                     callback.onExit( exitValue, stdout.getOutput() );
142                 }
143 
144             }
145             /*
146             out.write( exitValue );
147             out.write( '\n' );
148 
149             */
150             out.flush();
151         }
152 
153         public void destroy()
154         {
155 
156         }
157 
158         private void deleteQuietly( File f )
159         {
160 
161             try
162             {
163                 f.delete();
164             }
165             catch ( Exception e )
166             {
167                 // ignore
168             }
169         }
170 }