Skip to main content
summaryrefslogtreecommitdiffstats
blob: c6f1aaa8e42777e5c1e49177de81a9a0f7ab8403 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*******************************************************************************
 * Copyright (c) 2010 Poznan Supercomputing and Networking Center
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Jan Konczak (PSNC) - initial implementation
 ******************************************************************************/

package org.eclipse.ptp.rm.smoa.core.rservices;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Map;

import javax.xml.bind.JAXBException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.ptp.remote.core.IRemoteConnection;
import org.eclipse.ptp.remote.core.IRemoteConnectionChangeListener;
import org.eclipse.ptp.remote.core.IRemoteFileManager;
import org.eclipse.ptp.remote.core.IRemoteServices;
import org.eclipse.ptp.remote.core.exception.RemoteConnectionException;
import org.eclipse.ptp.rm.smoa.core.SMOAConfiguration.AuthType;

import com.smoa.comp.sdk.SMOAFactory;
import com.smoa.comp.sdk.SMOARsync;
import com.smoa.comp.sdk.SMOAStaging;
import com.smoa.comp.sdk.jsdl.JSDL;
import com.smoa.comp.stubs.staging.FileNotFoundFault;
import com.smoa.comp.stubs.staging.NotAuthorizedFault;
import com.smoa.core.sdk.attachments.FileStagingHandler;
import com.smoa.core.sdk.security.AuthenticationModule;
import com.smoa.core.sdk.security.anonymous.AnonymousAuthentication;
import com.smoa.core.sdk.security.gsi.GSIAuthentication;
import com.smoa.core.sdk.security.ssl.SSLAuthentication;
import com.smoa.core.sdk.security.wsse.WSSEAuthentication;
import com.smoa.core.sdk.security.wsse.username.UsernameToken;

/**
 * Main class for maintaining the real connection with SMOA Computing.
 * 
 * Keeps the SMOAFactory, SMOAStaging and SMOARsync objects.
 */
public class SMOAConnection implements IRemoteConnection {

	// Keys for map used to transfer connection attributes
	public static final String TAG_ADDRESS = "address"; //$NON-NLS-1$
	public static final String TAG_PORT = "port"; //$NON-NLS-1$
	public static final String TAG_AUTHTYPE = "auth"; //$NON-NLS-1$
	public static final String TAG_USERNAME = "user"; //$NON-NLS-1$
	public static final String TAG_PASSWORD = "pass"; //$NON-NLS-1$
	public static final String TAG_CACERT = "cacert"; //$NON-NLS-1$
	public static final String TAG_DN = "dn"; //$NON-NLS-1$

	// parent RemoteServices
	private final SMOARemoteServices remoteServices;

	// Connection state
	private boolean connectionIsOpen = false;

	// Crucial objects
	private SMOAFactory besFactory;
	private SMOAStaging smoaStaging;
	private SMOARsync rsync;

	// Connection info
	private String name = null;
	private String url = null;
	private int port = 0;
	private String resourceManagerName;

	// Authentication info
	private AuthType auth;
	private String username;
	private String password;
	private String dn;
	private String cacert;

	// Others
	private IRemoteFileManager fileManager = null;
	private String workDir = "."; //$NON-NLS-1$
	private String homeDir = null;
	private IRemoteConnection fileRemoteConnection = null;

	public SMOAConnection(SMOARemoteServices remoteServices, String name) {
		this.remoteServices = remoteServices;
		this.name = name;
	}

	public void addConnectionChangeListener(IRemoteConnectionChangeListener listener) {
		if (fileRemoteConnection != null) {
			fileRemoteConnection.addConnectionChangeListener(listener);
		}

	}

	public void close() {
		if (fileRemoteConnection != null) {
			fileRemoteConnection.close();
		}
	}

	public void forwardLocalPort(int localPort, String fwdAddress, int fwdPort) throws RemoteConnectionException {
		if (fileRemoteConnection != null) {
			fileRemoteConnection.forwardLocalPort(localPort, fwdAddress, fwdPort);
		}

	}

	public int forwardLocalPort(String fwdAddress, int fwdPort, IProgressMonitor monitor) throws RemoteConnectionException {
		if (fileRemoteConnection != null) {
			return fileRemoteConnection.forwardLocalPort(fwdAddress, fwdPort, monitor);
		}
		return 0;
	}

	public void forwardRemotePort(int remotePort, String fwdAddress, int fwdPort) throws RemoteConnectionException {
		if (fileRemoteConnection != null) {
			fileRemoteConnection.forwardRemotePort(remotePort, fwdAddress, fwdPort);
		}

	}

	public int forwardRemotePort(String fwdAddress, int fwdPort, IProgressMonitor monitor) throws RemoteConnectionException {
		if (fileRemoteConnection != null) {
			return fileRemoteConnection.forwardRemotePort(fwdAddress, fwdPort, monitor);
		}
		return 0;
	}

	public String getAddress() {
		return url;
	}

	public Map<String, String> getAttributes() {
		// TODO Auto-generated method stub
		return null;
	}

