|
Lines 12-26
Link Here
|
| 12 |
package org.eclipse.linuxtools.tools.launch.core.factory; |
12 |
package org.eclipse.linuxtools.tools.launch.core.factory; |
| 13 |
|
13 |
|
| 14 |
import java.io.BufferedReader; |
14 |
import java.io.BufferedReader; |
| 15 |
import java.io.File; |
|
|
| 16 |
import java.io.IOException; |
15 |
import java.io.IOException; |
| 17 |
import java.io.InputStreamReader; |
16 |
import java.io.InputStreamReader; |
|
|
17 |
import java.net.URI; |
| 18 |
import java.util.ArrayList; |
18 |
import java.util.ArrayList; |
| 19 |
import java.util.Arrays; |
19 |
import java.util.Arrays; |
| 20 |
import java.util.List; |
20 |
import java.util.List; |
| 21 |
import java.util.StringTokenizer; |
21 |
import java.util.StringTokenizer; |
| 22 |
|
22 |
|
|
|
23 |
import org.eclipse.core.filesystem.IFileStore; |
| 23 |
import org.eclipse.core.resources.IProject; |
24 |
import org.eclipse.core.resources.IProject; |
|
|
25 |
import org.eclipse.core.runtime.CoreException; |
| 26 |
import org.eclipse.core.runtime.IPath; |
| 27 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 28 |
import org.eclipse.core.runtime.Path; |
| 29 |
import org.eclipse.linuxtools.profiling.launch.IRemoteCommandLauncher; |
| 30 |
import org.eclipse.linuxtools.profiling.launch.IRemoteFileProxy; |
| 31 |
import org.eclipse.linuxtools.profiling.launch.RemoteProxyManager; |
| 24 |
|
32 |
|
| 25 |
/* |
33 |
/* |
| 26 |
* Create process using Runtime.getRuntime().exec and prepends the |
34 |
* Create process using Runtime.getRuntime().exec and prepends the |
|
Lines 32-37
import org.eclipse.core.resources.IProject;
Link Here
|
| 32 |
public class RuntimeProcessFactory extends LinuxtoolsProcessFactory { |
40 |
public class RuntimeProcessFactory extends LinuxtoolsProcessFactory { |
| 33 |
private static RuntimeProcessFactory instance = null; |
41 |
private static RuntimeProcessFactory instance = null; |
| 34 |
private static final String WHICH_CMD = "which"; //$NON-NLS-1$ |
42 |
private static final String WHICH_CMD = "which"; //$NON-NLS-1$ |
|
|
43 |
|
| 44 |
private IRemoteFileProxy proxy; |
| 45 |
|
| 35 |
|
46 |
|
| 36 |
private String[] tokenizeCommand(String command) { |
47 |
private String[] tokenizeCommand(String command) { |
| 37 |
StringTokenizer tokenizer = new StringTokenizer(command); |
48 |
StringTokenizer tokenizer = new StringTokenizer(command); |
|
Lines 42-72
public class RuntimeProcessFactory extends LinuxtoolsProcessFactory {
Link Here
|
| 42 |
return cmdarray; |
53 |
return cmdarray; |
| 43 |
} |
54 |
} |
| 44 |
|
55 |
|
| 45 |
private String[] fillPathCommand(String[] cmdarray, String[] envp) throws IOException { |
56 |
private String[] fillPathCommand(String[] cmdarray, IProject project) throws IOException { |
| 46 |
cmdarray[0] = whichCommand(cmdarray[0], envp); |
57 |
cmdarray[0] = whichCommand(cmdarray[0], project); |
| 47 |
return cmdarray; |
58 |
return cmdarray; |
| 48 |
} |
59 |
} |
| 49 |
|
60 |
|
| 50 |
private String[] fillPathSudoCommand(String[] cmdarray, String[] envp) throws IOException { |
61 |
private String[] fillPathSudoCommand(String[] cmdarray, IProject project) throws IOException { |
| 51 |
cmdarray[2] = whichCommand(cmdarray[2], envp); |
62 |
cmdarray[1] = whichCommand(cmdarray[1], project); |
| 52 |
return cmdarray; |
63 |
return cmdarray; |
| 53 |
} |
64 |
} |
| 54 |
|
65 |
|
| 55 |
public String whichCommand(String command, IProject project) throws IOException { |
66 |
public String whichCommand(String command, IProject project) throws IOException { |
| 56 |
return whichCommand(command, updateEnvironment(null, project)); |
67 |
|
| 57 |
} |
68 |
String[] envp = updateEnvironment(null, project); |
| 58 |
|
69 |
|
| 59 |
public String whichCommand(String command, String[] envp) throws IOException { |
|
|
| 60 |
Process p = Runtime.getRuntime().exec(new String[] {WHICH_CMD, command}, envp); |
| 61 |
try { |
70 |
try { |
| 62 |
if (p.waitFor() == 0) { |
71 |
proxy = RemoteProxyManager.getInstance().getFileProxy(project); |
| 63 |
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); |
72 |
URI whichUri = URI.create(WHICH_CMD); |
| 64 |
command = reader.readLine(); |
73 |
IPath whichPath = new Path(proxy.toPath(whichUri)); |
| 65 |
} else { |
74 |
IRemoteCommandLauncher launcher = RemoteProxyManager.getInstance().getLauncher(project); |
| 66 |
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream())); |
75 |
Process pProxy = launcher.execute(whichPath, new String[]{command}, envp, null, new NullProgressMonitor()); |
| 67 |
throw new IOException(reader.readLine()); |
76 |
if (pProxy != null){ |
|
|
77 |
BufferedReader error = new BufferedReader(new InputStreamReader(pProxy.getErrorStream())); |
| 78 |
if(error.readLine() != null){ |
| 79 |
throw new IOException(error.readLine()); |
| 80 |
} |
| 81 |
BufferedReader reader = new BufferedReader(new InputStreamReader(pProxy.getInputStream())); |
| 82 |
String readLine = reader.readLine(); |
| 83 |
command = readLine; |
| 68 |
} |
84 |
} |
| 69 |
} catch (InterruptedException e) { |
85 |
} catch (CoreException e) { |
| 70 |
e.printStackTrace(); |
86 |
e.printStackTrace(); |
| 71 |
} |
87 |
} |
| 72 |
return command; |
88 |
return command; |
|
Lines 94-110
public class RuntimeProcessFactory extends LinuxtoolsProcessFactory {
Link Here
|
| 94 |
return exec(cmd, envp, null, project); |
110 |
return exec(cmd, envp, null, project); |
| 95 |
} |
111 |
} |
| 96 |
|
112 |
|
| 97 |
public Process exec(String cmd, String[] envp, File dir, IProject project) |
113 |
public Process exec(String cmd, String[] envp, IFileStore dir, IProject project) |
| 98 |
throws IOException { |
114 |
throws IOException { |
| 99 |
return exec(tokenizeCommand(cmd), envp, dir, project); |
115 |
return exec(tokenizeCommand(cmd), envp, dir, project); |
| 100 |
} |
116 |
} |
| 101 |
|
117 |
|
| 102 |
public Process exec(String cmdarray[], String[] envp, File dir, IProject project) |
118 |
public Process exec(String cmdarray[], String[] envp, IFileStore dir, IProject project) |
| 103 |
throws IOException { |
119 |
throws IOException { |
| 104 |
envp = updateEnvironment(envp, project); |
120 |
|
| 105 |
cmdarray = fillPathCommand(cmdarray, envp); |
121 |
Process p = null; |
|
|
122 |
try { |
| 123 |
cmdarray = fillPathCommand(cmdarray, project); |
| 124 |
|
| 125 |
String command = cmdarray[0]; |
| 126 |
URI uri = URI.create(command); |
| 127 |
|
| 128 |
IPath path = new Path(proxy.toPath(uri)); |
| 129 |
IRemoteCommandLauncher launcher = RemoteProxyManager.getInstance().getLauncher(project); |
| 130 |
envp = updateEnvironment(envp, project); |
| 131 |
|
| 132 |
IPath changeToDir; |
| 133 |
if (dir == null){ |
| 134 |
changeToDir = null; |
| 135 |
} else{ |
| 136 |
changeToDir = new Path(proxy.toPath(dir.toURI())); |
| 137 |
} |
| 138 |
|
| 139 |
List<String> cmdlist = new ArrayList<String>(Arrays.asList(cmdarray)); |
| 140 |
cmdlist.remove(0); |
| 141 |
cmdlist.toArray(cmdarray); |
| 142 |
cmdarray = cmdlist.toArray(new String[0]); |
| 143 |
|
| 144 |
p = launcher.execute(path, cmdarray, envp, changeToDir , new NullProgressMonitor()); |
| 145 |
} catch (CoreException e) { |
| 146 |
// TODO Auto-generated catch block |
| 147 |
e.printStackTrace(); |
| 148 |
} |
| 106 |
|
149 |
|
| 107 |
return Runtime.getRuntime().exec(cmdarray, envp, dir); |
150 |
return p; |
| 108 |
} |
151 |
} |
| 109 |
|
152 |
|
| 110 |
public Process sudoExec(String cmd, IProject project) throws IOException { |
153 |
public Process sudoExec(String cmd, IProject project) throws IOException { |
|
Lines 115-121
public class RuntimeProcessFactory extends LinuxtoolsProcessFactory {
Link Here
|
| 115 |
return exec(cmd, envp, null, project); |
158 |
return exec(cmd, envp, null, project); |
| 116 |
} |
159 |
} |
| 117 |
|
160 |
|
| 118 |
public Process sudoExec(String cmd, String[] envp, File dir, IProject project) |
161 |
public Process sudoExec(String cmd, String[] envp, IFileStore dir, IProject project) |
| 119 |
throws IOException { |
162 |
throws IOException { |
| 120 |
return sudoExec(tokenizeCommand(cmd), envp, dir, project); |
163 |
return sudoExec(tokenizeCommand(cmd), envp, dir, project); |
| 121 |
} |
164 |
} |
|
Lines 128-145
public class RuntimeProcessFactory extends LinuxtoolsProcessFactory {
Link Here
|
| 128 |
return sudoExec(cmdarray, envp, null, project); |
171 |
return sudoExec(cmdarray, envp, null, project); |
| 129 |
} |
172 |
} |
| 130 |
|
173 |
|
| 131 |
public Process sudoExec(String[] cmdarray, String[] envp, File dir, IProject project) throws IOException { |
174 |
public Process sudoExec(String[] cmdarray, String[] envp, IFileStore dir, IProject project) throws IOException { |
|
|
175 |
URI uri = URI.create("sudo"); //$NON-NLS-1$ |
| 176 |
|
| 132 |
List<String> cmdList = Arrays.asList(cmdarray); |
177 |
List<String> cmdList = Arrays.asList(cmdarray); |
| 133 |
ArrayList<String> cmdArrayList = new ArrayList<String>(cmdList); |
178 |
ArrayList<String> cmdArrayList = new ArrayList<String>(cmdList); |
| 134 |
cmdArrayList.add(0, "sudo"); //$NON-NLS-1$ |
179 |
cmdArrayList.add(0, "-n"); //$NON-NLS-1$ |
| 135 |
cmdArrayList.add(1, "-n"); //$NON-NLS-1$ |
|
|
| 136 |
|
180 |
|
| 137 |
String[] cmdArraySudo = new String[cmdArrayList.size()]; |
181 |
String[] cmdArraySudo = new String[cmdArrayList.size()]; |
| 138 |
cmdArrayList.toArray(cmdArraySudo); |
182 |
cmdArrayList.toArray(cmdArraySudo); |
| 139 |
|
183 |
|
| 140 |
envp = updateEnvironment(envp, project); |
184 |
Process p = null; |
| 141 |
cmdArraySudo = fillPathSudoCommand(cmdArraySudo, envp); |
185 |
try { |
|
|
186 |
cmdArraySudo = fillPathSudoCommand(cmdArraySudo, project); |
| 187 |
|
| 188 |
IPath path = new Path(proxy.toPath(uri)); |
| 189 |
IRemoteCommandLauncher launcher = RemoteProxyManager.getInstance().getLauncher(project); |
| 190 |
envp = updateEnvironment(envp, project); |
| 191 |
|
| 192 |
IPath changeToDir; |
| 193 |
if (dir == null){ |
| 194 |
changeToDir = null; |
| 195 |
} else{ |
| 196 |
changeToDir = new Path(proxy.toPath(dir.toURI())); |
| 197 |
} |
| 198 |
|
| 199 |
List<String> cmdlist = new ArrayList<String>(Arrays.asList(cmdArraySudo)); |
| 200 |
cmdlist.remove(0); |
| 201 |
cmdlist.toArray(cmdArraySudo); |
| 202 |
cmdArraySudo = cmdlist.toArray(new String[0]); |
| 203 |
|
| 204 |
p = launcher.execute(path, cmdArraySudo, envp, changeToDir , new NullProgressMonitor()); |
| 205 |
} catch (CoreException e) { |
| 206 |
// TODO Auto-generated catch block |
| 207 |
e.printStackTrace(); |
| 208 |
} |
| 209 |
|
| 210 |
return p; |
| 142 |
|
211 |
|
| 143 |
return Runtime.getRuntime().exec(cmdArraySudo, envp, dir); |
|
|
| 144 |
} |
212 |
} |
| 145 |
} |
213 |
} |
| 146 |
- |
|
|