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 | 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 | |
|
93 | |
|
94 | 0 | FileUtils.fileWrite( tmpFile, commandLine ); |
95 | |
|
96 | 0 | Commandline cl = new Commandline(); |
97 | 0 | cl.setExecutable( "/bin/sh" ); |
98 | |
|
99 | |
|
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 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
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 | |
|
147 | |
|
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 | |
|
168 | 0 | } |
169 | 0 | } |
170 | |
} |