1 |
|
package org.apache.maven.wagon.providers.ssh.ganymed; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
import ch.ethz.ssh2.Connection; |
20 |
|
import ch.ethz.ssh2.HTTPProxyData; |
21 |
|
import ch.ethz.ssh2.ProxyData; |
22 |
|
import ch.ethz.ssh2.Session; |
23 |
|
import ch.ethz.ssh2.StreamGobbler; |
24 |
|
import org.apache.maven.wagon.CommandExecutionException; |
25 |
|
import org.apache.maven.wagon.Streams; |
26 |
|
import org.apache.maven.wagon.authentication.AuthenticationException; |
27 |
|
import org.apache.maven.wagon.providers.ssh.AbstractSshWagon; |
28 |
|
import org.apache.maven.wagon.providers.ssh.CommandExecutorStreamProcessor; |
29 |
|
import org.codehaus.plexus.util.IOUtil; |
30 |
|
|
31 |
|
import java.io.BufferedReader; |
32 |
|
import java.io.File; |
33 |
|
import java.io.IOException; |
34 |
|
import java.io.InputStream; |
35 |
|
import java.io.InputStreamReader; |
36 |
|
|
37 |
0 |
public abstract class AbstractGanymedWagon |
38 |
|
extends AbstractSshWagon |
39 |
|
{ |
40 |
|
protected Connection connection; |
41 |
|
|
42 |
|
public void openConnection() |
43 |
|
throws AuthenticationException |
44 |
|
{ |
45 |
0 |
super.openConnection(); |
46 |
|
|
47 |
0 |
String host = getRepository().getHost(); |
48 |
0 |
int port = getPort(); |
49 |
|
|
50 |
0 |
File privateKey = getPrivateKey(); |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
0 |
connection = new Connection( host, port ); |
61 |
|
|
62 |
0 |
if ( proxyInfo != null && proxyInfo.getHost() != null ) |
63 |
|
{ |
64 |
0 |
ProxyData proxy = new HTTPProxyData( proxyInfo.getHost(), proxyInfo.getPort(), proxyInfo.getUserName(), |
65 |
|
proxyInfo.getPassword() ); |
66 |
|
|
67 |
0 |
connection.setProxyData( proxy ); |
68 |
|
} |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
try |
92 |
|
{ |
93 |
|
|
94 |
0 |
connection.connect(); |
95 |
|
} |
96 |
0 |
catch ( IOException e ) |
97 |
|
{ |
98 |
0 |
fireSessionError( e ); |
99 |
0 |
throw new AuthenticationException( "Cannot connect. Reason: " + e.getMessage(), e ); |
100 |
0 |
} |
101 |
|
|
102 |
|
try |
103 |
|
{ |
104 |
|
boolean authenticated; |
105 |
|
|
106 |
0 |
if ( privateKey != null && privateKey.exists() ) |
107 |
|
{ |
108 |
0 |
authenticated = connection.authenticateWithPublicKey( authenticationInfo.getUserName(), privateKey, |
109 |
|
authenticationInfo.getPassphrase() ); |
110 |
|
} |
111 |
|
else |
112 |
|
{ |
113 |
0 |
authenticated = connection.authenticateWithPassword( authenticationInfo.getUserName(), |
114 |
|
authenticationInfo.getPassword() ); |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
0 |
if ( !authenticated ) |
119 |
|
{ |
120 |
0 |
throw new AuthenticationException( "Authentication failed." ); |
121 |
|
} |
122 |
|
} |
123 |
0 |
catch ( IOException e ) |
124 |
|
{ |
125 |
0 |
closeConnection(); |
126 |
0 |
fireSessionError( e ); |
127 |
0 |
throw new AuthenticationException( "Cannot authenticate. Reason: " + e.getMessage(), e ); |
128 |
0 |
} |
129 |
0 |
} |
130 |
|
|
131 |
|
|
132 |
|
public Streams executeCommand( String command, boolean ignoreFailures ) |
133 |
|
throws CommandExecutionException |
134 |
|
{ |
135 |
0 |
fireTransferDebug( "Executing command: " + command ); |
136 |
|
|
137 |
|
Session session; |
138 |
|
try |
139 |
|
{ |
140 |
0 |
session = connection.openSession(); |
141 |
|
} |
142 |
0 |
catch ( IOException e ) |
143 |
|
{ |
144 |
0 |
throw new CommandExecutionException( "Cannot open session. Reason: " + e.getMessage(), e ); |
145 |
0 |
} |
146 |
|
|
147 |
|
try |
148 |
|
{ |
149 |
0 |
session.execCommand( command ); |
150 |
|
} |
151 |
0 |
catch ( IOException e ) |
152 |
|
{ |
153 |
0 |
throw new CommandExecutionException( "Cannot execute remote command: " + command, e ); |
154 |
0 |
} |
155 |
|
|
156 |
0 |
InputStream stdout = new StreamGobbler( session.getStdout() ); |
157 |
0 |
InputStream stderr = new StreamGobbler( session.getStderr() ); |
158 |
|
|
159 |
0 |
BufferedReader stdoutReader = new BufferedReader( new InputStreamReader( stdout ) ); |
160 |
0 |
BufferedReader stderrReader = new BufferedReader( new InputStreamReader( stderr ) ); |
161 |
|
|
162 |
|
try |
163 |
|
{ |
164 |
0 |
Streams streams = CommandExecutorStreamProcessor.processStreams( stderrReader, stdoutReader ); |
165 |
|
|
166 |
0 |
if ( streams.getErr().length() > 0 ) |
167 |
|
{ |
168 |
0 |
int exitCode = session.getExitStatus().intValue(); |
169 |
0 |
throw new CommandExecutionException( "Exit code: " + exitCode + " - " + streams.getErr() ); |
170 |
|
} |
171 |
|
|
172 |
0 |
return streams; |
173 |
|
} |
174 |
0 |
catch ( IOException e ) |
175 |
|
{ |
176 |
0 |
throw new CommandExecutionException( "Cannot read streams after remote command: " + command, e ); |
177 |
|
} |
178 |
|
finally |
179 |
|
{ |
180 |
0 |
IOUtil.close( stdoutReader ); |
181 |
0 |
IOUtil.close( stderrReader ); |
182 |
|
} |
183 |
|
} |
184 |
|
|
185 |
|
public void closeConnection() |
186 |
|
{ |
187 |
0 |
if ( connection != null ) |
188 |
|
{ |
189 |
0 |
connection.close(); |
190 |
0 |
connection = null; |
191 |
|
} |
192 |
0 |
} |
193 |
|
} |