1 11 package org.eclipse.jdt.internal.launching; 12 13 14 import java.io.File ; 15 import java.io.IOException ; 16 import java.net.MalformedURLException ; 17 import java.net.URL ; 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.ListIterator ; 24 import java.util.Map ; 25 import java.util.Map.Entry; 26 27 import org.eclipse.core.runtime.CoreException; 28 import org.eclipse.core.runtime.IPath; 29 import org.eclipse.core.runtime.IStatus; 30 import org.eclipse.core.runtime.Path; 31 import org.eclipse.core.runtime.Platform; 32 import org.eclipse.core.runtime.Status; 33 import org.eclipse.debug.core.DebugPlugin; 34 import org.eclipse.debug.core.ILaunchManager; 35 import org.eclipse.debug.core.Launch; 36 import org.eclipse.debug.core.model.IProcess; 37 import org.eclipse.debug.core.model.IStreamsProxy; 38 import org.eclipse.jdt.launching.AbstractVMInstallType; 39 import org.eclipse.jdt.launching.IVMInstall; 40 import org.eclipse.jdt.launching.LibraryLocation; 41 import org.eclipse.osgi.service.environment.Constants; 42 43 import com.ibm.icu.text.MessageFormat; 44 45 49 public class StandardVMType extends AbstractVMInstallType { 50 51 public static final String ID_STANDARD_VM_TYPE = "org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType"; 53 56 private String fDefaultRootPath; 57 58 62 private static Map fgFailedInstallPath = new HashMap (); 63 64 67 private static final char fgSeparator = File.separatorChar; 68 69 73 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}; 76 81 public static File findJavaExecutable(File vmInstallLocation) { 82 for (int i = 0; i < fgCandidateJavaFiles.length; i++) { 85 for (int j = 0; j < fgCandidateJavaLocations.length; j++) { 86 File javaFile = new File (vmInstallLocation, fgCandidateJavaLocations[j] + fgCandidateJavaFiles[i]); 87 if (javaFile.isFile()) { 88 return javaFile; 89 } 90 } 91 } 92 return null; 93 } 94 95 98 public String getName() { 99 return LaunchingMessages.StandardVMType_Standard_VM_3; 100 } 101 102 105 protected IVMInstall doCreateVMInstall(String id) { 106 return new StandardVM(this, id); 107 } 108 109 114 protected synchronized LibraryInfo getLibraryInfo(File javaHome, File javaExecutable) { 115 116 String installPath = javaHome.getAbsolutePath(); 118 LibraryInfo info = LaunchingPlugin.getLibraryInfo(installPath); 119 if (info == null) { 120 info= (LibraryInfo)fgFailedInstallPath.get(installPath); 121 if (info == null) { 122 info = generateLibraryInfo(javaHome, javaExecutable); 123 if (info == null) { 124 info = getDefaultLibraryInfo(javaHome); 125 fgFailedInstallPath.put(installPath, info); 126 } else { 127 LaunchingPlugin.setLibraryInfo(installPath, info); 129 } 130 } 131 } 132 return info; 133 } 134 135 139 protected boolean canDetectDefaultSystemLibraries(File javaHome, File javaExecutable) { 140 LibraryLocation[] locations = getDefaultLibraryLocations(javaHome); 141 String version = getVMVersion(javaHome, javaExecutable); 142 return locations.length > 0 && !version.startsWith("1.1"); } 144 145 153 protected String getVMVersion(File javaHome, File javaExecutable) { 154 LibraryInfo info = getLibraryInfo(javaHome, javaExecutable); 155 return info.getVersion(); 156 } 157 158 161 public File detectInstallLocation() { 162 if (Platform.getOS().equals(Constants.OS_MACOSX)) { 164 return null; 165 } 166 167 File javaHome; 170 try { 171 javaHome= new File (System.getProperty("java.home")).getCanonicalFile(); } catch (IOException e) { 173 LaunchingPlugin.log(e); 174 return null; 175 } 176 if (!javaHome.exists()) { 177 return null; 178 } 179 180 File javaExecutable = findJavaExecutable(javaHome); 183 if (javaExecutable == null) { 184 return null; 185 } 186 187 boolean foundLibraries = false; 190 if (javaHome.getName().equalsIgnoreCase("jre")) { File parent= new File (javaHome.getParent()); 192 if (canDetectDefaultSystemLibraries(parent, javaExecutable)) { 193 javaHome = parent; 194 foundLibraries = true; 195 } 196 } 197 198 if (!foundLibraries) { 200 if (!canDetectDefaultSystemLibraries(javaHome, javaExecutable)) { 201 return null; 202 } 203 } 204 205 return javaHome; 206 } 207 208 212 protected IPath getDefaultSystemLibrary(File javaHome) { 213 IPath jreLibPath= new Path(javaHome.getPath()).append("lib").append("rt.jar"); if (jreLibPath.toFile().isFile()) { 215 return jreLibPath; 216 } 217 return new Path(javaHome.getPath()).append("jre").append("lib").append("rt.jar"); } 219 220 228 protected IPath getDefaultSystemLibrarySource(File libLocation) { 229 File parent= libLocation.getParentFile(); 230 while (parent != null) { 231 File parentsrc= new File (parent, "src.jar"); if (parentsrc.isFile()) { 233 setDefaultRootPath("src"); return new Path(parentsrc.getPath()); 235 } 236 parentsrc= new File (parent, "src.zip"); if (parentsrc.isFile()) { 238 setDefaultRootPath(""); return new Path(parentsrc.getPath()); 240 } 241 parent = parent.getParentFile(); 242 } 243 IPath result = checkForJ9LibrarySource(libLocation); 245 if (result != null) 246 return result; 247 setDefaultRootPath(""); return Path.EMPTY; 249 } 250 251 private IPath checkForJ9LibrarySource(File libLocation) { 254 File parent= libLocation.getParentFile(); 255 String name = libLocation.getName(); 256 if (name.equalsIgnoreCase("classes.zip")) { File source = new File (parent, "source/source.zip"); return source.isFile() ? new Path(source.getPath()) : Path.EMPTY; 259 } 260 if (name.equalsIgnoreCase("locale.zip")) { File source = new File (parent, "source/locale-src.zip"); return source.isFile() ? new Path(source.getPath()) : Path.EMPTY; 263 } 264 if (name.equalsIgnoreCase("charconv.zip")) { File source = new File (parent, "charconv-src.zip"); return source.isFile() ? new Path(source.getPath()) : Path.EMPTY; 267 } 268 return null; 269 } 270 271 protected IPath getDefaultPackageRootPath() { 272 return new Path(getDefaultRootPath()); 273 } 274 275 281 public LibraryLocation[] getDefaultLibraryLocations(File installLocation) { 282 283 File javaExecutable = findJavaExecutable(installLocation); 287 LibraryInfo libInfo; 288 if (javaExecutable == null) { 289 libInfo = getDefaultLibraryInfo(installLocation); 290 } else { 291 libInfo = getLibraryInfo(installLocation, javaExecutable); 292 } 293 294 List allLibs = new ArrayList (gatherAllLibraries(libInfo.getEndorsedDirs())); 296 297 String [] bootpath = libInfo.getBootpath(); 299 List boot = new ArrayList (bootpath.length); 300 URL url = getDefaultJavadocLocation(installLocation); 301 for (int i = 0; i < bootpath.length; i++) { 302 IPath path = new Path(bootpath[i]); 303 File lib = path.toFile(); 304 if (lib.exists() && lib.isFile()) { 305 LibraryLocation libraryLocation = new LibraryLocation(path, 306 getDefaultSystemLibrarySource(lib), 307 getDefaultPackageRootPath(), 308 url); 309 boot.add(libraryLocation); 310 } 311 } 312 allLibs.addAll(boot); 313 314 allLibs.addAll(gatherAllLibraries(libInfo.getExtensionDirs())); 316 317 HashSet set = new HashSet (); 319 LibraryLocation lib = null; 320 for(ListIterator liter = allLibs.listIterator(); liter.hasNext();) { 321 lib = (LibraryLocation) liter.next(); 322 IPath systemLibraryPath = lib.getSystemLibraryPath(); 323 String device = systemLibraryPath.getDevice(); 324 if (device != null) { 325 systemLibraryPath = systemLibraryPath.setDevice(device.toUpperCase()); 327 } 328 if(!set.add(systemLibraryPath.toOSString())) { 329 liter.remove(); 331 } 332 } 333 return (LibraryLocation[])allLibs.toArray(new LibraryLocation[allLibs.size()]); 334 } 335 336 342 protected LibraryInfo getDefaultLibraryInfo(File installLocation) { 343 IPath rtjar = getDefaultSystemLibrary(installLocation); 344 File extDir = getDefaultExtensionDirectory(installLocation); 345 File endDir = getDefaultEndorsedDirectory(installLocation); 346 String [] dirs = null; 347 if (extDir == null) { 348 dirs = new String [0]; 349 } else { 350 dirs = new String [] {extDir.getAbsolutePath()}; 351 } 352 String [] endDirs = null; 353 if (endDir == null) { 354 endDirs = new String [0]; 355 } else { 356 endDirs = new String [] {endDir.getAbsolutePath()}; 357 } 358 return new LibraryInfo("???", new String [] {rtjar.toOSString()}, dirs, endDirs); } 360 361 367 protected List gatherAllLibraries(String [] dirPaths) { 368 List libraries = new ArrayList (); 369 for (int i = 0; i < dirPaths.length; i++) { 370 File extDir = new File (dirPaths[i]); 371 if (extDir.exists() && extDir.isDirectory()) { 372 String [] names = extDir.list(); 373 for (int j = 0; j < names.length; j++) { 374 String name = names[j]; 375 File jar = new File (extDir, name); 376 if (jar.isFile()) { 377 int length = name.length(); 378 if (length > 4) { 379 String suffix = name.substring(length - 4); 380 if (suffix.equalsIgnoreCase(".zip") || suffix.equalsIgnoreCase(".jar")) { try { 382 IPath libPath = new Path(jar.getCanonicalPath()); 383 LibraryLocation library = new LibraryLocation(libPath, Path.EMPTY, Path.EMPTY, null); 384 libraries.add(library); 385 } catch (IOException e) { 386 LaunchingPlugin.log(e); 387 } 388 } 389 } 390 } 391 } 392 } 393 } 394 return libraries; 395 } 396 397 405 protected File getDefaultExtensionDirectory(File installLocation) { 406 File jre = null; 407 if (installLocation.getName().equalsIgnoreCase("jre")) { jre = installLocation; 409 } else { 410 jre = new File (installLocation, "jre"); } 412 File lib = new File (jre, "lib"); File ext = new File (lib, "ext"); return ext; 415 } 416 417 425 protected File getDefaultEndorsedDirectory(File installLocation) { 426 File lib = new File (installLocation, "lib"); File ext = new File (lib, "endorsed"); return ext; 429 } 430 431 protected String getDefaultRootPath() { 432 return fDefaultRootPath; 433 } 434 435 protected void setDefaultRootPath(String defaultRootPath) { 436 fDefaultRootPath = defaultRootPath; 437 } 438 439 442 public IStatus validateInstallLocation(File javaHome) { 443 IStatus status = null; 444 if (Platform.getOS().equals(Constants.OS_MACOSX)) { 445 status = new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), 0, LaunchingMessages.StandardVMType_Standard_VM_not_supported_on_MacOS__1, null); 446 } else { 447 File javaExecutable = findJavaExecutable(javaHome); 448 if (javaExecutable == null) { 449 status = new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), 0, LaunchingMessages.StandardVMType_Not_a_JDK_Root__Java_executable_was_not_found_1, null); } else { 451 if (canDetectDefaultSystemLibraries(javaHome, javaExecutable)) { 452 status = new Status(IStatus.OK, LaunchingPlugin.getUniqueIdentifier(), 0, LaunchingMessages.StandardVMType_ok_2, null); 453 } else { 454 status = new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), 0, LaunchingMessages.StandardVMType_Not_a_JDK_root__System_library_was_not_found__1, null); 455 } 456 } 457 } 458 return status; 459 } 460 461 470 protected LibraryInfo generateLibraryInfo(File javaHome, File javaExecutable) { 471 LibraryInfo info = null; 472 473 IPath classesZip = new Path(javaHome.getAbsolutePath()).append("lib").append("classes.zip"); if (classesZip.toFile().exists()) { 476 return new LibraryInfo("1.1.x", new String [] {classesZip.toOSString()}, new String [0], new String [0]); } 478 File file = LaunchingPlugin.getFileInPlugin(new Path("lib/launchingsupport.jar")); if (file.exists()) { 481 String javaExecutablePath = javaExecutable.getAbsolutePath(); 482 String [] cmdLine = new String [] {javaExecutablePath, "-classpath", file.getAbsolutePath(), "org.eclipse.jdt.internal.launching.support.LibraryDetector"}; Process p = null; 484 try { 485 String envp[] = null; 486 if (Platform.OS_MACOSX.equals(Platform.getOS())) { 487 Map map = DebugPlugin.getDefault().getLaunchManager().getNativeEnvironmentCasePreserved(); 488 if (map.remove(StandardVMDebugger.JAVA_JVM_VERSION) != null) { 489 envp = new String [map.size()]; 490 Iterator iterator = map.entrySet().iterator(); 491 int i = 0; 492 while (iterator.hasNext()) { 493 Entry entry = (Entry) iterator.next(); 494 envp[i] = (String )entry.getKey() + "=" + (String )entry.getValue(); i++; 496 } 497 } 498 } 499 p = DebugPlugin.exec(cmdLine, null, envp); 500 IProcess process = DebugPlugin.newProcess(new Launch(null, ILaunchManager.RUN_MODE, null), p, "Library Detection"); for (int i= 0; i < 200; i++) { 502 if (process.isTerminated()) { 504 break; 505 } 506 try { 507 Thread.sleep(50); 508 } catch (InterruptedException e) { 509 } 510 } 511 info = parseLibraryInfo(process); 512 } catch (CoreException ioe) { 513 LaunchingPlugin.log(ioe); 514 } finally { 515 if (p != null) { 516 p.destroy(); 517 } 518 } 519 } 520 if (info == null) { 521 LaunchingPlugin.log(MessageFormat.format("Failed to retrieve default libraries for {0}", new String []{javaHome.getAbsolutePath()})); } 524 return info; 525 } 526 527 530 protected LibraryInfo parseLibraryInfo(IProcess process) { 531 IStreamsProxy streamsProxy = process.getStreamsProxy(); 532 String text = null; 533 if (streamsProxy != null) { 534 text = streamsProxy.getOutputStreamMonitor().getContents(); 535 } 536 if (text != null && text.length() > 0) { 537 int index = text.indexOf("|"); if (index > 0) { 539 String version = text.substring(0, index); 540 text = text.substring(index + 1); 541 index = text.indexOf("|"); if (index > 0) { 543 String bootPaths = text.substring(0, index); 544 String [] bootPath = parsePaths(bootPaths); 545 546 text = text.substring(index + 1); 547 index = text.indexOf("|"); 549 if (index > 0) { 550 String extDirPaths = text.substring(0, index); 551 String endorsedDirsPath = text.substring(index + 1); 552 String [] extDirs = parsePaths(extDirPaths); 553 String [] endDirs = parsePaths(endorsedDirsPath); 554 return new LibraryInfo(version, bootPath, extDirs, endDirs); 555 } 556 } 557 } 558 } 559 return null; 560 } 561 562 protected String [] parsePaths(String paths) { 563 List list = new ArrayList (); 564 int pos = 0; 565 int index = paths.indexOf(File.pathSeparatorChar, pos); 566 while (index > 0) { 567 String path = paths.substring(pos, index); 568 list.add(path); 569 pos = index + 1; 570 index = paths.indexOf(File.pathSeparatorChar, pos); 571 } 572 String path = paths.substring(pos); 573 if (!path.equals("null")) { list.add(path); 575 } 576 return (String [])list.toArray(new String [list.size()]); 577 } 578 579 582 public void disposeVMInstall(String id) { 583 IVMInstall vm = findVMInstall(id); 584 if (vm != null) { 585 String path = vm.getInstallLocation().getAbsolutePath(); 586 LaunchingPlugin.setLibraryInfo(path, null); 587 fgFailedInstallPath.remove(path); 588 } 589 super.disposeVMInstall(id); 590 } 591 592 595 public URL getDefaultJavadocLocation(File installLocation) { 596 File javaExecutable = findJavaExecutable(installLocation); 597 if (javaExecutable != null) { 598 LibraryInfo libInfo = getLibraryInfo(installLocation, javaExecutable); 599 if (libInfo != null) { 600 String version = libInfo.getVersion(); 601 if (version != null) { 602 try { 603 if (version.startsWith("1.6")) { return new URL ("http://java.sun.com/javase/6/docs/api/"); } else if (version.startsWith("1.5")) { return new URL ("http://java.sun.com/j2se/1.5.0/docs/api/"); } else if (version.startsWith("1.4")) { return new URL ("http://java.sun.com/j2se/1.4.2/docs/api/"); } else if (version.startsWith("1.3")) { return new URL ("http://java.sun.com/j2se/1.3/docs/api/"); } else if (version.startsWith("1.2")) { return new URL ("http://java.sun.com/products/jdk/1.2/docs/api"); } 614 } catch (MalformedURLException e) { 615 } 616 } 617 } 618 } 619 return null; 620 } 621 622 } 623 | Popular Tags |