Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5e447a7e9d27b76a6441336e8aa6036adc63e135 (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
/*******************************************************************************
 * Copyright (c) 2007, 2013 IBM Corporation and others.
 * 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:
 * IBM Corporation - Initial API and implementation
 *******************************************************************************/
package org.eclipse.ptp.remote.rse.core;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.ptp.internal.remote.rse.core.LocalSpawnerSubsystem;
import org.eclipse.ptp.internal.remote.rse.core.SpawnerSubsystem;
import org.eclipse.ptp.remote.core.AbstractRemoteProcessBuilder;
import org.eclipse.ptp.remote.core.IRemoteConnection;
import org.eclipse.ptp.remote.core.IRemoteFileManager;
import org.eclipse.ptp.remote.core.IRemoteProcess;
import org.eclipse.ptp.remote.rse.core.messages.Messages;
import org.eclipse.rse.connectorservice.dstore.DStoreConnectorService;
import org.eclipse.rse.core.subsystems.IConnectorService;
import org.eclipse.rse.core.subsystems.ISubSystem;
import org.eclipse.rse.internal.connectorservice.local.LocalConnectorService;
import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
import org.eclipse.rse.services.shells.IHostShell;
import org.eclipse.rse.services.shells.IShellService;

@SuppressWarnings("restriction")
public class RSEProcessBuilder extends AbstractRemoteProcessBuilder {
	private final static String EXIT_CMD = "exit"; //$NON-NLS-1$
	private final static String CMD_DELIMITER = ";"; //$NON-NLS-1$

	private final RSEConnection fConnection;
	private final RSEFileManager fFileMgr;

	private Map<String, String> fRemoteEnv = new HashMap<String, String>();

	/**
	 * @since 4.0
	 */
	public RSEProcessBuilder(IRemoteConnection conn, IRemoteFileManager fileMgr, List<String> command) {
		super(conn, command);
		fConnection = (RSEConnection) conn;
		fFileMgr = (RSEFileManager) fileMgr;
		fRemoteEnv = new HashMap<String, String>(conn.getEnv());
	}

	/**
	 * @since 4.0
	 */
	public RSEProcessBuilder(IRemoteConnection conn, IRemoteFileManager fileMgr, String... command) {
		this(conn, fileMgr, Arrays.asList(command));
	}

	/**
	 * Convert environment map back to environment strings.
	 * 
	 * @return array of environment variables
	 */
	private String[] getEnvironment() {
		String[] env = new String[fRemoteEnv.size()];
		int pos = 0;
		for (Map.Entry<String, String> entry : fRemoteEnv.entrySet()) {
			env[pos++] = entry.getKey() + "=" + entry.getValue(); //$NON-NLS-1$
		}
		return env;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ptp.remote.AbstractRemoteProcessBuilder#environment()
	 */
	@Override
	public Map<String, String> environment() {
		return fRemoteEnv;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ptp.remote.core.AbstractRemoteProcessBuilder#getSupportedFlags
	 * ()
	 */
	@Override
	public int getSupportedFlags() {
		return NONE;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ptp.remote.IRemoteProcessBuilder#start(int)
	 */
	@Override
	public IRemoteProcess start(int flags) throws IOException {
		List<String> cmdArgs = command();
		if (cmdArgs.size() < 1) {
			throw new IndexOutOfBoundsException();
		}

		String remoteCmd = ""; //$NON-NLS-1$

		for (int i = 0; i < cmdArgs.size(); i++) {
			if (i > 0) {
				remoteCmd += " "; //$NON-NLS-1$
			}
			remoteCmd += spaceEscapify(cmdArgs.get(i));
		}

		IHostShell hostShell = null;
		try {
			String initialDir = ""; //$NON-NLS-1$
			if (directory() != null) {
				initialDir = directory().toURI().getPath();
			}

			SpawnerSubsystem subsystem = getSpawnerSubsystem();
			
			if(subsystem instanceof LocalSpawnerSubsystem) {
				Process process = subsystem.spawnLocalRedirected(remoteCmd, initialDir, null, getEnvironment(), new NullProgressMonitor());
				return new LocalProcessWrapper(process);
			}
			
			else {
				if (subsystem != null) {
					hostShell = subsystem.spawnRedirected(remoteCmd, initialDir, null, getEnvironment(),
							new NullProgressMonitor());

					if (hostShell == null) {
						// fall back to old method of using RSE
						hostShell = launchCommandWithRSE(remoteCmd, initialDir);
					}
				} else {
					// fall back to old method of using RSE
					hostShell = launchCommandWithRSE(remoteCmd, initialDir);
				}
			}
		} catch (SystemMessageException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

		return new RSEProcess(hostShell, redirectErrorStream());
	}

	private IHostShell launchCommandWithRSE(String remoteCmd, String initialDir) throws IOException, SystemMessageException {
		// The exit command is called to force the remote shell to close after
		// our command
		// is executed. This is to prevent a running process at the end of the
		// debug session.
		// See Bug 158786.
		IHostShell hostShell;
		remoteCmd += CMD_DELIMITER + EXIT_CMD;

		IShellService shellService = fConnection.getRemoteShellService();
		if (shellService == null) {
			throw new IOException(Messages.RSEProcessBuilder_0);
		}
		hostShell = shellService.runCommand(initialDir, remoteCmd, getEnvironment(), new NullProgressMonitor());
		return hostShell;
	}

	private SpawnerSubsystem getSpawnerSubsystem() {
		IConnectorService connectorService = getDStoreConnectorService();

		if (connectorService == null) {
			// try getting the local connector service
			connectorService = getLocalConnectorService();
			if(connectorService == null) {
				return null;
			}
		}

		ISubSystem subsystems[] = connectorService.getSubSystems();

		for (ISubSystem subsystem : subsystems) {
			if (subsystem instanceof SpawnerSubsystem) {
				return (SpawnerSubsystem) subsystem;
			}
		}

		return null;
	}

	private LocalConnectorService getLocalConnectorService() {
		for (IConnectorService service : fConnection.getHost().getConnectorServices()) {
			if (service instanceof LocalConnectorService) {
				return (LocalConnectorService) service;
			}
		}
		
		return null;
	}

	private DStoreConnectorService getDStoreConnectorService() {
		for (IConnectorService service : fConnection.getHost().getConnectorServices()) {
			if (service instanceof DStoreConnectorService) {
				return (DStoreConnectorService) service;
			}
		}

		return null;
	}

	/**
 	 * Escape spaces. Only spaces inside a PAIR of single/double quotes would be taken as part of the string and remains.
	 * For strings with not balanced quotes, such as <abc'd ef>, will not result in the space being escaped.
	 * 
	 * @param inputString
	 * @return
	 */
	private static String spaceEscapify(String inputString) {
		String result = null;
		if (inputString == null) {
			return null;
		}else{
			char[] array = inputString.toCharArray();
			StringBuffer buffer = new StringBuffer();
			boolean doubleQuote = false;
			boolean singleQuote = false;
			for (int i = 0; i < array.length; i++) {
				char c = array[i];
				if (c == '"') {
					// Check if there is space inside single/double quotes.
					if (i > 0 && array[i - 1] == '\\') {
						doubleQuote = false;
					} else if (doubleQuote){
						// Met the second double quotes, spaces outside would be escaped
						// So reset the doubleComment flag and singleComment flag to false;
						doubleQuote = !doubleQuote;
						singleQuote = false;
					}else {
						// Met the first double quotes, so change the flag to indicate inside the doubleComment
						doubleQuote = !doubleQuote;
					}
				}else if (c == '\''){
					if (i > 0 && array[i - 1] == '\\') {
						singleQuote = false;
					} else if (singleQuote){
						singleQuote = !singleQuote;
						doubleQuote = false;
					}else {
						singleQuote = !singleQuote;
					}
				}
				if (c == ' ' && !doubleQuote && !singleQuote) {
					buffer.append('\\');
					buffer.append(c);
				} else {
					buffer.append(c);
				}
			}
			result = buffer.toString();
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ptp.remote.core.AbstractRemoteProcessBuilder#directory()
	 */
	@Override
	public IFileStore directory() {
		if (super.directory() == null) {
			// check PWD first for UNIX systems
			String cwd = environment().get("PWD"); //$NON-NLS-1$

			// if that didn't work, try %CD% for Windows systems
			if (cwd == null) {
				cwd = environment().get("CD"); //$NON-NLS-1$
			}

			if (cwd != null) {
				return fFileMgr.getResource(cwd);
			}

		}
		return super.directory();
	}

}

Back to the top