1 11 package org.eclipse.jdt.internal.launching; 12 13 14 import java.io.File ; 15 import java.util.ArrayList ; 16 import java.util.Date ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Map.Entry; 21 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IPath; 24 import org.eclipse.core.runtime.IProgressMonitor; 25 import org.eclipse.core.runtime.NullProgressMonitor; 26 import org.eclipse.core.runtime.Path; 27 import org.eclipse.core.runtime.Platform; 28 import org.eclipse.core.runtime.SubProgressMonitor; 29 import org.eclipse.debug.core.DebugPlugin; 30 import org.eclipse.debug.core.ILaunch; 31 import org.eclipse.debug.core.model.IProcess; 32 import org.eclipse.jdt.launching.AbstractVMRunner; 33 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 34 import org.eclipse.jdt.launching.IVMInstall; 35 import org.eclipse.jdt.launching.IVMInstall2; 36 import org.eclipse.jdt.launching.VMRunnerConfiguration; 37 38 import com.ibm.icu.text.DateFormat; 39 import com.ibm.icu.text.MessageFormat; 40 41 44 public class StandardVMRunner extends AbstractVMRunner { 45 46 49 protected IVMInstall fVMInstance; 50 51 55 public StandardVMRunner(IVMInstall vmInstance) { 56 fVMInstance= vmInstance; 57 } 58 59 65 protected String renderDebugTarget(String classToRun, int host) { 66 String format= LaunchingMessages.StandardVMRunner__0__at_localhost__1__1; 67 return MessageFormat.format(format, new String [] { classToRun, String.valueOf(host) }); 68 } 69 70 75 public static String renderProcessLabel(String [] commandLine) { 76 String format= LaunchingMessages.StandardVMRunner__0____1___2; 77 String timestamp= DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(new Date (System.currentTimeMillis())); 78 return MessageFormat.format(format, new String [] { commandLine[0], timestamp }); 79 } 80 81 86 protected static String renderCommandLine(String [] commandLine) { 87 if (commandLine.length < 1) 88 return ""; StringBuffer buf= new StringBuffer (); 90 for (int i= 0; i < commandLine.length; i++) { 91 buf.append(' '); 92 char[] characters= commandLine[i].toCharArray(); 93 StringBuffer command= new StringBuffer (); 94 boolean containsSpace= false; 95 for (int j = 0; j < characters.length; j++) { 96 char character= characters[j]; 97 if (character == '\"') { 98 command.append('\\'); 99 } else if (character == ' ') { 100 containsSpace = true; 101 } 102 command.append(character); 103 } 104 if (containsSpace) { 105 buf.append('\"'); 106 buf.append(command.toString()); 107 buf.append('\"'); 108 } else { 109 buf.append(command.toString()); 110 } 111 } 112 return buf.toString(); 113 } 114 115 120 protected void addArguments(String [] args, List v) { 121 if (args == null) { 122 return; 123 } 124 for (int i= 0; i < args.length; i++) { 125 v.add(args[i]); 126 } 127 } 128 129 138 protected File getWorkingDir(VMRunnerConfiguration config) throws CoreException { 139 String path = config.getWorkingDirectory(); 140 if (path == null) { 141 return null; 142 } 143 File dir = new File (path); 144 if (!dir.isDirectory()) { 145 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); 146 } 147 return dir; 148 } 149 150 153 protected String getPluginIdentifier() { 154 return LaunchingPlugin.getUniqueIdentifier(); 155 } 156 157 165 protected String constructProgramString(VMRunnerConfiguration config) throws CoreException { 166 167 String command= null; 169 Map map= config.getVMSpecificAttributesMap(); 170 if (map != null) { 171 command = (String )map.get(IJavaLaunchConfigurationConstants.ATTR_JAVA_COMMAND); 172 } 173 174 if (command == null) { 176 File exe = StandardVMType.findJavaExecutable(fVMInstance.getInstallLocation()); 177 if (exe == null) { 178 abort(MessageFormat.format(LaunchingMessages.StandardVMRunner_Unable_to_locate_executable_for__0__1, new String []{fVMInstance.getName()}), null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR); 179 } 180 return exe.getAbsolutePath(); 181 } 182 183 String installLocation = fVMInstance.getInstallLocation().getAbsolutePath() + File.separatorChar; 186 File exe = new File (installLocation + "bin" + File.separatorChar + command); if (fileExists(exe)){ 188 return exe.getAbsolutePath(); 189 } 190 exe = new File (exe.getAbsolutePath() + ".exe"); if (fileExists(exe)){ 192 return exe.getAbsolutePath(); 193 } 194 exe = new File (installLocation + "jre" + File.separatorChar + "bin" + File.separatorChar + command); if (fileExists(exe)) { 196 return exe.getAbsolutePath(); 197 } 198 exe = new File (exe.getAbsolutePath() + ".exe"); if (fileExists(exe)) { 200 return exe.getAbsolutePath(); 201 } 202 203 204 abort(MessageFormat.format(LaunchingMessages.StandardVMRunner_Specified_executable__0__does_not_exist_for__1__4, new String []{command, fVMInstance.getName()}), null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR); 206 return null; 208 } 209 210 215 protected boolean fileExists(File file) { 216 return file.exists() && file.isFile(); 217 } 218 219 protected String convertClassPath(String [] cp) { 220 int pathCount= 0; 221 StringBuffer buf= new StringBuffer (); 222 if (cp.length == 0) { 223 return ""; } 225 for (int i= 0; i < cp.length; i++) { 226 if (pathCount > 0) { 227 buf.append(File.pathSeparator); 228 } 229 buf.append(cp[i]); 230 pathCount++; 231 } 232 return buf.toString(); 233 } 234 235 236 239 public void run(VMRunnerConfiguration config, ILaunch launch, IProgressMonitor monitor) throws CoreException { 240 241 if (monitor == null) { 242 monitor = new NullProgressMonitor(); 243 } 244 245 IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 1); 246 subMonitor.beginTask(LaunchingMessages.StandardVMRunner_Launching_VM____1, 2); 247 subMonitor.subTask(LaunchingMessages.StandardVMRunner_Constructing_command_line____2); 248 249 String program= constructProgramString(config); 250 251 List arguments= new ArrayList (); 252 arguments.add(program); 253 254 String [] allVMArgs = combineVmArgs(config, fVMInstance); 257 addArguments(allVMArgs, arguments); 258 259 addBootClassPathArguments(arguments, config); 260 261 String [] cp= config.getClassPath(); 262 if (cp.length > 0) { 263 arguments.add("-classpath"); arguments.add(convertClassPath(cp)); 265 } 266 arguments.add(config.getClassToLaunch()); 267 268 String [] programArgs= config.getProgramArguments(); 269 addArguments(programArgs, arguments); 270 271 String [] cmdLine= new String [arguments.size()]; 272 arguments.toArray(cmdLine); 273 274 String [] envp = prependJREPath(config.getEnvironment(), new Path(program)); 275 276 subMonitor.worked(1); 277 278 if (monitor.isCanceled()) { 280 return; 281 } 282 283 subMonitor.subTask(LaunchingMessages.StandardVMRunner_Starting_virtual_machine____3); 284 Process p= null; 285 File workingDir = getWorkingDir(config); 286 p= exec(cmdLine, workingDir, envp); 287 if (p == null) { 288 return; 289 } 290 291 if (monitor.isCanceled()) { 293 p.destroy(); 294 return; 295 } 296 297 IProcess process= newProcess(launch, p, renderProcessLabel(cmdLine), getDefaultProcessMap()); 298 process.setAttribute(IProcess.ATTR_CMDLINE, renderCommandLine(cmdLine)); 299 subMonitor.worked(1); 300 subMonitor.done(); 301 } 302 303 310 protected String [] prependJREPath(String [] env, IPath jdkpath) { 311 if (Platform.OS_MACOSX.equals(Platform.getOS())) { 312 if (fVMInstance instanceof IVMInstall2) { 313 IVMInstall2 vm = (IVMInstall2) fVMInstance; 314 String javaVersion = vm.getJavaVersion(); 315 if (javaVersion != null) { 316 if (env == null) { 317 Map map = DebugPlugin.getDefault().getLaunchManager().getNativeEnvironmentCasePreserved(); 318 if (map.containsKey(StandardVMDebugger.JAVA_JVM_VERSION)) { 319 String [] env2 = new String [map.size()]; 320 Iterator iterator = map.entrySet().iterator(); 321 int i = 0; 322 while (iterator.hasNext()) { 323 Entry entry = (Entry) iterator.next(); 324 String key = (String ) entry.getKey(); 325 if (StandardVMDebugger.JAVA_JVM_VERSION.equals(key)) { 326 env2[i] = key + "=" + javaVersion; } else { 328 env2[i] = key + "=" + (String )entry.getValue(); } 330 i++; 331 } 332 env = env2; 333 } 334 } else { 335 for (int i = 0; i < env.length; i++) { 336 String string = env[i]; 337 if (string.startsWith(StandardVMDebugger.JAVA_JVM_VERSION)) { 338 env[i]=StandardVMDebugger.JAVA_JVM_VERSION+"="+javaVersion; break; 340 } 341 } 342 } 343 } 344 } 345 } 346 return env; 347 } 348 349 354 protected void addBootClassPathArguments(List arguments, VMRunnerConfiguration config) { 355 String [] prependBootCP= null; 356 String [] bootCP= null; 357 String [] appendBootCP= null; 358 Map map = config.getVMSpecificAttributesMap(); 359 if (map != null) { 360 prependBootCP= (String []) map.get(IJavaLaunchConfigurationConstants.ATTR_BOOTPATH_PREPEND); 361 bootCP= (String []) map.get(IJavaLaunchConfigurationConstants.ATTR_BOOTPATH); 362 appendBootCP= (String []) map.get(IJavaLaunchConfigurationConstants.ATTR_BOOTPATH_APPEND); 363 } 364 if (prependBootCP == null && bootCP == null && appendBootCP == null) { 365 bootCP = config.getBootClassPath(); 367 } 368 if (prependBootCP != null) { 369 arguments.add("-Xbootclasspath/p:" + convertClassPath(prependBootCP)); } 371 if (bootCP != null) { 372 if (bootCP.length > 0) { 373 arguments.add("-Xbootclasspath:" + convertClassPath(bootCP)); } 375 } 376 if (appendBootCP != null) { 377 arguments.add("-Xbootclasspath/a:" + convertClassPath(appendBootCP)); } 379 } 380 381 } 382 | Popular Tags |