	public Map<String, String> getEnv() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getEnv(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	public SMOAFactory getFactory() {
		return besFactory;
	}

	public IRemoteConnection getFileConnection() {
		return fileRemoteConnection;
	}

	public IRemoteFileManager getFileManager() {
		return fileManager;
	}

	public String getHomeDir() {
		if (homeDir != null) {
			return homeDir;
		}

		if (!isOpen()) {
			return "."; //$NON-NLS-1$
		}

		JSDL jsdl;
		try {
			jsdl = smoaStaging.listDirectory(".", null); //$NON-NLS-1$
		} catch (final FileNotFoundFault e) {
			throw new RuntimeException();
		} catch (final NotAuthorizedFault e) {
			throw new RuntimeException();
		}
		homeDir = jsdl.getWorkingDirectory();
		return homeDir;

	}

	public String getName() {
		return name;
	}

	public int getPort() {
		return port;
	}

	public String getProperty(String key) {
		// TODO Auto-generated method stub
		return null;
	}

	public IRemoteServices getRemoteServices() {
		return remoteServices;
	}

	public String getRMName() {
		return resourceManagerName;
	}

	public SMOARsync getRsync() {
		return rsync;
	}

	public SMOAStaging getSMOAStaging() {
		return smoaStaging;
	}

	public String getUsername() {
		return username;
	}

	public String getWorkingDirectory() {
		if (workDir.equals(".")) { //$NON-NLS-1$
			getHomeDir();
			if (homeDir != null) {
				workDir = getHomeDir();
			} else {
				return "."; //$NON-NLS-1$
			}
		}
		if (fileRemoteConnection != null) {
			fileRemoteConnection.setWorkingDirectory(workDir);
		}
		return workDir;
	}

	public boolean isOpen() {
		if (fileRemoteConnection != null && !fileRemoteConnection.isOpen()) {
			return false;
		}
		return connectionIsOpen;
	}

	public void open(IProgressMonitor monitor) throws RemoteConnectionException {
		try {
			final AnonymousAuthentication anonAuth = new AnonymousAuthentication();
			final FileStagingHandler fileStagingHandler = new SMOAFileStagingHandler();

			switch (auth) {
			case Anonymous:
				besFactory = new SMOAFactory(url, port, anonAuth, true);
				smoaStaging = new SMOAStaging(url, port, anonAuth, true, fileStagingHandler);
				rsync = new SMOARsync(url, port, anonAuth, true);
				break;
			case GSI:
				final GSIAuthentication gsi = new GSIAuthentication(dn != null && !dn.isEmpty() ? dn : null);
				besFactory = new SMOAFactory(url, port, gsi, true);
				smoaStaging = new SMOAStaging(url, port, gsi, true, fileStagingHandler);
				rsync = new SMOARsync(url, port, gsi, true);

				break;
			case UsernamePassword: {

				final WSSEAuthentication wsseAuth = new WSSEAuthentication(new UsernameToken(username, password, false, false));

				AuthenticationModule am;

				if (cacert == null) {
					am = anonAuth;
				} else {
					am = new SSLAuthentication(cacert, null, dn != null && !dn.isEmpty() ? dn : null);

				}

				rsync = new SMOARsync(url, port, am, wsseAuth);
				besFactory = new SMOAFactory(url, port, am, wsseAuth);
				smoaStaging = new SMOAStaging(url, port, am, wsseAuth, true, fileStagingHandler);

				break;
			}
			default:
				throw new RuntimeException(Messages.SMOAConnection_UnsupportedAuthType);
			}
			connectionIsOpen = true;

		} catch (final JAXBException e) {
			throw new RemoteConnectionException(e);
		} catch (final IOException e) {
			throw new RemoteConnectionException(e);
		} catch (final GeneralSecurityException e) {
			throw new RemoteConnectionException(e);
		}
		if (fileRemoteConnection != null) {
			fileRemoteConnection.open(monitor);
		}
	}

	public void removeConnectionChangeListener(IRemoteConnectionChangeListener listener) {
		if (fileRemoteConnection != null) {
			fileRemoteConnection.removeConnectionChangeListener(listener);
		}

	}

	public void setAddress(String address) {
		if (address == null) {
			return;
		}
		if (address.equals(url)) {
			return;
		}
		if (isOpen()) {
			throw new RuntimeException(Messages.SMOAConnection_CannotModifyOpenConnection);
		}
		url = address;
	}

	public void setAuthType(AuthType auth) {
		if (this.auth == auth) {
			return;
		}
		if (isOpen()) {
			throw new RuntimeException(Messages.SMOAConnection_CannotModifyOpenConnection);
		}
		this.auth = auth;
	}

	public void setCaCert(String string) {
		cacert = string;
	}

	public void setDN(String string) {
		dn = string;
	}

	public void setFileConnection(IRemoteConnection fileRemoteConnection) {
		if (isOpen()) {
			throw new RuntimeException(Messages.SMOAConnection_CannotModifyOpenConnection);
		}
		this.fileRemoteConnection = fileRemoteConnection;
	}

	public void setFileManager(IRemoteFileManager fileManager) {
		this.fileManager = fileManager;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPort(int port) {
		if (this.port == port) {
			return;
		}
		if (isOpen()) {
			throw new RuntimeException(Messages.SMOAConnection_CannotModifyOpenConnection);
		}
		this.port = port;
	}

	public void setRMName(String rMName) {
		resourceManagerName = rMName;
	}

	public void setUsername(String username) {
		if (isOpen()) {
			throw new RuntimeException();
		}
		this.username = username;
	}

	public void setWorkingDirectory(String path) {
		workDir = path;
		if (fileRemoteConnection != null) {
			fileRemoteConnection.setWorkingDirectory(path);
		}

	}

	public boolean supportsTCPPortForwarding() {
		if (fileRemoteConnection != null) {
			return fileRemoteConnection.supportsTCPPortForwarding();
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ptp.remote.core.IRemoteConnection#setAttribute(java.lang.
	 * String, java.lang.String)
	 */
	public void setAttribute(String key, String value) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ptp.remote.core.IRemoteConnection#setPassword(java.lang.String
	 * )
	 */
	public void setPassword(String password) {
		if (isOpen()) {
			throw new RuntimeException(Messages.SMOAConnection_CannotModifyOpenConnection);
		}
		this.password = password;
	}
}

Back to the top