1 package org.apache.maven.wagon.providers.ssh;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
94
95 FileUtils.fileWrite( tmpFile, commandLine );
96
97 Commandline cl = new Commandline();
98 cl.setExecutable( "/bin/sh" );
99
100
101 cl.createArg().setFile( tmpFile );
102
103 exitValue = CommandLineUtils.executeCommandLine( cl, stdout, stderr );
104 System.out.println( "exit value " + exitValue );
105
106
107
108
109
110
111
112
113
114
115
116
117
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
148
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
169 }
170 }
171 }