1 11 package org.eclipse.jdt.internal.ui.text.correction; 12 13 import java.io.BufferedReader ; 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.IOException ; 17 import java.io.InputStreamReader ; 18 import java.util.ArrayList ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 import org.eclipse.core.runtime.Assert; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IProgressMonitor; 25 import org.eclipse.core.runtime.SubProgressMonitor; 26 27 28 import org.eclipse.debug.core.DebugPlugin; 29 import org.eclipse.debug.core.ILaunch; 30 import org.eclipse.debug.core.ILaunchConfiguration; 31 32 import org.eclipse.jdt.internal.corext.util.Messages; 33 34 import org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate; 35 import org.eclipse.jdt.launching.AbstractVMRunner; 36 import org.eclipse.jdt.launching.ExecutionArguments; 37 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 38 import org.eclipse.jdt.launching.IVMInstall; 39 import org.eclipse.jdt.launching.IVMRunner; 40 import org.eclipse.jdt.launching.JavaRuntime; 41 import org.eclipse.jdt.launching.LibraryLocation; 42 import org.eclipse.jdt.launching.VMRunnerConfiguration; 43 44 import org.eclipse.jdt.internal.ui.JavaPlugin; 45 46 52 public final class SerialVersionLaunchConfigurationDelegate extends AbstractJavaLaunchConfigurationDelegate { 53 54 58 public static final int FAILING_ID= 0; 59 60 63 final class SerialVersionRunner extends AbstractVMRunner { 64 65 66 private static final String TEMP_FILE_ENCODING= "utf-8"; 68 69 private static final String TEMP_FILE_NAME= "serials.tmp"; 71 72 private final IVMInstall fInstall; 73 74 80 SerialVersionRunner(final IVMInstall install) { 81 Assert.isNotNull(install); 82 83 fInstall= install; 84 } 85 86 93 private String flattenClassPath(final String [] path) { 94 Assert.isNotNull(path); 95 int count= 0; 96 if (path.length == 0) 97 return ""; final StringBuffer buffer= new StringBuffer (); 99 for (int index= 0; index < path.length; index++) { 100 if (count > 0) 101 buffer.append(File.pathSeparator); 102 buffer.append(path[index]); 103 count++; 104 } 105 return buffer.toString(); 106 } 107 108 117 private String getJavaExecutable(final VMRunnerConfiguration configuration) throws CoreException { 118 Assert.isNotNull(configuration); 119 String command= null; 120 final Map map= configuration.getVMSpecificAttributesMap(); 121 if (map != null) 122 command= (String ) map.get(IJavaLaunchConfigurationConstants.ATTR_JAVA_COMMAND); 123 if (command == null) { 124 final File executable= findJavaExecutable(fInstall.getInstallLocation()); 125 if (executable == null) 126 abort(Messages.format(CorrectionMessages.SerialVersionHashProposal_unable_locate_executable, new String [] { fInstall.getName()}), null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR); 127 return executable.getAbsolutePath(); 128 } 129 final String location= fInstall.getInstallLocation().getAbsolutePath() + File.separatorChar; 130 File executable= new File (location + "bin" + File.separatorChar + command); if (executable.exists() && executable.isFile()) 132 return executable.getAbsolutePath(); 133 executable= new File (executable.getAbsolutePath() + ".exe"); if (executable.exists() && executable.isFile()) 135 return executable.getAbsolutePath(); 136 executable= new File (location + "jre" + File.separatorChar + "bin" + File.separatorChar + command); if (executable.exists() && executable.isFile()) 138 return executable.getAbsolutePath(); 139 executable= new File (executable.getAbsolutePath() + ".exe"); if (executable.exists() && executable.isFile()) 141 return executable.getAbsolutePath(); 142 abort(Messages.format(CorrectionMessages.SerialVersionHashProposal_wrong_executable, new String [] { command, fInstall.getName()}), null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR); 143 return null; 144 } 145 146 149 protected String getPluginIdentifier() { 150 return JavaPlugin.getPluginId(); 151 } 152 153 156 public void run(final VMRunnerConfiguration configuration, final ILaunch launch, final IProgressMonitor monitor) throws CoreException { 157 Assert.isNotNull(configuration); 158 Assert.isNotNull(launch); 159 Assert.isNotNull(monitor); 160 161 fErrorMessage= null; 162 fSerialVersionID= null; 163 164 monitor.beginTask(CorrectionMessages.SerialVersionLaunchConfigurationDelegate_launching_vm, 40); 165 try { 166 monitor.worked(10); 167 monitor.subTask(CorrectionMessages.SerialVersionLaunchConfigurationDelegate_constructing_command_line); 168 final List arguments= new ArrayList (); 169 arguments.add(getJavaExecutable(configuration)); 170 final String [] vmArguments= combineVmArgs(configuration, fInstall); 171 for (int index= 0; index < vmArguments.length; index++) 172 arguments.add(vmArguments[index]); 173 String [] bootClassPath= configuration.getBootClassPath(); 174 final String [] classPath= configuration.getClassPath(); 175 String [] combinedClassPath= null; 176 LibraryLocation[] locations= null; 177 if (bootClassPath == null) { 178 locations= JavaRuntime.getLibraryLocations(fInstall); 179 bootClassPath= new String [locations.length]; 180 for (int index= 0; index < locations.length; index++) 181 bootClassPath[index]= locations[index].getSystemLibraryPath().toOSString(); 182 } 183 if (monitor.isCanceled()) 184 return; 185 combinedClassPath= new String [bootClassPath.length + classPath.length]; 186 int offset= 0; 187 for (int index= 0; index < bootClassPath.length; index++) { 188 combinedClassPath[offset]= bootClassPath[index]; 189 offset++; 190 } 191 for (int index= 0; index < classPath.length; index++) { 192 combinedClassPath[offset]= classPath[index]; 193 offset++; 194 } 195 if (combinedClassPath.length > 0) { 196 arguments.add("-classpath"); arguments.add(flattenClassPath(combinedClassPath)); 198 } 199 arguments.add(configuration.getClassToLaunch()); 200 final String [] programArguments= configuration.getProgramArguments(); 201 for (int index= 0; index < programArguments.length; index++) 202 arguments.add(programArguments[index]); 203 final String [] commandLine= new String [arguments.size()]; 204 arguments.toArray(commandLine); 205 if (monitor.isCanceled()) 206 return; 207 monitor.worked(10); 208 monitor.subTask(CorrectionMessages.SerialVersionLaunchConfigurationDelegate_starting_vm); 209 final Process process= exec(commandLine, null); 210 if (process != null) { 211 try { 212 process.waitFor(); 213 } catch (InterruptedException exception) { 214 } 216 monitor.worked(10); 217 final String directory= System.getProperty("java.io.tmpdir"); if (directory != null && !"".equals(directory)) { final String separator= System.getProperty("file.separator"); if (separator != null && !"".equals(separator)) { final File file= new File (directory + separator + TEMP_FILE_NAME); 222 if (file.exists()) { 223 monitor.worked(40); 224 file.deleteOnExit(); 225 BufferedReader reader= null; 226 final List lines= new ArrayList (); 227 try { 228 reader= new BufferedReader (new InputStreamReader (new FileInputStream (file), TEMP_FILE_ENCODING)); 229 while (reader.ready()) { 230 final String line= reader.readLine(); 231 if (line != null && !"".equals(line)) lines.add(line); 233 } 234 } catch (IOException exception) { 235 fErrorMessage= exception.getLocalizedMessage(); 236 } finally { 237 if (reader != null) { 238 try { 239 reader.close(); 240 } catch (IOException exception) { 241 } 243 } 244 } 245 fSerialVersionID= new long[lines.size()]; 246 for (int index= 0; index < fSerialVersionID.length; index++) { 247 final String line= (String ) lines.get(index); 248 try { 249 fSerialVersionID[index]= Long.parseLong(line); 250 } catch (NumberFormatException exception) { 251 fSerialVersionID[index]= FAILING_ID; 252 } 253 } 254 } else 255 fErrorMessage= CorrectionMessages.SerialVersionLaunchConfigurationDelegate_temp_file_not_exists; 256 } else 257 fErrorMessage= CorrectionMessages.SerialVersionLaunchConfigurationDelegate_error_getting_separator_property; 258 } else 259 fErrorMessage= CorrectionMessages.SerialVersionLaunchConfigurationDelegate_error_getting_temp_dir_property; 260 if (monitor.isCanceled()) 261 process.destroy(); 262 } 263 } finally { 264 monitor.done(); 265 } 266 } 267 } 268 269 private static final char fgSeparator = File.separatorChar; 270 274 private static final String [] fgCandidateJavaFiles = {"javaw", "javaw.exe", "java", "java.exe", "j9w", "j9w.exe", "j9", "j9.exe"}; private static final String [] fgCandidateJavaLocations = {"bin" + fgSeparator, "jre" + fgSeparator + "bin" + fgSeparator}; 277 282 public static File findJavaExecutable(File vmInstallLocation) { 283 for (int i = 0; i < fgCandidateJavaFiles.length; i++) { 286 for (int j = 0; j < fgCandidateJavaLocations.length; j++) { 287 File javaFile = new File (vmInstallLocation, fgCandidateJavaLocations[j] + fgCandidateJavaFiles[i]); 288 if (javaFile.isFile()) { 289 return javaFile; 290 } 291 } 292 } 293 return null; 294 } 295 296 297 private String fErrorMessage= null; 298 299 300 private long[] fSerialVersionID= {}; 301 302 307 public String getErrorMessage() { 308 return fErrorMessage; 309 } 310 311 316 public long[] getSerialVersionIDs() { 317 return fSerialVersionID; 318 } 319 320 323 public void launch(final ILaunchConfiguration configuration, final String mode, final ILaunch launch, IProgressMonitor monitor) throws CoreException { 324 Assert.isNotNull(configuration); 325 Assert.isNotNull(monitor); 326 try { 327 monitor.beginTask(Messages.format("{0}...", new String [] { configuration.getName()}), 100); if (monitor.isCanceled()) 329 return; 330 331 monitor.subTask(CorrectionMessages.SerialVersionLaunchConfigurationDelegate_verifying_launch_attributes); 332 333 final String type= verifyMainTypeName(configuration); 334 final IVMInstall install= verifyVMInstall(configuration); 335 final IVMRunner runner= new SerialVersionRunner(install); 336 monitor.worked(10); 337 338 monitor.subTask(CorrectionMessages.SerialVersionLaunchConfigurationDelegate_setting_up); 339 340 final String [] environment= DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration); 341 final String programArguments= getProgramArguments(configuration); 342 final String vmArguments= getVMArguments(configuration); 343 final ExecutionArguments execArguments= new ExecutionArguments(vmArguments, programArguments); 344 final Map attributes= getVMSpecificAttributesMap(configuration); 345 final String [] classpath= getClasspath(configuration); 346 347 monitor.worked(5); 348 349 final VMRunnerConfiguration vmConfiguration= new VMRunnerConfiguration(type, classpath); 350 vmConfiguration.setProgramArguments(execArguments.getProgramArgumentsArray()); 351 vmConfiguration.setEnvironment(environment); 352 vmConfiguration.setVMArguments(execArguments.getVMArgumentsArray()); 353 vmConfiguration.setVMSpecificAttributesMap(attributes); 354 vmConfiguration.setBootClassPath(getBootpath(configuration)); 355 356 if (monitor.isCanceled()) 357 return; 358 359 monitor.subTask(CorrectionMessages.SerialVersionLaunchConfigurationDelegate_launching_computation); 360 monitor.worked(5); 361 362 runner.run(vmConfiguration, launch, new SubProgressMonitor(monitor, 80)); 363 364 if (monitor.isCanceled()) 365 return; 366 367 } finally { 368 monitor.done(); 369 } 370 } 371 } 372 | Popular Tags |