PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.jdt.launching.source_3.6.1.v20110803_r371/org/eclipse/jdt/internal/launching/StandardVMRunner.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 412 lines | 271 code | 41 blank | 100 comment | 74 complexity | 7c7730d1643ca53e4c67d5ce2701445b MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2008 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.jdt.internal.launching;
  12. import java.io.File;
  13. import java.util.ArrayList;
  14. import java.util.Date;
  15. import java.util.Iterator;
  16. import java.util.List;
  17. import java.util.Map;
  18. import java.util.Map.Entry;
  19. import org.eclipse.core.runtime.CoreException;
  20. import org.eclipse.core.runtime.IProgressMonitor;
  21. import org.eclipse.core.runtime.NullProgressMonitor;
  22. import org.eclipse.core.runtime.Platform;
  23. import org.eclipse.core.runtime.SubProgressMonitor;
  24. import org.eclipse.debug.core.DebugPlugin;
  25. import org.eclipse.debug.core.ILaunch;
  26. import org.eclipse.debug.core.model.IProcess;
  27. import org.eclipse.jdt.launching.AbstractVMRunner;
  28. import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
  29. import org.eclipse.jdt.launching.IVMInstall;
  30. import org.eclipse.jdt.launching.IVMInstall2;
  31. import org.eclipse.jdt.launching.VMRunnerConfiguration;
  32. import com.ibm.icu.text.DateFormat;
  33. import com.ibm.icu.text.MessageFormat;
  34. /**
  35. * A launcher for running Java main classes.
  36. */
  37. public class StandardVMRunner extends AbstractVMRunner {
  38. /**
  39. * The VM install instance
  40. */
  41. protected IVMInstall fVMInstance;
  42. /**
  43. * Constructor
  44. * @param vmInstance
  45. */
  46. public StandardVMRunner(IVMInstall vmInstance) {
  47. fVMInstance= vmInstance;
  48. }
  49. /**
  50. * Returns the 'rendered' name for the current target
  51. * @param classToRun
  52. * @param host
  53. * @return the name for the current target
  54. */
  55. protected String renderDebugTarget(String classToRun, int host) {
  56. String format= LaunchingMessages.StandardVMRunner__0__at_localhost__1__1;
  57. return MessageFormat.format(format, new String[] { classToRun, String.valueOf(host) });
  58. }
  59. /**
  60. * Returns the 'rendered' name for the specified command line
  61. * @param commandLine
  62. * @return the name for the process
  63. */
  64. public static String renderProcessLabel(String[] commandLine) {
  65. String format= LaunchingMessages.StandardVMRunner__0____1___2;
  66. String timestamp= DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(new Date(System.currentTimeMillis()));
  67. return MessageFormat.format(format, new String[] { commandLine[0], timestamp });
  68. }
  69. /**
  70. * Prepares the command line from the specified array of strings
  71. * @param commandLine
  72. * @return
  73. */
  74. protected String renderCommandLine(String[] commandLine) {
  75. if (commandLine.length < 1)
  76. return ""; //$NON-NLS-1$
  77. StringBuffer buf= new StringBuffer();
  78. for (int i= 0; i < commandLine.length; i++) {
  79. buf.append(' ');
  80. char[] characters= commandLine[i].toCharArray();
  81. StringBuffer command= new StringBuffer();
  82. boolean containsSpace= false;
  83. for (int j = 0; j < characters.length; j++) {
  84. char character= characters[j];
  85. if (character == '\"') {
  86. command.append('\\');
  87. } else if (character == ' ') {
  88. containsSpace = true;
  89. }
  90. command.append(character);
  91. }
  92. if (containsSpace) {
  93. buf.append('\"');
  94. buf.append(command.toString());
  95. buf.append('\"');
  96. } else {
  97. buf.append(command.toString());
  98. }
  99. }
  100. return buf.toString();
  101. }
  102. /**
  103. * Adds the values of args to the given list v
  104. * @param args
  105. * @param v
  106. */
  107. protected void addArguments(String[] args, List v) {
  108. if (args == null) {
  109. return;
  110. }
  111. for (int i= 0; i < args.length; i++) {
  112. v.add(args[i]);
  113. }
  114. }
  115. /**
  116. * Returns the working directory to use for the launched VM,
  117. * or <code>null</code> if the working directory is to be inherited
  118. * from the current process.
  119. *
  120. * @return the working directory to use
  121. * @exception CoreException if the working directory specified by
  122. * the configuration does not exist or is not a directory
  123. */
  124. protected File getWorkingDir(VMRunnerConfiguration config) throws CoreException {
  125. String path = config.getWorkingDirectory();
  126. if (path == null) {
  127. return null;
  128. }
  129. File dir = new File(path);
  130. if (!dir.isDirectory()) {
  131. abort(MessageFormat.format(LaunchingMessages.StandardVMRunner_Specified_working_directory_does_not_exist_or_is_not_a_directory___0__3, new String[] {path}), null, IJavaLaunchConfigurationConstants.ERR_WORKING_DIRECTORY_DOES_NOT_EXIST);
  132. }
  133. return dir;
  134. }
  135. /**
  136. * @see VMRunner#getPluginIdentifier()
  137. */
  138. protected String getPluginIdentifier() {
  139. return LaunchingPlugin.getUniqueIdentifier();
  140. }
  141. /**
  142. * Construct and return a String containing the full path of a java executable
  143. * command such as 'java' or 'javaw.exe'. If the configuration specifies an
  144. * explicit executable, that is used.
  145. *
  146. * @return full path to java executable
  147. * @exception CoreException if unable to locate an executable
  148. */
  149. protected String constructProgramString(VMRunnerConfiguration config) throws CoreException {
  150. // Look for the user-specified java executable command
  151. String command= null;
  152. Map map= config.getVMSpecificAttributesMap();
  153. if (map != null) {
  154. command = (String)map.get(IJavaLaunchConfigurationConstants.ATTR_JAVA_COMMAND);
  155. }
  156. // If no java command was specified, use default executable
  157. if (command == null) {
  158. File exe = null;
  159. if (fVMInstance instanceof StandardVM) {
  160. exe = ((StandardVM)fVMInstance).getJavaExecutable();
  161. } else {
  162. exe = StandardVMType.findJavaExecutable(fVMInstance.getInstallLocation());
  163. }
  164. if (exe == null) {
  165. abort(MessageFormat.format(LaunchingMessages.StandardVMRunner_Unable_to_locate_executable_for__0__1, new String[]{fVMInstance.getName()}), null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
  166. } else {
  167. return exe.getAbsolutePath();
  168. }
  169. }
  170. // Build the path to the java executable. First try 'bin', and if that
  171. // doesn't exist, try 'jre/bin'
  172. String installLocation = fVMInstance.getInstallLocation().getAbsolutePath() + File.separatorChar;
  173. File exe = new File(installLocation + "bin" + File.separatorChar + command); //$NON-NLS-1$
  174. if (fileExists(exe)){
  175. return exe.getAbsolutePath();
  176. }
  177. exe = new File(exe.getAbsolutePath() + ".exe"); //$NON-NLS-1$
  178. if (fileExists(exe)){
  179. return exe.getAbsolutePath();
  180. }
  181. exe = new File(installLocation + "jre" + File.separatorChar + "bin" + File.separatorChar + command); //$NON-NLS-1$ //$NON-NLS-2$
  182. if (fileExists(exe)) {
  183. return exe.getAbsolutePath();
  184. }
  185. exe = new File(exe.getAbsolutePath() + ".exe"); //$NON-NLS-1$
  186. if (fileExists(exe)) {
  187. return exe.getAbsolutePath();
  188. }
  189. // not found
  190. abort(MessageFormat.format(LaunchingMessages.StandardVMRunner_Specified_executable__0__does_not_exist_for__1__4, new String[]{command, fVMInstance.getName()}), null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
  191. // NOTE: an exception will be thrown - null cannot be returned
  192. return null;
  193. }
  194. /**
  195. * Convenience method to determine if the specified file exists or not
  196. * @param file
  197. * @return true if the file indeed exists, false otherwise
  198. */
  199. protected boolean fileExists(File file) {
  200. return file.exists() && file.isFile();
  201. }
  202. protected String convertClassPath(String[] cp) {
  203. int pathCount= 0;
  204. StringBuffer buf= new StringBuffer();
  205. if (cp.length == 0) {
  206. return ""; //$NON-NLS-1$
  207. }
  208. for (int i= 0; i < cp.length; i++) {
  209. if (pathCount > 0) {
  210. buf.append(File.pathSeparator);
  211. }
  212. buf.append(cp[i]);
  213. pathCount++;
  214. }
  215. return buf.toString();
  216. }
  217. /**
  218. * This method is used to ensure that the JVM file encoding matches that of the console preference for file encoding.
  219. * If the user explicitly declares a file encoding in the launch configuration, then that file encoding is used.
  220. * @param vmargs the original listing of JVM arguments
  221. * @return the listing of JVM arguments including file encoding if one was not specified
  222. *
  223. * @since 3.4
  224. */
  225. protected String[] ensureEncoding(ILaunch launch, String[] vmargs) {
  226. boolean foundencoding = false;
  227. for(int i = 0; i < vmargs.length; i++) {
  228. if(vmargs[i].startsWith("-Dfile.encoding=")) { //$NON-NLS-1$
  229. foundencoding = true;
  230. }
  231. }
  232. if(!foundencoding) {
  233. String encoding = launch.getAttribute(DebugPlugin.ATTR_CONSOLE_ENCODING);
  234. if(encoding == null) {
  235. return vmargs;
  236. }
  237. String[] newargs = new String[vmargs.length+1];
  238. System.arraycopy(vmargs, 0, newargs, 0, vmargs.length);
  239. newargs[newargs.length-1] = "-Dfile.encoding="+encoding; //$NON-NLS-1$
  240. return newargs;
  241. }
  242. return vmargs;
  243. }
  244. /* (non-Javadoc)
  245. * @see org.eclipse.jdt.launching.IVMRunner#run(org.eclipse.jdt.launching.VMRunnerConfiguration, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
  246. */
  247. public void run(VMRunnerConfiguration config, ILaunch launch, IProgressMonitor monitor) throws CoreException {
  248. if (monitor == null) {
  249. monitor = new NullProgressMonitor();
  250. }
  251. IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 1);
  252. subMonitor.beginTask(LaunchingMessages.StandardVMRunner_Launching_VM____1, 2);
  253. subMonitor.subTask(LaunchingMessages.StandardVMRunner_Constructing_command_line____2);
  254. String program= constructProgramString(config);
  255. List arguments= new ArrayList();
  256. arguments.add(program);
  257. // VM args are the first thing after the java program so that users can specify
  258. // options like '-client' & '-server' which are required to be the first option
  259. String[] allVMArgs = combineVmArgs(config, fVMInstance);
  260. addArguments(ensureEncoding(launch, allVMArgs), arguments);
  261. addBootClassPathArguments(arguments, config);
  262. String[] cp= config.getClassPath();
  263. if (cp.length > 0) {
  264. arguments.add("-classpath"); //$NON-NLS-1$
  265. arguments.add(convertClassPath(cp));
  266. }
  267. arguments.add(config.getClassToLaunch());
  268. String[] programArgs= config.getProgramArguments();
  269. addArguments(programArgs, arguments);
  270. String[] cmdLine= new String[arguments.size()];
  271. arguments.toArray(cmdLine);
  272. String[] envp = prependJREPath(config.getEnvironment());
  273. subMonitor.worked(1);
  274. // check for cancellation
  275. if (monitor.isCanceled()) {
  276. return;
  277. }
  278. subMonitor.subTask(LaunchingMessages.StandardVMRunner_Starting_virtual_machine____3);
  279. Process p= null;
  280. File workingDir = getWorkingDir(config);
  281. p= exec(cmdLine, workingDir, envp);
  282. if (p == null) {
  283. return;
  284. }
  285. // check for cancellation
  286. if (monitor.isCanceled()) {
  287. p.destroy();
  288. return;
  289. }
  290. IProcess process= newProcess(launch, p, renderProcessLabel(cmdLine), getDefaultProcessMap());
  291. process.setAttribute(IProcess.ATTR_CMDLINE, renderCommandLine(cmdLine));
  292. subMonitor.worked(1);
  293. subMonitor.done();
  294. }
  295. /**
  296. * Prepends the correct java version variable state to the environment path for Mac VMs
  297. *
  298. * @param env the current array of environment variables to run with
  299. * @param jdkpath the path of the current jdk
  300. * @since 3.3
  301. */
  302. protected String[] prependJREPath(String[] env) {
  303. if (Platform.OS_MACOSX.equals(Platform.getOS())) {
  304. if (fVMInstance instanceof IVMInstall2) {
  305. IVMInstall2 vm = (IVMInstall2) fVMInstance;
  306. String javaVersion = vm.getJavaVersion();
  307. if (javaVersion != null) {
  308. if (env == null) {
  309. Map map = DebugPlugin.getDefault().getLaunchManager().getNativeEnvironmentCasePreserved();
  310. if (map.containsKey(StandardVMDebugger.JAVA_JVM_VERSION)) {
  311. String[] env2 = new String[map.size()];
  312. Iterator iterator = map.entrySet().iterator();
  313. int i = 0;
  314. while (iterator.hasNext()) {
  315. Entry entry = (Entry) iterator.next();
  316. String key = (String) entry.getKey();
  317. if (StandardVMDebugger.JAVA_JVM_VERSION.equals(key)) {
  318. env2[i] = key + "=" + javaVersion; //$NON-NLS-1$
  319. } else {
  320. env2[i] = key + "=" + (String)entry.getValue(); //$NON-NLS-1$
  321. }
  322. i++;
  323. }
  324. env = env2;
  325. }
  326. } else {
  327. for (int i = 0; i < env.length; i++) {
  328. String string = env[i];
  329. if (string.startsWith(StandardVMDebugger.JAVA_JVM_VERSION)) {
  330. env[i]=StandardVMDebugger.JAVA_JVM_VERSION+"="+javaVersion; //$NON-NLS-1$
  331. break;
  332. }
  333. }
  334. }
  335. }
  336. }
  337. }
  338. return env;
  339. }
  340. /**
  341. * Adds arguments to the bootpath
  342. * @param arguments
  343. * @param config
  344. */
  345. protected void addBootClassPathArguments(List arguments, VMRunnerConfiguration config) {
  346. String[] prependBootCP= null;
  347. String[] bootCP= null;
  348. String[] appendBootCP= null;
  349. Map map = config.getVMSpecificAttributesMap();
  350. if (map != null) {
  351. prependBootCP= (String[]) map.get(IJavaLaunchConfigurationConstants.ATTR_BOOTPATH_PREPEND);
  352. bootCP= (String[]) map.get(IJavaLaunchConfigurationConstants.ATTR_BOOTPATH);
  353. appendBootCP= (String[]) map.get(IJavaLaunchConfigurationConstants.ATTR_BOOTPATH_APPEND);
  354. }
  355. if (prependBootCP == null && bootCP == null && appendBootCP == null) {
  356. // use old single attribute instead of new attributes if not specified
  357. bootCP = config.getBootClassPath();
  358. }
  359. if (prependBootCP != null) {
  360. arguments.add("-Xbootclasspath/p:" + convertClassPath(prependBootCP)); //$NON-NLS-1$
  361. }
  362. if (bootCP != null) {
  363. if (bootCP.length > 0) {
  364. arguments.add("-Xbootclasspath:" + convertClassPath(bootCP)); //$NON-NLS-1$
  365. }
  366. }
  367. if (appendBootCP != null) {
  368. arguments.add("-Xbootclasspath/a:" + convertClassPath(appendBootCP)); //$NON-NLS-1$
  369. }
  370. }
  371. }