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 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
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
93
94 FileUtils.fileWrite( tmpFile, commandLine );
95
96 Commandline cl = new Commandline();
97 cl.setExecutable( "/bin/sh" );
98
99
100 cl.createArg().setFile( tmpFile );
101
102 exitValue = CommandLineUtils.executeCommandLine( cl, stdout, stderr );
103 System.out.println( "exit value " + exitValue );
104
105
106
107
108
109
110
111
112
113
114
115
116
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
147
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
168 }
169 }
170 }