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