1 package org.apache.maven.scm.provider.integrity;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import com.mks.api.CmdRunner;
23 import com.mks.api.Command;
24 import com.mks.api.IntegrationPoint;
25 import com.mks.api.IntegrationPointFactory;
26 import com.mks.api.Session;
27 import com.mks.api.response.APIException;
28 import com.mks.api.response.Response;
29 import org.apache.maven.scm.log.ScmLogger;
30 import org.codehaus.plexus.util.StringUtils;
31
32 import java.io.IOException;
33
34
35
36
37
38
39
40 public class APISession
41 {
42
43 public static final String VERSION =
44 IntegrationPointFactory.getAPIVersion().substring( 0, IntegrationPointFactory.getAPIVersion().indexOf( ' ' ) );
45
46 public static final int MAJOR_VERSION = Integer.parseInt( VERSION.substring( 0, VERSION.indexOf( '.' ) ) );
47
48 public static final int MINOR_VERSION =
49 Integer.parseInt( VERSION.substring( VERSION.indexOf( '.' ) + 1, VERSION.length() ) );
50
51
52 private ScmLogger logger;
53
54
55 private String hostName;
56
57 private int port = 0;
58
59 private String userName;
60
61 private String password;
62
63
64 private IntegrationPoint ip;
65
66 private Session session;
67
68 private boolean terminated;
69
70
71
72
73
74
75
76 public APISession( ScmLogger logger )
77 {
78 logger.info( "MKS Integrity API Version: " + VERSION );
79 this.logger = logger;
80 }
81
82
83
84
85
86
87
88
89
90
91 public Response connect( String host, int portNum, String user, String paswd )
92 throws APIException
93 {
94
95 terminated = false;
96
97 ip = IntegrationPointFactory.getInstance().createLocalIntegrationPoint( MAJOR_VERSION, MINOR_VERSION );
98
99 ip.setAutoStartIntegrityClient( true );
100
101 if ( null != paswd && paswd.length() > 0 )
102 {
103 logger.info( "Creating session for " + user + "/" + StringUtils.repeat( "*", paswd.length() ) );
104 session = ip.createSession( user, paswd );
105 logger.info( "Attempting to establish connection using " + user + "@" + host + ":" + portNum );
106 }
107 else
108 {
109 logger.info( "Using a common session. Connection information is obtained from client preferences" );
110 session = ip.getCommonSession();
111 }
112
113 Command ping = new Command( Command.SI, "connect" );
114 CmdRunner cmdRunner = session.createCmdRunner();
115
116 if ( null != host && host.length() > 0 )
117 {
118 cmdRunner.setDefaultHostname( host );
119 }
120 if ( portNum > 0 )
121 {
122 cmdRunner.setDefaultPort( portNum );
123 }
124 if ( null != user && user.length() > 0 )
125 {
126 cmdRunner.setDefaultUsername( user );
127 }
128 if ( null != paswd && paswd.length() > 0 )
129 {
130 cmdRunner.setDefaultPassword( paswd );
131 }
132
133 Response res = cmdRunner.execute( ping );
134 logger.debug( res.getCommandString() + " returned exit code " + res.getExitCode() );
135
136 hostName = res.getConnectionHostname();
137 port = res.getConnectionPort();
138 userName = res.getConnectionUsername();
139 password = paswd;
140 cmdRunner.release();
141 logger.info( "Successfully established connection " + userName + "@" + hostName + ":" + port );
142 return res;
143 }
144
145
146
147
148
149
150
151
152 public Response runCommand( Command cmd )
153 throws APIException
154 {
155 CmdRunner cmdRunner = session.createCmdRunner();
156 cmdRunner.setDefaultHostname( hostName );
157 cmdRunner.setDefaultPort( port );
158 cmdRunner.setDefaultUsername( userName );
159 if ( null != password && password.length() > 0 )
160 {
161 cmdRunner.setDefaultPassword( password );
162 }
163 Response res = cmdRunner.execute( cmd );
164 logger.debug( res.getCommandString() + " returned exit code " + res.getExitCode() );
165 cmdRunner.release();
166 return res;
167 }
168
169
170
171
172
173
174
175
176
177 public Response runCommandAs( Command cmd, String impersonateUser )
178 throws APIException
179 {
180 CmdRunner cmdRunner = session.createCmdRunner();
181 cmdRunner.setDefaultHostname( hostName );
182 cmdRunner.setDefaultPort( port );
183 cmdRunner.setDefaultUsername( userName );
184 if ( null != password && password.length() > 0 )
185 {
186 cmdRunner.setDefaultPassword( password );
187 }
188 cmdRunner.setDefaultImpersonationUser( impersonateUser );
189 Response res = cmdRunner.execute( cmd );
190 logger.debug( res.getCommandString() + " returned exit code " + res.getExitCode() );
191 cmdRunner.release();
192 return res;
193 }
194
195
196
197
198 public void terminate()
199 {
200
201 if ( !terminated )
202 {
203 try
204 {
205 if ( null != session )
206 {
207 session.release();
208 }
209
210 if ( null != ip )
211 {
212 ip.release();
213 }
214 terminated = true;
215 logger.info( "Successfully disconnected connection " + userName + "@" + hostName + ":" + port );
216 }
217 catch ( APIException aex )
218 {
219 logger.debug( "Caught API Exception when releasing session!" );
220 aex.printStackTrace();
221 }
222 catch ( IOException ioe )
223 {
224 logger.debug( "Caught IO Exception when releasing session!" );
225 ioe.printStackTrace();
226 }
227 }
228 }
229
230
231
232
233
234
235 public String getHostName()
236 {
237 return hostName;
238 }
239
240
241
242
243
244
245 public int getPort()
246 {
247 return port;
248 }
249
250
251
252
253
254
255 public String getUserName()
256 {
257 return userName;
258 }
259
260
261
262
263
264
265 public String getPassword()
266 {
267 if ( null != password && password.length() > 0 )
268 {
269 return password;
270 }
271 else
272 {
273 return "";
274 }
275 }
276
277
278
279
280 public ScmLogger getLogger()
281 {
282 return logger;
283 }
284 }