~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to profiling/org.eclipse.linuxtools.ssh.proxy/src/org/eclipse/linuxtools/internal/ssh/proxy/SSHCommandLauncher.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2012 IBM Corporation and others.
 
3
 * All rights reserved. This program and the accompanying materials
 
4
 * are made available under the terms of the Eclipse Public License v1.0
 
5
 * which accompanies this distribution, and is available at
 
6
 * http://www.eclipse.org/legal/epl-v10.html
 
7
 *
 
8
 * Contributors:
 
9
 *     IBM Corporation - initial API and implementation
 
10
 *******************************************************************************/
 
11
package org.eclipse.linuxtools.internal.ssh.proxy;
 
12
 
 
13
import java.io.OutputStream;
 
14
import java.net.URI;
 
15
 
 
16
import org.eclipse.core.runtime.CoreException;
 
17
import org.eclipse.core.runtime.IPath;
 
18
import org.eclipse.core.runtime.IProgressMonitor;
 
19
import org.eclipse.core.runtime.Status;
 
20
import org.eclipse.linuxtools.profiling.launch.IRemoteCommandLauncher;
 
21
 
 
22
import org.eclipse.linuxtools.ssh.proxy.Activator;
 
23
 
 
24
import com.jcraft.jsch.ChannelExec;
 
25
 
 
26
/**
 
27
 * @noextend This class is not intended to be subclassed by clients.
 
28
 */
 
29
public class SSHCommandLauncher extends SSHBase implements IRemoteCommandLauncher {
 
30
        private String errorMessage;
 
31
        private SSHProcess fProcess;
 
32
 
 
33
        /**
 
34
         * Creates a new launcher Fills in stderr and stdout output to the given
 
35
         * streams. Streams can be set to <code>null</code>, if output not
 
36
         * required
 
37
         */
 
38
        public SSHCommandLauncher(URI uri) {
 
39
                super(uri);
 
40
        }
 
41
 
 
42
        @Override
 
43
        public Process execute(IPath commandPath, String[] args, String[] env,
 
44
                        IPath changeToDirectory, IProgressMonitor monitor)
 
45
                        throws CoreException {
 
46
                StringBuilder cmd = new StringBuilder();
 
47
 
 
48
                if (changeToDirectory != null)
 
49
                        cmd.append("cd " + changeToDirectory.toString() + "; "); //$NON-NLS-1$ //$NON-NLS-2$
 
50
 
 
51
                cmd.append(commandPath.toString());
 
52
                cmd.append(" "); //$NON-NLS-1$
 
53
                for (String s : args) {
 
54
                        cmd.append(s);
 
55
                        cmd.append(" "); //$NON-NLS-1$
 
56
                }
 
57
 
 
58
                try{
 
59
                        ChannelExec channel = createChannelExec();
 
60
 
 
61
                        for (String s : env) {
 
62
                                String[] tokens = s.split("=", 2); //$NON-NLS-1$
 
63
                                switch (tokens.length) {
 
64
                                        case 1:
 
65
                                                channel.setEnv(tokens[0], null);
 
66
                                                break;
 
67
                                        case 2:
 
68
                                                channel.setEnv(tokens[0], tokens[1]);
 
69
                                                break;
 
70
                                        default:
 
71
                                                Activator.log(Status.WARNING, Messages.SSHCommandLauncher_malformed_env_var_string + s);
 
72
                                        }
 
73
                        }
 
74
 
 
75
                        channel.setCommand(cmd.toString());
 
76
                        channel.connect();
 
77
                        fProcess = new SSHProcess(channel);
 
78
                        return fProcess;
 
79
                } catch (Exception e) {
 
80
                        errorMessage = e.getMessage();
 
81
                        e.printStackTrace();
 
82
                }
 
83
                return null;
 
84
        }
 
85
 
 
86
        @Override
 
87
        public int waitAndRead(OutputStream output, OutputStream err, IProgressMonitor monitor) {
 
88
                if (fProcess == null)
 
89
                        return IRemoteCommandLauncher.ILLEGAL_COMMAND;
 
90
 
 
91
                return fProcess.waitAndRead(output, err, monitor);
 
92
        }
 
93
 
 
94
        /* (non-Javadoc)
 
95
         * @see org.eclipse.cdt.core.ICommandLauncher#getErrorMessage()
 
96
         */
 
97
        @Override
 
98
        public String getErrorMessage() {
 
99
                return errorMessage;
 
100
        }
 
101
}