diff options
Diffstat (limited to 'plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/RemoteHelper.java')
-rw-r--r-- | plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/RemoteHelper.java | 680 |
1 files changed, 680 insertions, 0 deletions
diff --git a/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/RemoteHelper.java b/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/RemoteHelper.java new file mode 100644 index 0000000..e0097ec --- /dev/null +++ b/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/RemoteHelper.java | |||
@@ -0,0 +1,680 @@ | |||
1 | /******************************************************************************** | ||
2 | * Copyright (c) 2013 MontaVista Software, Inc and Others. | ||
3 | * This program and the accompanying materials are made available under the terms | ||
4 | * of the Eclipse Public License v1.0 which accompanies this distribution, and is | ||
5 | * available at http://www.eclipse.org/legal/epl-v10.html | ||
6 | * | ||
7 | * Contributors: | ||
8 | * Anna Dushistova (MontaVista) - initial API and implementation | ||
9 | * Lianhao Lu (Intel) - Modified to add other file operations. | ||
10 | * Ioana Grigoropol (Intel) - Separated remote functionality | ||
11 | ********************************************************************************/ | ||
12 | package org.yocto.remote.utils; | ||
13 | |||
14 | import java.io.BufferedInputStream; | ||
15 | import java.io.BufferedOutputStream; | ||
16 | import java.io.BufferedReader; | ||
17 | import java.io.File; | ||
18 | import java.io.FileInputStream; | ||
19 | import java.io.FileNotFoundException; | ||
20 | import java.io.FileOutputStream; | ||
21 | import java.io.FileReader; | ||
22 | import java.io.IOException; | ||
23 | import java.io.InputStream; | ||
24 | import java.io.PrintWriter; | ||
25 | import java.net.URI; | ||
26 | import java.net.URISyntaxException; | ||
27 | import java.nio.file.Files; | ||
28 | import java.util.ArrayList; | ||
29 | import java.util.Arrays; | ||
30 | import java.util.HashMap; | ||
31 | import java.util.Iterator; | ||
32 | import java.util.List; | ||
33 | import java.util.Map; | ||
34 | |||
35 | import org.eclipse.core.internal.filesystem.InternalFileSystemCore; | ||
36 | import org.eclipse.core.internal.resources.LocalMetaArea; | ||
37 | import org.eclipse.core.internal.resources.Workspace; | ||
38 | import org.eclipse.core.resources.ResourcesPlugin; | ||
39 | import org.eclipse.core.runtime.CoreException; | ||
40 | import org.eclipse.core.runtime.FileLocator; | ||
41 | import org.eclipse.core.runtime.IPath; | ||
42 | import org.eclipse.core.runtime.IProgressMonitor; | ||
43 | import org.eclipse.core.runtime.IStatus; | ||
44 | import org.eclipse.core.runtime.MultiStatus; | ||
45 | import org.eclipse.core.runtime.NullProgressMonitor; | ||
46 | import org.eclipse.core.runtime.Path; | ||
47 | import org.eclipse.core.runtime.Status; | ||
48 | import org.eclipse.core.runtime.SubProgressMonitor; | ||
49 | import org.eclipse.osgi.util.NLS; | ||
50 | import org.eclipse.rse.core.IRSECoreStatusCodes; | ||
51 | import org.eclipse.rse.core.IRSEInitListener; | ||
52 | import org.eclipse.rse.core.IRSESystemType; | ||
53 | import org.eclipse.rse.core.IRSEUserIdConstants; | ||
54 | import org.eclipse.rse.core.RSECorePlugin; | ||
55 | import org.eclipse.rse.core.model.IHost; | ||
56 | import org.eclipse.rse.core.model.ISubSystemConfigurationCategories; | ||
57 | import org.eclipse.rse.core.model.ISubSystemConfigurator; | ||
58 | import org.eclipse.rse.core.model.ISystemHostPool; | ||
59 | import org.eclipse.rse.core.model.ISystemRegistry; | ||
60 | import org.eclipse.rse.core.IRSECoreRegistry; | ||
61 | import org.eclipse.rse.core.subsystems.ISubSystem; | ||
62 | import org.eclipse.rse.core.subsystems.ISubSystemConfiguration; | ||
63 | import org.eclipse.rse.internal.core.RSEInitJob; | ||
64 | import org.eclipse.rse.internal.ui.view.SystemPerspectiveHelpers; | ||
65 | import org.eclipse.rse.services.IService; | ||
66 | import org.eclipse.rse.services.clientserver.messages.SystemMessageException; | ||
67 | import org.eclipse.rse.services.files.IFileService; | ||
68 | import org.eclipse.rse.services.files.IHostFile; | ||
69 | import org.eclipse.rse.services.shells.HostShellProcessAdapter; | ||
70 | import org.eclipse.rse.services.shells.IHostShell; | ||
71 | import org.eclipse.rse.services.shells.IShellService; | ||
72 | import org.eclipse.rse.subsystems.files.core.model.RemoteFileUtility; | ||
73 | import org.eclipse.rse.subsystems.files.core.servicesubsystem.FileServiceSubSystem; | ||
74 | import org.eclipse.rse.subsystems.files.core.servicesubsystem.IFileServiceSubSystem; | ||
75 | import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFileSubSystem; | ||
76 | import org.eclipse.rse.subsystems.shells.core.subsystems.servicesubsystem.IShellServiceSubSystem; | ||
77 | import org.eclipse.rse.subsystems.terminals.core.ITerminalServiceSubSystem; | ||
78 | import org.eclipse.rse.ui.RSEUIPlugin; | ||
79 | import org.eclipse.rse.ui.SystemBasePlugin; | ||
80 | import org.eclipse.rse.ui.SystemConnectionForm; | ||
81 | import org.eclipse.ui.console.MessageConsole; | ||
82 | |||
83 | public class RemoteHelper { | ||
84 | private final static String EXIT_CMD = "exit"; //$NON-NLS-1$ | ||
85 | private final static String CMD_DELIMITER = ";"; //$NON-NLS-1$ | ||
86 | private final static String LOCAL_CONN_NAME = "Local"; | ||
87 | private final static String CONNECTION_NAME = "127.0.0.1"; | ||
88 | private final static String HOST_NAME = "LOCALHOST"; | ||
89 | public static final String TERMINATOR = "234o987dsfkcqiuwey18837032843259d";//$NON-NLS-1$ | ||
90 | public static final int TOTALWORKLOAD = 100; | ||
91 | private static Map<IHost, RemoteMachine> machines; | ||
92 | |||
93 | public static IPath getWorkspaceMetaArea(){ | ||
94 | Workspace workspace = (Workspace)ResourcesPlugin.getWorkspace(); | ||
95 | LocalMetaArea metaDataArea = workspace.getMetaArea(); | ||
96 | return metaDataArea.getLocation(); | ||
97 | } | ||
98 | public static void storeProjDescrInMetaArea(IHost conn, String projName, String remoteSrc){ | ||
99 | IPath path = getWorkspaceMetaArea(); | ||
100 | String sep = File.separator; | ||
101 | String localDest = path.toString() + sep + ".projects" + sep + projName + sep + ".project"; | ||
102 | try { | ||
103 | getRemoteFile(conn, localDest, remoteSrc + sep + ".project", new NullProgressMonitor()); | ||
104 | } catch (Exception e) { | ||
105 | e.printStackTrace(); | ||
106 | } | ||
107 | } | ||
108 | public static String retrieveProjRootFromMetaArea(String projName){ | ||
109 | IPath path = getWorkspaceMetaArea(); | ||
110 | String sep = File.separator; | ||
111 | return path.toString() + sep + ".projects" + sep + projName + sep; | ||
112 | } | ||
113 | public static void storeURIInMetaArea(String projName, URI uri){ | ||
114 | IPath path = getWorkspaceMetaArea(); | ||
115 | String sep = File.separator; | ||
116 | File f = new File(path.toString() + sep + ".projects" + sep + projName + sep + ".originalURI"); | ||
117 | PrintWriter writer; | ||
118 | try { | ||
119 | writer = new PrintWriter(f); | ||
120 | writer.println(uri.getScheme()); | ||
121 | writer.println(uri.getHost()); | ||
122 | writer.println(uri.getPath()); | ||
123 | writer.println(uri.getFragment()); | ||
124 | writer.close(); | ||
125 | } catch (FileNotFoundException e) { | ||
126 | e.printStackTrace(); | ||
127 | } | ||
128 | } | ||
129 | |||
130 | public static URI retrieveURIFromMetaArea(String projName){ | ||
131 | IPath path = getWorkspaceMetaArea(); | ||
132 | String sep = File.separator; | ||
133 | File f = new File(path.toString() + sep + ".projects" + sep + projName + sep + ".originalURI"); | ||
134 | try { | ||
135 | BufferedReader buf = new BufferedReader(new FileReader(f)); | ||
136 | String line = null; | ||
137 | List<String> elems = new ArrayList<String>(); | ||
138 | while((line = buf.readLine()) != null){ | ||
139 | if (line.equals("null")) | ||
140 | line = null; | ||
141 | elems.add(line); | ||
142 | } | ||
143 | buf.close(); | ||
144 | if (elems.size() == 4){ | ||
145 | URI uri = new URI(elems.get(0), elems.get(1), elems.get(2), elems.get(3)); | ||
146 | return uri; | ||
147 | } | ||
148 | } catch (IOException e) { | ||
149 | e.printStackTrace(); | ||
150 | } catch (URISyntaxException e) { | ||
151 | e.printStackTrace(); | ||
152 | } | ||
153 | return null; | ||
154 | } | ||
155 | |||
156 | public static boolean isInitialized(final URI uri){ | ||
157 | boolean init = RSECorePlugin.isInitComplete(RSECorePlugin.INIT_MODEL); | ||
158 | if (!init) { | ||
159 | RSEInitJob.getInstance().addInitListener(new IRSEInitListener() { | ||
160 | @Override | ||
161 | public void phaseComplete(int arg0) { | ||
162 | try { | ||
163 | InternalFileSystemCore.getInstance().getStore(uri); | ||
164 | } catch (CoreException e) { | ||
165 | e.printStackTrace(); | ||
166 | } | ||
167 | } | ||
168 | }); | ||
169 | } | ||
170 | return init; | ||
171 | } | ||
172 | |||
173 | public static IHost getRemoteConnectionByName(String remoteConnection) { | ||
174 | if (remoteConnection == null) | ||
175 | return null; | ||
176 | IHost[] connections = RSECorePlugin.getTheSystemRegistry().getHosts(); | ||
177 | for (int i = 0; i < connections.length; i++) | ||
178 | if (connections[i].getAliasName().equals(remoteConnection)) | ||
179 | return connections[i]; | ||
180 | return null; | ||
181 | } | ||
182 | |||
183 | public static ISubSystem getFileSubsystem(IHost host) { | ||
184 | if (host == null) | ||
185 | return null; | ||
186 | ISubSystem[] subSystems = host.getSubSystems(); | ||
187 | for (int i = 0; i < subSystems.length; i++) { | ||
188 | if (subSystems[i] instanceof IFileServiceSubSystem) | ||
189 | return subSystems[i]; | ||
190 | } | ||
191 | return null; | ||
192 | } | ||
193 | |||
194 | public static ISubSystem getShellSubsystem(IHost host) { | ||
195 | if (host == null) | ||
196 | return null; | ||
197 | ISubSystem[] subSystems = host.getSubSystems(); | ||
198 | for (int i = 0; i < subSystems.length; i++) { | ||
199 | if (subSystems[i] instanceof IShellServiceSubSystem) | ||
200 | return subSystems[i]; | ||
201 | } | ||
202 | return null; | ||
203 | } | ||
204 | |||
205 | public static IHost[] getSuitableConnections() { | ||
206 | |||
207 | //we only get RSE connections with files&cmds subsystem | ||
208 | ArrayList <IHost> filConnections = new ArrayList <IHost>(Arrays.asList(RSECorePlugin.getTheSystemRegistry() | ||
209 | .getHostsBySubSystemConfigurationCategory(ISubSystemConfigurationCategories.SUBSYSTEM_CATEGORY_FILES))); //$NON-NLS-1$ | ||
210 | |||
211 | ArrayList <IHost> terminalConnections = new ArrayList <IHost>(Arrays.asList(RSECorePlugin.getTheSystemRegistry() | ||
212 | .getHostsBySubSystemConfigurationCategory("terminal")));//$NON-NLS-1$ | ||
213 | |||
214 | ArrayList <IHost> shellConnections = new ArrayList <IHost>(Arrays.asList(RSECorePlugin.getTheSystemRegistry() | ||
215 | .getHostsBySubSystemConfigurationCategory("shells"))); //$NON-NLS-1$ | ||
216 | |||
217 | Iterator <IHost>iter = filConnections.iterator(); | ||
218 | while(iter.hasNext()){ | ||
219 | IHost fileConnection = iter.next(); | ||
220 | if(!terminalConnections.contains(fileConnection) && !shellConnections.contains(fileConnection)){ | ||
221 | iter.remove(); | ||
222 | } | ||
223 | IRSESystemType sysType = fileConnection.getSystemType(); | ||
224 | if (sysType == null || !sysType.isEnabled()) { | ||
225 | iter.remove(); | ||
226 | } | ||
227 | } | ||
228 | |||
229 | return filConnections.toArray(new IHost[filConnections.size()]); | ||
230 | } | ||
231 | |||
232 | public static void putRemoteFileInPlugin(IHost connection, String locaPathInPlugin, String remoteExePath, | ||
233 | IProgressMonitor monitor) throws Exception { | ||
234 | |||
235 | assert(connection != null); | ||
236 | monitor.beginTask(Messages.InfoUpload, 100); | ||
237 | |||
238 | IFileService fileService; | ||
239 | try { | ||
240 | fileService = getConnectedRemoteFileService( | ||
241 | connection, | ||
242 | new SubProgressMonitor(monitor, 5)); | ||
243 | InputStream inputStream = FileLocator.openStream( | ||
244 | Activator.getDefault().getBundle(), new Path(locaPathInPlugin), false); | ||
245 | Path remotePath = new Path(remoteExePath); | ||
246 | |||
247 | //TODO workaround for now | ||
248 | //in case the underlying scp file service doesn't support inputStream upload | ||
249 | BufferedInputStream bis = new BufferedInputStream(inputStream); | ||
250 | File tempFile = File.createTempFile("scp", "temp"); //$NON-NLS-1$ //$NON-NLS-2$ | ||
251 | FileOutputStream os = new FileOutputStream(tempFile); | ||
252 | BufferedOutputStream bos = new BufferedOutputStream(os); | ||
253 | byte[] buffer = new byte[1024]; | ||
254 | int readCount; | ||
255 | while( (readCount = bis.read(buffer)) > 0) | ||
256 | { | ||
257 | bos.write(buffer, 0, readCount); | ||
258 | } | ||
259 | bos.close(); | ||
260 | fileService.upload(tempFile, remotePath.removeLastSegments(1) | ||
261 | .toString(), remotePath.lastSegment(), true, null, null, | ||
262 | new SubProgressMonitor(monitor, 80)); | ||
263 | // Need to change the permissions to match the original file | ||
264 | // permissions because of a bug in upload | ||
265 | remoteShellExec( | ||
266 | connection, | ||
267 | "", "chmod", "+x " + spaceEscapify(remotePath.toString()), new SubProgressMonitor(monitor, 5)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | ||
268 | |||
269 | } finally { | ||
270 | monitor.done(); | ||
271 | } | ||
272 | return; | ||
273 | } | ||
274 | |||
275 | public static void getRemoteFile(IHost connection, String localExePath, String remoteExePath, | ||
276 | IProgressMonitor monitor) throws Exception { | ||
277 | |||
278 | assert(connection!=null); | ||
279 | monitor.beginTask(Messages.InfoDownload, 100); | ||
280 | |||
281 | IFileService fileService; | ||
282 | try { | ||
283 | fileService = getConnectedRemoteFileService( | ||
284 | connection, | ||
285 | new SubProgressMonitor(monitor, 10)); | ||
286 | File file = new File(localExePath); | ||
287 | monitor.worked(5); | ||
288 | Path remotePath = new Path(remoteExePath); | ||
289 | fileService.download(remotePath.removeLastSegments(1).toString(), | ||
290 | remotePath.lastSegment(),file,true, null, | ||
291 | new SubProgressMonitor(monitor, 85)); | ||
292 | // Need to change the permissions to match the original file | ||
293 | // permissions because of a bug in upload | ||
294 | //RemoteApplication p = remoteShellExec( | ||
295 | // config, | ||
296 | // "", "chmod", "+x " + spaceEscapify(remotePath.toString()), new SubProgressMonitor(monitor, 5)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | ||
297 | //Thread.sleep(500); | ||
298 | //p.destroy(); | ||
299 | |||
300 | } finally { | ||
301 | monitor.done(); | ||
302 | } | ||
303 | return; | ||
304 | } | ||
305 | |||
306 | public static ITerminalServiceSubSystem getTerminalSubSystem( | ||
307 | IHost connection) { | ||
308 | ISystemRegistry systemRegistry = RSECorePlugin.getTheSystemRegistry(); | ||
309 | ISubSystem[] subsystems = systemRegistry.getSubSystems(connection); | ||
310 | for (int i = 0; i < subsystems.length; i++) { | ||
311 | if (subsystems[i] instanceof ITerminalServiceSubSystem) { | ||
312 | ITerminalServiceSubSystem subSystem = (ITerminalServiceSubSystem) subsystems[i]; | ||
313 | return subSystem; | ||
314 | } | ||
315 | } | ||
316 | return null; | ||
317 | } | ||
318 | |||
319 | public static String spaceEscapify(String inputString) { | ||
320 | if (inputString == null) | ||
321 | return null; | ||
322 | |||
323 | return inputString.replaceAll(" ", "\\\\ "); //$NON-NLS-1$ //$NON-NLS-2$ | ||
324 | } | ||
325 | |||
326 | public static Process remoteShellExec(IHost connection, | ||
327 | String prelaunchCmd, String remoteCommandPath, String arguments, | ||
328 | IProgressMonitor monitor) throws CoreException { | ||
329 | |||
330 | monitor.beginTask(NLS.bind(Messages.RemoteShellExec_1, | ||
331 | remoteCommandPath, arguments), 10); | ||
332 | String realRemoteCommand = arguments == null ? spaceEscapify(remoteCommandPath) | ||
333 | : spaceEscapify(remoteCommandPath) + " " + arguments; //$NON-NLS-1$ | ||
334 | |||
335 | String remoteCommand = realRemoteCommand + CMD_DELIMITER + EXIT_CMD; | ||
336 | |||
337 | if(prelaunchCmd != null) { | ||
338 | if (!prelaunchCmd.trim().equals("")) //$NON-NLS-1$ | ||
339 | remoteCommand = prelaunchCmd + CMD_DELIMITER + remoteCommand; | ||
340 | } | ||
341 | |||
342 | IShellService shellService; | ||
343 | Process p = null; | ||
344 | try { | ||
345 | shellService = (IShellService) getConnectedShellService( | ||
346 | connection, | ||
347 | new SubProgressMonitor(monitor, 7)); | ||
348 | |||
349 | // This is necessary because runCommand does not actually run the | ||
350 | // command right now. | ||
351 | String env[] = new String[0]; | ||
352 | try { | ||
353 | IHostShell hostShell = shellService.launchShell( | ||
354 | "", env, new SubProgressMonitor(monitor, 3)); //$NON-NLS-1$ | ||
355 | hostShell.writeToShell(remoteCommand); | ||
356 | p = new HostShellProcessAdapter(hostShell); | ||
357 | } catch (Exception e) { | ||
358 | if (p != null) { | ||
359 | p.destroy(); | ||
360 | } | ||
361 | abort(Messages.RemoteShellExec_2, e, | ||
362 | IRSECoreStatusCodes.EXCEPTION_OCCURRED); | ||
363 | } | ||
364 | } catch (Exception e1) { | ||
365 | abort(e1.getMessage(), e1, | ||
366 | IRSECoreStatusCodes.EXCEPTION_OCCURRED); | ||
367 | } | ||
368 | |||
369 | monitor.done(); | ||
370 | return p; | ||
371 | } | ||
372 | |||
373 | /** | ||
374 | * Throws a core exception with an error status object built from the given | ||
375 | * message, lower level exception, and error code. | ||
376 | * | ||
377 | * @param message | ||
378 | * the status message | ||
379 | * @param exception | ||
380 | * lower level exception associated with the error, or | ||
381 | * <code>null</code> if none | ||
382 | * @param code | ||
383 | * error code | ||
384 | */ | ||
385 | public static void abort(String message, Throwable exception, int code) throws CoreException { | ||
386 | IStatus status; | ||
387 | if (exception != null) { | ||
388 | MultiStatus multiStatus = new MultiStatus(Activator.PLUGIN_ID, code, message, exception); | ||
389 | multiStatus.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, code, exception.getLocalizedMessage(), exception)); | ||
390 | status = multiStatus; | ||
391 | } else { | ||
392 | status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, code, message, null); | ||
393 | } | ||
394 | throw new CoreException(status); | ||
395 | } | ||
396 | /** | ||
397 | * Checks whether a IHost associated system's is enabled and not a local one | ||
398 | * @param host | ||
399 | * @return | ||
400 | */ | ||
401 | public static boolean isHostViable(IHost host) { | ||
402 | IRSESystemType sysType = host.getSystemType(); | ||
403 | if (sysType != null && sysType.isEnabled() && !sysType.isLocal()) | ||
404 | return true; | ||
405 | return false; | ||
406 | } | ||
407 | |||
408 | /** | ||
409 | * Ensures that RSECorePlugin is initialized before performing any actions | ||
410 | */ | ||
411 | public static void waitForRSEInitCompletition() { | ||
412 | if (!RSECorePlugin.isInitComplete(RSECorePlugin.INIT_MODEL)) | ||
413 | try { | ||
414 | RSECorePlugin.waitForInitCompletion(RSECorePlugin.INIT_MODEL); | ||
415 | } catch (InterruptedException e) { | ||
416 | return; | ||
417 | } | ||
418 | } | ||
419 | public static RemoteMachine getRemoteMachine(IHost connection){ | ||
420 | if (!getMachines().containsKey(connection)) | ||
421 | getMachines().put(connection, new RemoteMachine(connection)); | ||
422 | return getMachines().get(connection); | ||
423 | } | ||
424 | |||
425 | private static Map<IHost, RemoteMachine> getMachines() { | ||
426 | if (machines == null) | ||
427 | machines = new HashMap<IHost, RemoteMachine>(); | ||
428 | return machines; | ||
429 | } | ||
430 | |||
431 | public static MessageConsole getConsole(IHost connection) { | ||
432 | return getRemoteMachine(connection).getConsole(); | ||
433 | } | ||
434 | |||
435 | public static CommandResponseHandler getCommandHandler(IHost connection) { | ||
436 | return getRemoteMachine(connection).getCmdHandler(); | ||
437 | } | ||
438 | |||
439 | public static ProcessStreamBuffer processOutput(IProgressMonitor monitor, IHostShell hostShell, CommandResponseHandler cmdHandler) throws Exception { | ||
440 | return new CommandOutputProcessor(monitor, hostShell, cmdHandler, "").processOutput(); | ||
441 | } | ||
442 | |||
443 | public static IHost getRemoteConnectionForURI(URI uri, IProgressMonitor monitor) { | ||
444 | if (uri == null) | ||
445 | return null; | ||
446 | |||
447 | String host = uri.getHost(); | ||
448 | if (host == null) { | ||
449 | // this is a local connection | ||
450 | ISystemRegistry sr = RSECorePlugin.getTheSystemRegistry(); | ||
451 | IHost local = null; | ||
452 | while (local == null) { | ||
453 | local = sr.getLocalHost(); | ||
454 | } | ||
455 | return local; | ||
456 | } | ||
457 | ISystemRegistry sr = RSECorePlugin.getTheSystemRegistry(); | ||
458 | IHost[] connections = sr.getHosts(); | ||
459 | |||
460 | IHost unconnected = null; | ||
461 | for (IHost conn : connections) { | ||
462 | if (host.equalsIgnoreCase(conn.getHostName())) { | ||
463 | IRemoteFileSubSystem fss = getRemoteFileSubSystem(conn); | ||
464 | if (fss != null && fss.isConnected()) | ||
465 | return conn; | ||
466 | unconnected = conn; | ||
467 | } | ||
468 | } | ||
469 | |||
470 | return unconnected; | ||
471 | } | ||
472 | |||
473 | public static IRemoteFileSubSystem getRemoteFileSubSystem(IHost host) { | ||
474 | IRemoteFileSubSystem candidate = null; | ||
475 | IRemoteFileSubSystem otherServiceCandidate = null; | ||
476 | IRemoteFileSubSystem[] subSystems = RemoteFileUtility.getFileSubSystems(host); | ||
477 | |||
478 | for (IRemoteFileSubSystem subSystem : subSystems) { | ||
479 | if (subSystem instanceof FileServiceSubSystem) { | ||
480 | if (subSystem.isConnected()) | ||
481 | return subSystem; | ||
482 | |||
483 | if (otherServiceCandidate == null) | ||
484 | otherServiceCandidate = subSystem; | ||
485 | |||
486 | } else if (candidate == null || (subSystem.isConnected() && !candidate.isConnected())) | ||
487 | candidate = subSystem; | ||
488 | |||
489 | } | ||
490 | if (candidate != null && candidate.isConnected()) | ||
491 | return candidate; | ||
492 | if (otherServiceCandidate != null) | ||
493 | return otherServiceCandidate; | ||
494 | return null; | ||
495 | } | ||
496 | |||
497 | public static IFileService getConnectedRemoteFileService(IHost connection, IProgressMonitor monitor) throws Exception { | ||
498 | return getRemoteMachine(connection).getRemoteFileService(monitor); | ||
499 | } | ||
500 | |||
501 | public static IHostFile[] getRemoteDirContent(IHost connection, String remoteParent, String fileFilter, int fileType, IProgressMonitor monitor){ | ||
502 | |||
503 | try { | ||
504 | IFileService fileServ = getConnectedRemoteFileService(connection, monitor); | ||
505 | return fileServ.list(remoteParent, fileFilter, fileType, monitor); | ||
506 | } catch (SystemMessageException e) { | ||
507 | e.printStackTrace(); | ||
508 | } catch (Exception e) { | ||
509 | e.printStackTrace(); | ||
510 | } | ||
511 | return null; | ||
512 | } | ||
513 | |||
514 | public static IService getConnectedShellService(IHost connection, IProgressMonitor monitor) throws Exception { | ||
515 | return getRemoteMachine(connection).getShellService(monitor); | ||
516 | } | ||
517 | |||
518 | public static void handleRunCommandRemote(IHost connection, YoctoCommand cmd, IProgressMonitor monitor){ | ||
519 | try { | ||
520 | CommandRunnable cmdRun = new CommandRunnable(connection, cmd, monitor); | ||
521 | cmdRun.run(); | ||
522 | } catch (Exception e) { | ||
523 | e.printStackTrace(); | ||
524 | } | ||
525 | } | ||
526 | |||
527 | public static IHostShell runCommandRemote(IHost connection, YoctoCommand cmd, | ||
528 | IProgressMonitor monitor) throws CoreException { | ||
529 | |||
530 | monitor.beginTask(NLS.bind(Messages.RemoteShellExec_1, | ||
531 | cmd, cmd.getArguments()), 10); | ||
532 | |||
533 | String remoteCommand = cmd.getCommand() + " " + cmd.getArguments() + " ; echo " + TERMINATOR + "; exit ;"; | ||
534 | |||
535 | IShellService shellService; | ||
536 | try { | ||
537 | shellService = (IShellService) getConnectedShellService(connection, new SubProgressMonitor(monitor, 7)); | ||
538 | |||
539 | String env[] = getRemoteMachine(connection).prepareEnvString(monitor); | ||
540 | |||
541 | try { | ||
542 | IHostShell hostShell = shellService.runCommand(cmd.getInitialDirectory(), remoteCommand, env, new SubProgressMonitor(monitor, 3)); | ||
543 | return hostShell; | ||
544 | } catch (Exception e) { | ||
545 | e.printStackTrace(); | ||
546 | } | ||
547 | } catch (Exception e1) { | ||
548 | e1.printStackTrace(); | ||
549 | } | ||
550 | return null; | ||
551 | } | ||
552 | |||
553 | public static IHostFile getRemoteHostFile(IHost connection, String remoteFilePath, IProgressMonitor monitor){ | ||
554 | assert(connection != null); | ||
555 | monitor.beginTask(Messages.InfoDownload, 100); | ||
556 | |||
557 | try { | ||
558 | IFileService fileService = getConnectedRemoteFileService(connection, new SubProgressMonitor(monitor, 10)); | ||
559 | Path remotePath = new Path(remoteFilePath); | ||
560 | IHostFile remoteFile = fileService.getFile(remotePath.removeLastSegments(1).toString(), remotePath.lastSegment(), new SubProgressMonitor(monitor, 5)); | ||
561 | return remoteFile; | ||
562 | } catch (Exception e) { | ||
563 | e.printStackTrace(); | ||
564 | }finally { | ||
565 | monitor.done(); | ||
566 | } | ||
567 | return null; | ||
568 | } | ||
569 | |||
570 | public static InputStream getRemoteInputStream(IHost connection, String parentPath, String remoteFilePath, IProgressMonitor monitor){ | ||
571 | assert(connection != null); | ||
572 | monitor.beginTask(Messages.InfoDownload, 100); | ||
573 | |||
574 | try { | ||
575 | IFileService fileService = getConnectedRemoteFileService(connection, new SubProgressMonitor(monitor, 10)); | ||
576 | |||
577 | return fileService.getInputStream(parentPath, remoteFilePath, false, monitor); | ||
578 | } catch (Exception e) { | ||
579 | e.printStackTrace(); | ||
580 | }finally { | ||
581 | monitor.done(); | ||
582 | } | ||
583 | return null; | ||
584 | } | ||
585 | |||
586 | public static URI createNewURI(URI oldURI, String name) { | ||
587 | try { | ||
588 | String sep = oldURI.getPath().endsWith("/") ? "" : "/"; | ||
589 | return new URI(oldURI.getScheme(), oldURI.getHost(), oldURI.getPath() + sep + name, oldURI.getFragment()); | ||
590 | } catch (URISyntaxException e) { | ||
591 | e.printStackTrace(); | ||
592 | return null; | ||
593 | } | ||
594 | } | ||
595 | |||
596 | public static boolean fileExistsRemote(IHost conn, IProgressMonitor monitor, String path) { | ||
597 | try { | ||
598 | IFileService fs = getConnectedRemoteFileService(conn, monitor); | ||
599 | int nameStart = path.lastIndexOf("/"); | ||
600 | String parentPath = path.substring(0, nameStart); | ||
601 | String name = path.substring(nameStart + 1); | ||
602 | IHostFile hostFile = fs.getFile(parentPath, name, monitor); | ||
603 | |||
604 | return hostFile.exists(); | ||
605 | } catch (Exception e) { | ||
606 | e.printStackTrace(); | ||
607 | } | ||
608 | return false; | ||
609 | } | ||
610 | |||
611 | public static IHost createLocalConnection() { | ||
612 | IHost createdHost = null; | ||
613 | try { | ||
614 | |||
615 | ISubSystemConfigurator[] configurators = getSubSystemConfigurators(); | ||
616 | |||
617 | IRSESystemType systemType = getSSHSystemType(); | ||
618 | ISystemRegistry sr = RSECorePlugin.getTheSystemRegistry(); | ||
619 | IHost[] connections = sr.getHosts(); | ||
620 | for (IHost conn : connections) { | ||
621 | if (conn.getName().equals(LOCAL_CONN_NAME)) { | ||
622 | ISystemHostPool pool = conn.getHostPool(); | ||
623 | createdHost = sr.createHost(pool.getSystemProfile().getName(), systemType, CONNECTION_NAME, HOST_NAME, "", "",IRSEUserIdConstants.USERID_LOCATION_NOTSET,configurators); | ||
624 | break; | ||
625 | } | ||
626 | } | ||
627 | |||
628 | // a tweak that is the result of UCD feedback. Phil | ||
629 | if ((createdHost != null) && SystemPerspectiveHelpers.isRSEPerspectiveActive()) { | ||
630 | if (systemType.getId().equals(IRSESystemType.SYSTEMTYPE_ISERIES_ID)) { | ||
631 | ISubSystem[] objSubSystems = sr.getSubSystemsBySubSystemConfigurationCategory("nativefiles", createdHost); //$NON-NLS-1$ | ||
632 | if ((objSubSystems != null) && (objSubSystems.length > 0))// might be in product that doesn't have iSeries plugins | ||
633 | RSEUIPlugin.getTheSystemRegistryUI().expandSubSystem(objSubSystems[0]); | ||
634 | else | ||
635 | RSEUIPlugin.getTheSystemRegistryUI().expandHost(createdHost); | ||
636 | } else | ||
637 | RSEUIPlugin.getTheSystemRegistryUI().expandHost(createdHost); | ||
638 | } | ||
639 | } catch (Exception exc) { | ||
640 | exc.printStackTrace(); | ||
641 | } | ||
642 | return createdHost; | ||
643 | } | ||
644 | |||
645 | private static IRSESystemType getSSHSystemType() { | ||
646 | IRSECoreRegistry coreReg = RSECorePlugin.getTheCoreRegistry(); | ||
647 | return coreReg.getSystemTypeById("org.eclipse.rse.systemtype.ssh"); | ||
648 | } | ||
649 | |||
650 | private static ISubSystemConfigurator[] getSubSystemConfigurators() | ||
651 | { | ||
652 | // what kind of subsystems do we have here? | ||
653 | ISystemRegistry sr = RSECorePlugin.getTheSystemRegistry(); | ||
654 | ISubSystemConfiguration[] configurations = sr.getSubSystemConfigurationsBySystemType(getSSHSystemType(), true); | ||
655 | |||
656 | ArrayList configList = new ArrayList(); | ||
657 | for (int i = 0; i < configurations.length; i++){ | ||
658 | ISubSystemConfiguration configuration = configurations[i]; | ||
659 | |||
660 | class DefaultConfigurator implements ISubSystemConfigurator { | ||
661 | private ISubSystemConfiguration _configuration; | ||
662 | public DefaultConfigurator(ISubSystemConfiguration configuration){ | ||
663 | _configuration = configuration; | ||
664 | } | ||
665 | |||
666 | public boolean applyValues(ISubSystem ss) { | ||
667 | return true; | ||
668 | } | ||
669 | |||
670 | public ISubSystemConfiguration getSubSystemConfiguration() { | ||
671 | return _configuration; | ||
672 | } | ||
673 | } | ||
674 | configList.add(new DefaultConfigurator(configuration)); | ||
675 | } | ||
676 | |||
677 | return (ISubSystemConfigurator[])configList.toArray(new ISubSystemConfigurator[configList.size()]); | ||
678 | |||
679 | } | ||
680 | } | ||