1 11 package org.eclipse.jdt.launching; 12 13 14 import java.io.BufferedInputStream ; 15 import java.io.ByteArrayInputStream ; 16 import java.io.File ; 17 import java.io.FileInputStream ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.StringReader ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Hashtable ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Set ; 31 32 import javax.xml.parsers.DocumentBuilder ; 33 import javax.xml.parsers.ParserConfigurationException ; 34 import javax.xml.transform.TransformerException ; 35 36 import org.eclipse.core.resources.IProject; 37 import org.eclipse.core.resources.IResource; 38 import org.eclipse.core.resources.IWorkspaceRoot; 39 import org.eclipse.core.resources.ResourcesPlugin; 40 import org.eclipse.core.runtime.CoreException; 41 import org.eclipse.core.runtime.IConfigurationElement; 42 import org.eclipse.core.runtime.IExtensionPoint; 43 import org.eclipse.core.runtime.IPath; 44 import org.eclipse.core.runtime.IProgressMonitor; 45 import org.eclipse.core.runtime.IStatus; 46 import org.eclipse.core.runtime.MultiStatus; 47 import org.eclipse.core.runtime.Path; 48 import org.eclipse.core.runtime.Platform; 49 import org.eclipse.core.runtime.Preferences; 50 import org.eclipse.core.runtime.Status; 51 import org.eclipse.core.variables.IStringVariableManager; 52 import org.eclipse.core.variables.VariablesPlugin; 53 import org.eclipse.debug.core.ILaunchConfiguration; 54 import org.eclipse.debug.core.sourcelookup.ISourceContainer; 55 import org.eclipse.jdt.core.IClasspathAttribute; 56 import org.eclipse.jdt.core.IClasspathContainer; 57 import org.eclipse.jdt.core.IClasspathEntry; 58 import org.eclipse.jdt.core.IJavaModel; 59 import org.eclipse.jdt.core.IJavaProject; 60 import org.eclipse.jdt.core.JavaCore; 61 import org.eclipse.jdt.internal.launching.CompositeId; 62 import org.eclipse.jdt.internal.launching.DefaultEntryResolver; 63 import org.eclipse.jdt.internal.launching.DefaultProjectClasspathEntry; 64 import org.eclipse.jdt.internal.launching.JREContainerInitializer; 65 import org.eclipse.jdt.internal.launching.JavaSourceLookupUtil; 66 import org.eclipse.jdt.internal.launching.LaunchingMessages; 67 import org.eclipse.jdt.internal.launching.LaunchingPlugin; 68 import org.eclipse.jdt.internal.launching.ListenerList; 69 import org.eclipse.jdt.internal.launching.RuntimeClasspathEntry; 70 import org.eclipse.jdt.internal.launching.RuntimeClasspathEntryResolver; 71 import org.eclipse.jdt.internal.launching.RuntimeClasspathProvider; 72 import org.eclipse.jdt.internal.launching.SocketAttachConnector; 73 import org.eclipse.jdt.internal.launching.StandardVMType; 74 import org.eclipse.jdt.internal.launching.VMDefinitionsContainer; 75 import org.eclipse.jdt.internal.launching.VMListener; 76 import org.eclipse.jdt.internal.launching.VariableClasspathEntry; 77 import org.eclipse.jdt.internal.launching.environments.EnvironmentsManager; 78 import org.eclipse.jdt.launching.environments.IExecutionEnvironment; 79 import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager; 80 import org.w3c.dom.Element ; 81 import org.w3c.dom.Node ; 82 import org.w3c.dom.NodeList ; 83 import org.xml.sax.InputSource ; 84 import org.xml.sax.SAXException ; 85 86 import com.ibm.icu.text.MessageFormat; 87 88 100 public final class JavaRuntime { 101 102 106 public static final String JRELIB_VARIABLE= "JRE_LIB"; 108 112 public static final String JRESRC_VARIABLE= "JRE_SRC"; 114 118 public static final String JRESRCROOT_VARIABLE= "JRE_SRCROOT"; 120 126 public static final String EXTENSION_POINT_RUNTIME_CLASSPATH_ENTRY_RESOLVERS= "runtimeClasspathEntryResolvers"; 128 134 public static final String EXTENSION_POINT_RUNTIME_CLASSPATH_PROVIDERS= "classpathProviders"; 136 142 public static final String EXTENSION_POINT_EXECUTION_ENVIRONMENTS= "executionEnvironments"; 144 150 public static final String EXTENSION_POINT_VM_INSTALLS = "vmInstalls"; 152 179 public static final String JRE_CONTAINER = LaunchingPlugin.getUniqueIdentifier() + ".JRE_CONTAINER"; 181 192 public static final int ERR_UNABLE_TO_RESOLVE_JRE = 160; 193 194 201 public static final String PREF_CONNECT_TIMEOUT = LaunchingPlugin.getUniqueIdentifier() + ".PREF_CONNECT_TIMEOUT"; 203 208 public static final String PREF_VM_XML = LaunchingPlugin.getUniqueIdentifier() + ".PREF_VM_XML"; 210 215 public static final int DEF_CONNECT_TIMEOUT = 20000; 216 217 227 public final static String ATTR_CMDLINE= LaunchingPlugin.getUniqueIdentifier() + ".launcher.cmdLine"; 229 250 public static final String CLASSPATH_ATTR_LIBRARY_PATH_ENTRY = LaunchingPlugin.getUniqueIdentifier() + ".CLASSPATH_ATTR_LIBRARY_PATH_ENTRY"; 252 private static Object fgVMLock = new Object (); 254 private static boolean fgInitializingVMs = false; 255 256 private static HashSet fgVMTypes = null; 257 private static String fgDefaultVMId = null; 258 private static String fgDefaultVMConnectorId = null; 259 260 264 private static Map fgVariableResolvers = null; 265 private static Map fgContainerResolvers = null; 266 private static Map fgRuntimeClasspathEntryResolvers = null; 267 268 271 private static Map fgPathProviders = null; 272 273 276 private static IRuntimeClasspathProvider fgDefaultClasspathProvider = new StandardClasspathProvider(); 277 private static IRuntimeClasspathProvider fgDefaultSourcePathProvider = new StandardSourcePathProvider(); 278 279 282 private static ListenerList fgVMListeners = new ListenerList(5); 283 284 289 private static ThreadLocal fgProjects = new ThreadLocal (); private static ThreadLocal fgEntryCount = new ThreadLocal (); 292 295 private static Set fgContributedVMs = new HashSet (); 296 297 301 private JavaRuntime() { 302 } 303 304 307 private static void initializeVMTypeExtensions() { 308 IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(LaunchingPlugin.ID_PLUGIN, "vmInstallTypes"); if(extensionPoint != null) { 310 IConfigurationElement[] configs = extensionPoint.getConfigurationElements(); 311 MultiStatus status = new MultiStatus(LaunchingPlugin.getUniqueIdentifier(), IStatus.OK, "Exceptions occurred", null); fgVMTypes = new HashSet (); 313 for (int i= 0; i < configs.length; i++) { 314 try { 315 fgVMTypes.add(configs[i].createExecutableExtension("class")); } 317 catch (CoreException e) {status.add(e.getStatus());} 318 } 319 if (!status.isOK()) { 320 LaunchingPlugin.log(status); 322 } 323 } 324 else { 325 LaunchingPlugin.log(new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), "VM Install extension point not found", null)); } 327 } 328 329 339 public static IVMInstall getVMInstall(IJavaProject project) throws CoreException { 340 IVMInstall vm = null; 342 IClasspathEntry[] classpath = project.getRawClasspath(); 343 IRuntimeClasspathEntryResolver resolver = null; 344 IClasspathEntry entry = null; 345 for (int i = 0; i < classpath.length; i++) { 346 entry = classpath[i]; 347 switch (entry.getEntryKind()) { 348 case IClasspathEntry.CPE_VARIABLE: 349 resolver = getVariableResolver(entry.getPath().segment(0)); 350 if (resolver != null) { 351 vm = resolver.resolveVMInstall(entry); 352 } 353 break; 354 case IClasspathEntry.CPE_CONTAINER: 355 resolver = getContainerResolver(entry.getPath().segment(0)); 356 if (resolver != null) { 357 vm = resolver.resolveVMInstall(entry); 358 } 359 break; 360 } 361 if (vm != null) { 362 return vm; 363 } 364 } 365 return null; 366 } 367 368 374 public static IVMInstallType getVMInstallType(String id) { 375 IVMInstallType[] vmTypes= getVMInstallTypes(); 376 for (int i= 0; i < vmTypes.length; i++) { 377 if (vmTypes[i].getId().equals(id)) { 378 return vmTypes[i]; 379 } 380 } 381 return null; 382 } 383 384 392 public static void setDefaultVMInstall(IVMInstall vm, IProgressMonitor monitor) throws CoreException { 393 setDefaultVMInstall(vm, monitor, true); 394 } 395 396 407 public static void setDefaultVMInstall(IVMInstall vm, IProgressMonitor monitor, boolean savePreference) throws CoreException { 408 IVMInstall previous = null; 409 if (fgDefaultVMId != null) { 410 previous = getVMFromCompositeId(fgDefaultVMId); 411 } 412 fgDefaultVMId= getCompositeIdFromVM(vm); 413 if (savePreference) { 414 saveVMConfiguration(); 415 } 416 IVMInstall current = null; 417 if (fgDefaultVMId != null) { 418 current = getVMFromCompositeId(fgDefaultVMId); 419 } 420 if (previous != current) { 421 notifyDefaultVMChanged(previous, current); 422 } 423 } 424 425 434 public static void setDefaultVMConnector(IVMConnector connector, IProgressMonitor monitor) throws CoreException { 435 fgDefaultVMConnectorId= connector.getIdentifier(); 436 saveVMConfiguration(); 437 } 438 439 444 public static IVMInstall getDefaultVMInstall() { 445 IVMInstall install= getVMFromCompositeId(getDefaultVMId()); 446 if (install != null && install.getInstallLocation().exists()) { 447 return install; 448 } 449 if (install != null) { 451 install.getVMInstallType().disposeVMInstall(install.getId()); 452 } 453 synchronized (fgVMLock) { 454 fgDefaultVMId = null; 455 fgVMTypes = null; 456 initializeVMs(); 457 } 458 return getVMFromCompositeId(getDefaultVMId()); 459 } 460 461 466 public static IVMConnector getDefaultVMConnector() { 467 String id = getDefaultVMConnectorId(); 468 IVMConnector connector = null; 469 if (id != null) { 470 connector = getVMConnector(id); 471 } 472 if (connector == null) { 473 connector = new SocketAttachConnector(); 474 } 475 return connector; 476 } 477 478 485 public static IVMInstallType[] getVMInstallTypes() { 486 initializeVMs(); 487 return (IVMInstallType[]) fgVMTypes.toArray(new IVMInstallType[fgVMTypes.size()]); 488 } 489 490 494 private static String getDefaultVMId() { 495 initializeVMs(); 496 return fgDefaultVMId; 497 } 498 499 503 private static String getDefaultVMConnectorId() { 504 initializeVMs(); 505 return fgDefaultVMConnectorId; 506 } 507 508 515 public static String getCompositeIdFromVM(IVMInstall vm) { 516 if (vm == null) { 517 return null; 518 } 519 IVMInstallType vmType = vm.getVMInstallType(); 520 String typeID = vmType.getId(); 521 CompositeId id = new CompositeId(new String [] { typeID, vm.getId() }); 522 return id.toString(); 523 } 524 525 533 public static IVMInstall getVMFromCompositeId(String idString) { 534 if (idString == null || idString.length() == 0) { 535 return null; 536 } 537 CompositeId id= CompositeId.fromString(idString); 538 if (id.getPartCount() == 2) { 539 IVMInstallType vmType= getVMInstallType(id.get(0)); 540 if (vmType != null) { 541 return vmType.findVMInstall(id.get(1)); 542 } 543 } 544 return null; 545 } 546 547 556 public static IRuntimeClasspathEntry newStringVariableClasspathEntry(String expression) { 557 return new VariableClasspathEntry(expression); 558 } 559 560 568 public static IRuntimeClasspathEntry newDefaultProjectClasspathEntry(IJavaProject project) { 569 return new DefaultProjectClasspathEntry(project); 570 } 571 572 579 public static IRuntimeClasspathEntry newProjectRuntimeClasspathEntry(IJavaProject project) { 580 return newRuntimeClasspathEntry(JavaCore.newProjectEntry(project.getProject().getFullPath())); 581 } 582 583 584 591 public static IRuntimeClasspathEntry newArchiveRuntimeClasspathEntry(IResource resource) { 592 return newRuntimeClasspathEntry(JavaCore.newLibraryEntry(resource.getFullPath(), null, null)); 593 } 594 595 603 public static IRuntimeClasspathEntry newArchiveRuntimeClasspathEntry(IPath path) { 604 return newRuntimeClasspathEntry(JavaCore.newLibraryEntry(path, null, null)); 605 } 606 607 616 public static IRuntimeClasspathEntry newVariableRuntimeClasspathEntry(IPath path) { 617 return newRuntimeClasspathEntry(JavaCore.newVariableEntry(path, null, null)); 618 } 619 620 631 public static IRuntimeClasspathEntry newRuntimeContainerClasspathEntry(IPath path, int classpathProperty) throws CoreException { 632 return newRuntimeContainerClasspathEntry(path, classpathProperty, null); 633 } 634 635 649 public static IRuntimeClasspathEntry newRuntimeContainerClasspathEntry(IPath path, int classpathProperty, IJavaProject project) throws CoreException { 650 RuntimeClasspathEntry entry = new RuntimeClasspathEntry(JavaCore.newContainerEntry(path), classpathProperty); 651 entry.setJavaProject(project); 652 return entry; 653 } 654 655 663 public static IRuntimeClasspathEntry newRuntimeClasspathEntry(String memento) throws CoreException { 664 try { 665 Element root = null; 666 DocumentBuilder parser = LaunchingPlugin.getParser(); 667 StringReader reader = new StringReader (memento); 668 InputSource source = new InputSource (reader); 669 root = parser.parse(source).getDocumentElement(); 670 671 String id = root.getAttribute("id"); if (id == null || id.length() == 0) { 673 return new RuntimeClasspathEntry(root); 675 } 676 IRuntimeClasspathEntry2 entry = LaunchingPlugin.getDefault().newRuntimeClasspathEntry(id); 678 NodeList list = root.getChildNodes(); 679 Node node = null; 680 Element element = null; 681 for (int i = 0; i < list.getLength(); i++) { 682 node = list.item(i); 683 if (node.getNodeType() == Node.ELEMENT_NODE) { 684 element = (Element)node; 685 if ("memento".equals(element.getNodeName())) { entry.initializeFrom(element); 687 } 688 } 689 } 690 return entry; 691 } catch (SAXException e) { 692 abort(LaunchingMessages.JavaRuntime_31, e); 693 } catch (IOException e) { 694 abort(LaunchingMessages.JavaRuntime_32, e); 695 } 696 return null; 697 } 698 699 708 private static IRuntimeClasspathEntry newRuntimeClasspathEntry(IClasspathEntry entry) { 709 return new RuntimeClasspathEntry(entry); 710 } 711 712 721 public static IRuntimeClasspathEntry[] computeUnresolvedRuntimeClasspath(IJavaProject project) throws CoreException { 722 IClasspathEntry[] entries = project.getRawClasspath(); 723 List classpathEntries = new ArrayList (3); 724 for (int i = 0; i < entries.length; i++) { 725 IClasspathEntry entry = entries[i]; 726 switch (entry.getEntryKind()) { 727 case IClasspathEntry.CPE_CONTAINER: 728 IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), project); 729 if (container != null) { 730 switch (container.getKind()) { 731 case IClasspathContainer.K_APPLICATION: 732 break; 734 case IClasspathContainer.K_DEFAULT_SYSTEM: 735 classpathEntries.add(newRuntimeContainerClasspathEntry(container.getPath(), IRuntimeClasspathEntry.STANDARD_CLASSES, project)); 736 break; 737 case IClasspathContainer.K_SYSTEM: 738 classpathEntries.add(newRuntimeContainerClasspathEntry(container.getPath(), IRuntimeClasspathEntry.BOOTSTRAP_CLASSES, project)); 739 break; 740 } 741 } 742 break; 743 case IClasspathEntry.CPE_VARIABLE: 744 if (JRELIB_VARIABLE.equals(entry.getPath().segment(0))) { 745 IRuntimeClasspathEntry jre = newVariableRuntimeClasspathEntry(entry.getPath()); 746 jre.setClasspathProperty(IRuntimeClasspathEntry.STANDARD_CLASSES); 747 classpathEntries.add(jre); 748 } 749 break; 750 default: 751 break; 752 } 753 } 754 classpathEntries.add(newDefaultProjectClasspathEntry(project)); 755 return (IRuntimeClasspathEntry[]) classpathEntries.toArray(new IRuntimeClasspathEntry[classpathEntries.size()]); 756 } 757 758 767 public static IRuntimeClasspathEntry[] computeUnresolvedSourceLookupPath(ILaunchConfiguration configuration) throws CoreException { 768 return getSourceLookupPathProvider(configuration).computeUnresolvedClasspath(configuration); 769 } 770 771 781 public static IRuntimeClasspathEntry[] resolveSourceLookupPath(IRuntimeClasspathEntry[] entries, ILaunchConfiguration configuration) throws CoreException { 782 return getSourceLookupPathProvider(configuration).resolveClasspath(entries, configuration); 783 } 784 785 793 public static IRuntimeClasspathProvider getClasspathProvider(ILaunchConfiguration configuration) throws CoreException { 794 String providerId = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH_PROVIDER, (String )null); 795 IRuntimeClasspathProvider provider = null; 796 if (providerId == null) { 797 provider = fgDefaultClasspathProvider; 798 } else { 799 provider = (IRuntimeClasspathProvider)getClasspathProviders().get(providerId); 800 if (provider == null) { 801 abort(MessageFormat.format(LaunchingMessages.JavaRuntime_26, new String []{providerId}), null); 802 } 803 } 804 return provider; 805 } 806 807 815 public static IRuntimeClasspathProvider getSourceLookupPathProvider(ILaunchConfiguration configuration) throws CoreException { 816 String providerId = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER, (String )null); 817 IRuntimeClasspathProvider provider = null; 818 if (providerId == null) { 819 provider = fgDefaultSourcePathProvider; 820 } else { 821 provider = (IRuntimeClasspathProvider)getClasspathProviders().get(providerId); 822 if (provider == null) { 823 abort(MessageFormat.format(LaunchingMessages.JavaRuntime_27, new String []{providerId}), null); 824 } 825 } 826 return provider; 827 } 828 829 851 public static IRuntimeClasspathEntry[] resolveRuntimeClasspathEntry(IRuntimeClasspathEntry entry, ILaunchConfiguration configuration) throws CoreException { 852 switch (entry.getType()) { 853 case IRuntimeClasspathEntry.PROJECT: 854 IResource resource = entry.getResource(); 856 if (resource instanceof IProject) { 857 IProject p = (IProject)resource; 858 IJavaProject project = JavaCore.create(p); 859 if (project == null || !p.isOpen() || !project.exists()) { 860 return new IRuntimeClasspathEntry[0]; 861 } 862 IRuntimeClasspathEntry[] entries = resolveOutputLocations(project, entry.getClasspathProperty()); 863 if (entries != null) { 864 return entries; 865 } 866 } else { 867 abort(MessageFormat.format(LaunchingMessages.JavaRuntime_Classpath_references_non_existant_project___0__3, new String []{entry.getPath().lastSegment()}), null); 869 } 870 break; 871 case IRuntimeClasspathEntry.VARIABLE: 872 IRuntimeClasspathEntryResolver resolver = getVariableResolver(entry.getVariableName()); 873 if (resolver == null) { 874 IRuntimeClasspathEntry[] resolved = resolveVariableEntry(entry, null, configuration); 875 if (resolved != null) { 876 return resolved; 877 } 878 break; 879 } 880 return resolver.resolveRuntimeClasspathEntry(entry, configuration); 881 case IRuntimeClasspathEntry.CONTAINER: 882 resolver = getContainerResolver(entry.getVariableName()); 883 if (resolver == null) { 884 return computeDefaultContainerEntries(entry, configuration); 885 } 886 return resolver.resolveRuntimeClasspathEntry(entry, configuration); 887 case IRuntimeClasspathEntry.ARCHIVE: 888 String location = entry.getLocation(); 890 if (location == null) { 891 abort(MessageFormat.format(LaunchingMessages.JavaRuntime_Classpath_references_non_existant_archive___0__4, new String []{entry.getPath().toString()}), null); 892 } 893 File file = new File (location); 894 if (!file.exists()) { 895 abort(MessageFormat.format(LaunchingMessages.JavaRuntime_Classpath_references_non_existant_archive___0__4, new String []{entry.getPath().toString()}), null); 896 } 897 break; 898 case IRuntimeClasspathEntry.OTHER: 899 resolver = getContributedResolver(((IRuntimeClasspathEntry2)entry).getTypeId()); 900 return resolver.resolveRuntimeClasspathEntry(entry, configuration); 901 default: 902 break; 903 } 904 return new IRuntimeClasspathEntry[] {entry}; 905 } 906 907 917 private static IRuntimeClasspathEntry[] resolveVariableEntry(IRuntimeClasspathEntry entry, IJavaProject project, ILaunchConfiguration configuration) throws CoreException { 918 IPath archPath = JavaCore.getClasspathVariable(entry.getVariableName()); 920 if (archPath != null) { 921 if (entry.getPath().segmentCount() > 1) { 922 archPath = archPath.append(entry.getPath().removeFirstSegments(1)); 923 } 924 IPath srcPath = null; 925 IPath srcVar = entry.getSourceAttachmentPath(); 926 IPath srcRootPath = null; 927 IPath srcRootVar = entry.getSourceAttachmentRootPath(); 928 if (archPath != null && !archPath.isEmpty()) { 929 if (srcVar != null && !srcVar.isEmpty()) { 930 srcPath = JavaCore.getClasspathVariable(srcVar.segment(0)); 931 if (srcPath != null) { 932 if (srcVar.segmentCount() > 1) { 933 srcPath = srcPath.append(srcVar.removeFirstSegments(1)); 934 } 935 if (srcRootVar != null && !srcRootVar.isEmpty()) { 936 srcRootPath = JavaCore.getClasspathVariable(srcRootVar.segment(0)); 937 if (srcRootPath != null) { 938 if (srcRootVar.segmentCount() > 1) { 939 srcRootPath = srcRootPath.append(srcRootVar.removeFirstSegments(1)); 940 } 941 } 942 } 943 } 944 } 945 IClasspathEntry archEntry = JavaCore.newLibraryEntry(archPath, srcPath, srcRootPath, entry.getClasspathEntry().isExported()); 947 IRuntimeClasspathEntry runtimeArchEntry = newRuntimeClasspathEntry(archEntry); 948 runtimeArchEntry.setClasspathProperty(entry.getClasspathProperty()); 949 if (configuration == null) { 950 return resolveRuntimeClasspathEntry(runtimeArchEntry, project); 951 } 952 return resolveRuntimeClasspathEntry(runtimeArchEntry, configuration); 953 } 954 } 955 return null; 956 } 957 958 968 private static IRuntimeClasspathEntry[] resolveOutputLocations(IJavaProject project, int classpathProperty) throws CoreException { 969 List nonDefault = new ArrayList (); 970 if (project.exists() && project.getProject().isOpen()) { 971 IClasspathEntry entries[] = project.getRawClasspath(); 972 for (int i = 0; i < entries.length; i++) { 973 IClasspathEntry classpathEntry = entries[i]; 974 if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { 975 IPath path = classpathEntry.getOutputLocation(); 976 if (path != null) { 977 nonDefault.add(path); 978 } 979 } 980 } 981 } 982 if (nonDefault.isEmpty()) { 983 return null; 984 } 985 IPath def = project.getOutputLocation(); 987 if (!nonDefault.contains(def)) { 988 nonDefault.add(def); 989 } 990 IRuntimeClasspathEntry[] locations = new IRuntimeClasspathEntry[nonDefault.size()]; 991 for (int i = 0; i < locations.length; i++) { 992 IClasspathEntry newEntry = JavaCore.newLibraryEntry((IPath)nonDefault.get(i), null, null); 993 locations[i] = new RuntimeClasspathEntry(newEntry); 994 locations[i].setClasspathProperty(classpathProperty); 995 } 996 return locations; 997 } 998 999 1021 public static IRuntimeClasspathEntry[] resolveRuntimeClasspathEntry(IRuntimeClasspathEntry entry, IJavaProject project) throws CoreException { 1022 switch (entry.getType()) { 1023 case IRuntimeClasspathEntry.PROJECT: 1024 IResource resource = entry.getResource(); 1026 if (resource instanceof IProject) { 1027 IProject p = (IProject)resource; 1028 IJavaProject jp = JavaCore.create(p); 1029 if (jp != null && p.isOpen() && jp.exists()) { 1030 IRuntimeClasspathEntry[] entries = resolveOutputLocations(jp, entry.getClasspathProperty()); 1031 if (entries != null) { 1032 return entries; 1033 } 1034 } else { 1035 return new IRuntimeClasspathEntry[0]; 1036 } 1037 } 1038 break; 1039 case IRuntimeClasspathEntry.VARIABLE: 1040 IRuntimeClasspathEntryResolver resolver = getVariableResolver(entry.getVariableName()); 1041 if (resolver == null) { 1042 IRuntimeClasspathEntry[] resolved = resolveVariableEntry(entry, project, null); 1043 if (resolved != null) { 1044 return resolved; 1045 } 1046 break; 1047 } 1048 return resolver.resolveRuntimeClasspathEntry(entry, project); 1049 case IRuntimeClasspathEntry.CONTAINER: 1050 resolver = getContainerResolver(entry.getVariableName()); 1051 if (resolver == null) { 1052 return computeDefaultContainerEntries(entry, project); 1053 } 1054 return resolver.resolveRuntimeClasspathEntry(entry, project); 1055 case IRuntimeClasspathEntry.OTHER: 1056 resolver = getContributedResolver(((IRuntimeClasspathEntry2)entry).getTypeId()); 1057 return resolver.resolveRuntimeClasspathEntry(entry, project); 1058 default: 1059 break; 1060 } 1061 return new IRuntimeClasspathEntry[] {entry}; 1062 } 1063 1064 1068 private static IRuntimeClasspathEntry[] computeDefaultContainerEntries(IRuntimeClasspathEntry entry, ILaunchConfiguration config) throws CoreException { 1069 IJavaProject project = entry.getJavaProject(); 1070 if (project == null) { 1071 project = getJavaProject(config); 1072 } 1073 return computeDefaultContainerEntries(entry, project); 1074 } 1075 1076 1080 private static IRuntimeClasspathEntry[] computeDefaultContainerEntries(IRuntimeClasspathEntry entry, IJavaProject project) throws CoreException { 1081 if (project == null || entry == null) { 1082 return new IRuntimeClasspathEntry[0]; 1084 } 1085 IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), project); 1086 if (container == null) { 1087 abort(MessageFormat.format(LaunchingMessages.JavaRuntime_Could_not_resolve_classpath_container___0__1, new String []{entry.getPath().toString()}), null); 1088 return null; 1090 } 1091 IClasspathEntry[] cpes = container.getClasspathEntries(); 1092 int property = -1; 1093 switch (container.getKind()) { 1094 case IClasspathContainer.K_APPLICATION: 1095 property = IRuntimeClasspathEntry.USER_CLASSES; 1096 break; 1097 case IClasspathContainer.K_DEFAULT_SYSTEM: 1098 property = IRuntimeClasspathEntry.STANDARD_CLASSES; 1099 break; 1100 case IClasspathContainer.K_SYSTEM: 1101 property = IRuntimeClasspathEntry.BOOTSTRAP_CLASSES; 1102 break; 1103 } 1104 List resolved = new ArrayList (cpes.length); 1105 List projects = (List ) fgProjects.get(); 1106 Integer count = (Integer ) fgEntryCount.get(); 1107 if (projects == null) { 1108 projects = new ArrayList (); 1109 fgProjects.set(projects); 1110 count = new Integer (0); 1111 } 1112 int intCount = count.intValue(); 1113 intCount++; 1114 fgEntryCount.set(new Integer (intCount)); 1115 try { 1116 for (int i = 0; i < cpes.length; i++) { 1117 IClasspathEntry cpe = cpes[i]; 1118 if (cpe.getEntryKind() == IClasspathEntry.CPE_PROJECT) { 1119 IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(cpe.getPath().segment(0)); 1120 IJavaProject jp = JavaCore.create(p); 1121 if (!projects.contains(jp)) { 1122 projects.add(jp); 1123 IRuntimeClasspathEntry classpath = newDefaultProjectClasspathEntry(jp); 1124 IRuntimeClasspathEntry[] entries = resolveRuntimeClasspathEntry(classpath, jp); 1125 for (int j = 0; j < entries.length; j++) { 1126 IRuntimeClasspathEntry e = entries[j]; 1127 if (!resolved.contains(e)) { 1128 resolved.add(entries[j]); 1129 } 1130 } 1131 } 1132 } else { 1133 IRuntimeClasspathEntry e = newRuntimeClasspathEntry(cpe); 1134 if (!resolved.contains(e)) { 1135 resolved.add(e); 1136 } 1137 } 1138 } 1139 } finally { 1140 intCount--; 1141 if (intCount == 0) { 1142 fgProjects.set(null); 1143 fgEntryCount.set(null); 1144 } else { 1145 fgEntryCount.set(new Integer (intCount)); 1146 } 1147 } 1148 IRuntimeClasspathEntry[] result = new IRuntimeClasspathEntry[resolved.size()]; 1150 for (int i = 0; i < result.length; i++) { 1151 result[i] = (IRuntimeClasspathEntry) resolved.get(i); 1152 result[i].setClasspathProperty(property); 1153 } 1154 return result; 1155 } 1156 1157 1166 public static IRuntimeClasspathEntry[] computeUnresolvedRuntimeClasspath(ILaunchConfiguration configuration) throws CoreException { 1167 return getClasspathProvider(configuration).computeUnresolvedClasspath(configuration); 1168 } 1169 1170 1180 public static IRuntimeClasspathEntry[] resolveRuntimeClasspath(IRuntimeClasspathEntry[] entries, ILaunchConfiguration configuration) throws CoreException { 1181 return getClasspathProvider(configuration).resolveClasspath(entries, configuration); 1182 } 1183 1184 1191 public static IJavaProject getJavaProject(ILaunchConfiguration configuration) throws CoreException { 1192 String projectName = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String )null); 1193 if ((projectName == null) || (projectName.trim().length() < 1)) { 1194 return null; 1195 } 1196 IJavaProject javaProject = getJavaModel().getJavaProject(projectName); 1197 if (javaProject != null && javaProject.getProject().exists() && !javaProject.getProject().isOpen()) { 1198 abort(MessageFormat.format(LaunchingMessages.JavaRuntime_28, new String [] {configuration.getName(), projectName}), IJavaLaunchConfigurationConstants.ERR_PROJECT_CLOSED, null); 1199 } 1200 if ((javaProject == null) || !javaProject.exists()) { 1201 abort(MessageFormat.format(LaunchingMessages.JavaRuntime_Launch_configuration__0__references_non_existing_project__1___1, new String [] {configuration.getName(), projectName}), IJavaLaunchConfigurationConstants.ERR_NOT_A_JAVA_PROJECT, null); 1202 } 1203 return javaProject; 1204 } 1205 1206 1209 private static IJavaModel getJavaModel() { 1210 return JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); 1211 } 1212 1213 1233 public static IVMInstall computeVMInstall(ILaunchConfiguration configuration) throws CoreException { 1234 String jreAttr = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, (String )null); 1235 if (jreAttr == null) { 1236 String type = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, (String )null); 1237 if (type == null) { 1238 IJavaProject proj = getJavaProject(configuration); 1239 if (proj != null) { 1240 IVMInstall vm = getVMInstall(proj); 1241 if (vm != null) { 1242 return vm; 1243 } 1244 } 1245 } else { 1246 String name = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME, (String )null); 1247 return resolveVM(type, name, configuration); 1248 } 1249 } else { 1250 IPath jrePath = Path.fromPortableString(jreAttr); 1251 IClasspathEntry entry = JavaCore.newContainerEntry(jrePath); 1252 IRuntimeClasspathEntryResolver2 resolver = getVariableResolver(jrePath.segment(0)); 1253 if (resolver != null) { 1254 return resolver.resolveVMInstall(entry); 1255 } else { 1256 resolver = getContainerResolver(jrePath.segment(0)); 1257 if (resolver != null) { 1258 return resolver.resolveVMInstall(entry); 1259 } 1260 } 1261 } 1262 1263 return getDefaultVMInstall(); 1264 } 1265 1274 private static IVMInstall resolveVM(String type, String name, ILaunchConfiguration configuration) throws CoreException { 1275 IVMInstallType vt = getVMInstallType(type); 1276 if (vt == null) { 1277 abort(MessageFormat.format(LaunchingMessages.JavaRuntime_Specified_VM_install_type_does_not_exist___0__2, new String [] {type}), null); 1279 } 1280 IVMInstall vm = null; 1281 if (name == null) { 1283 LaunchingPlugin.log(new Status(IStatus.WARNING, LaunchingPlugin.getUniqueIdentifier(), IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_VM_INSTALL, MessageFormat.format("VM not fully specified in launch configuration {0} - missing VM name. Reverting to default VM.", new String [] {configuration.getName()}), null)); return getDefaultVMInstall(); 1287 } 1288 vm = vt.findVMInstallByName(name); 1289 if (vm == null) { 1290 abort(MessageFormat.format(LaunchingMessages.JavaRuntime_Specified_VM_install_not_found__type__0___name__1__2, new String [] {vt.getName(), name}), null); 1292 } else { 1293 return vm; 1294 } 1295 return null; 1297 } 1298 1299 1306 private static void abort(String message, Throwable exception) throws CoreException { 1307 abort(message, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR, exception); 1308 } 1309 1310 1311 1320 private static void abort(String message, int code, Throwable exception) throws CoreException { 1321 throw new CoreException(new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), code, message, exception)); 1322 } 1323 1324 1332 public static String [] computeDefaultRuntimeClassPath(IJavaProject jproject) throws CoreException { 1333 IRuntimeClasspathEntry[] unresolved = computeUnresolvedRuntimeClasspath(jproject); 1334 List resolved = new ArrayList (unresolved.length); 1337 for (int i = 0; i < unresolved.length; i++) { 1338 IRuntimeClasspathEntry entry = unresolved[i]; 1339 if (entry.getClasspathProperty() == IRuntimeClasspathEntry.USER_CLASSES) { 1340 IRuntimeClasspathEntry[] entries = resolveRuntimeClasspathEntry(entry, jproject); 1341 for (int j = 0; j < entries.length; j++) { 1342 String location = entries[j].getLocation(); 1343 if (location != null) { 1344 resolved.add(location); 1345 } 1346 } 1347 } 1348 } 1349 return (String [])resolved.toArray(new String [resolved.size()]); 1350 } 1351 1352 1362 public static void saveVMConfiguration() throws CoreException { 1363 if (fgVMTypes == null) { 1364 return; 1366 } 1367 try { 1368 String xml = getVMsAsXML(); 1369 getPreferences().setValue(PREF_VM_XML, xml); 1370 savePreferences(); 1371 } catch (IOException e) { 1372 throw new CoreException(new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), IStatus.ERROR, LaunchingMessages.JavaRuntime_exceptionsOccurred, e)); 1373 } catch (ParserConfigurationException e) { 1374 throw new CoreException(new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), IStatus.ERROR, LaunchingMessages.JavaRuntime_exceptionsOccurred, e)); 1375 } catch (TransformerException e) { 1376 throw new CoreException(new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), IStatus.ERROR, LaunchingMessages.JavaRuntime_exceptionsOccurred, e)); 1377 } 1378 } 1379 1380 1387 private static String getVMsAsXML() throws IOException , ParserConfigurationException , TransformerException { 1388 VMDefinitionsContainer container = new VMDefinitionsContainer(); 1389 container.setDefaultVMInstallCompositeID(getDefaultVMId()); 1390 container.setDefaultVMInstallConnectorTypeID(getDefaultVMConnectorId()); 1391 IVMInstallType[] vmTypes = getVMInstallTypes(); 1392 IVMInstall[] vms = null; 1393 for (int i = 0; i < vmTypes.length; ++i) { 1394 vms = vmTypes[i].getVMInstalls(); 1395 for (int j = 0; j < vms.length; j++) { 1396 container.addVM(vms[j]); 1397 } 1398 } 1399 return container.getAsXML(); 1400 } 1401 1402 1410 private static boolean addPersistedVMs(VMDefinitionsContainer vmDefs) throws IOException { 1411 String vmXMLString = getPreferences().getString(PREF_VM_XML); 1413 1414 if (vmXMLString.length() > 0) { 1416 try { 1417 ByteArrayInputStream inputStream = new ByteArrayInputStream (vmXMLString.getBytes("UTF8")); VMDefinitionsContainer.parseXMLIntoContainer(inputStream, vmDefs); 1419 return false; 1420 } catch (IOException ioe) { 1421 LaunchingPlugin.log(ioe); 1422 } 1423 } else { 1424 IPath stateLocation= LaunchingPlugin.getDefault().getStateLocation(); 1426 IPath stateFile= stateLocation.append("vmConfiguration.xml"); File file = new File (stateFile.toOSString()); 1428 1429 if (file.exists()) { 1430 InputStream fileInputStream = new BufferedInputStream (new FileInputStream (file)); 1433 VMDefinitionsContainer.parseXMLIntoContainer(fileInputStream, vmDefs); 1434 } 1435 } 1436 return true; 1437 } 1438 1439 1443 private static void addVMExtensions(VMDefinitionsContainer vmDefs) { 1444 IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(LaunchingPlugin.ID_PLUGIN, JavaRuntime.EXTENSION_POINT_VM_INSTALLS); 1445 IConfigurationElement[] configs= extensionPoint.getConfigurationElements(); 1446 for (int i = 0; i < configs.length; i++) { 1447 IConfigurationElement element = configs[i]; 1448 try { 1449 if ("vmInstall".equals(element.getName())) { String vmType = element.getAttribute("vmInstallType"); if (vmType == null) { 1452 abort(MessageFormat.format("Missing required vmInstallType attribute for vmInstall contributed by {0}", new String []{element.getContributor().getName()}), null); 1454 } 1455 String id = element.getAttribute("id"); if (id == null) { 1457 abort(MessageFormat.format("Missing required id attribute for vmInstall contributed by {0}", new String []{element.getContributor().getName()}), null); 1459 } 1460 IVMInstallType installType = getVMInstallType(vmType); 1461 if (installType == null) { 1462 abort(MessageFormat.format("vmInstall {0} contributed by {1} references undefined VM install type {2}", new String []{id, element.getContributor().getName(), vmType}), null); 1464 } 1465 IVMInstall install = installType.findVMInstall(id); 1466 if (install == null) { 1467 String name = element.getAttribute("name"); if (name == null) { 1470 abort(MessageFormat.format("vmInstall {0} contributed by {1} missing required attribute name", new String []{id, element.getContributor().getName()}), null); 1472 } 1473 String home = element.getAttribute("home"); if (home == null) { 1475 abort(MessageFormat.format("vmInstall {0} contributed by {1} missing required attribute home", new String []{id, element.getContributor().getName()}), null); 1477 } 1478 String javadoc = element.getAttribute("javadocURL"); String vmArgs = element.getAttribute("vmArgs"); VMStandin standin = new VMStandin(installType, id); 1481 standin.setName(name); 1482 home = substitute(home); 1483 File homeDir = new File (home); 1484 if (homeDir.exists()) { 1485 try { 1486 home = homeDir.getCanonicalPath(); 1488 homeDir = new File (home); 1489 } catch (IOException e) { 1490 } 1491 } 1492 IStatus status = installType.validateInstallLocation(homeDir); 1493 if (!status.isOK()) { 1494 abort(MessageFormat.format("Illegal install location {0} for vmInstall {1} contributed by {2}: {3}", new String []{home, id, element.getContributor().getName(), status.getMessage()}), null); 1496 } 1497 standin.setInstallLocation(homeDir); 1498 if (javadoc != null) { 1499 try { 1500 standin.setJavadocLocation(new URL (javadoc)); 1501 } catch (MalformedURLException e) { 1502 abort(MessageFormat.format("Illegal javadocURL attribute for vmInstall {0} contributed by {1}", new String []{id, element.getContributor().getName()}), e); 1504 } 1505 } 1506 if (vmArgs != null) { 1507 standin.setVMArgs(vmArgs); 1508 } 1509 IConfigurationElement[] libraries = element.getChildren("library"); LibraryLocation[] locations = null; 1511 if (libraries.length > 0) { 1512 locations = new LibraryLocation[libraries.length]; 1513 for (int j = 0; j < libraries.length; j++) { 1514 IConfigurationElement library = libraries[j]; 1515 String libPathStr = library.getAttribute("path"); if (libPathStr == null) { 1517 abort(MessageFormat.format("library for vmInstall {0} contributed by {1} missing required attribute libPath", new String []{id, element.getContributor().getName()}), null); 1519 } 1520 String sourcePathStr = library.getAttribute("sourcePath"); String packageRootStr = library.getAttribute("packageRootPath"); String javadocOverride = library.getAttribute("javadocURL"); URL url = null; 1524 if (javadocOverride != null) { 1525 try { 1526 url = new URL (javadocOverride); 1527 } catch (MalformedURLException e) { 1528 abort(MessageFormat.format("Illegal javadocURL attribute specified for library {0} for vmInstall {1} contributed by {2}" ,new String []{libPathStr, id, element.getContributor().getName()}), e); 1530 } 1531 } 1532 IPath homePath = new Path(home); 1533 IPath libPath = homePath.append(substitute(libPathStr)); 1534 IPath sourcePath = Path.EMPTY; 1535 if (sourcePathStr != null) { 1536 sourcePath = homePath.append(substitute(sourcePathStr)); 1537 } 1538 IPath packageRootPath = Path.EMPTY; 1539 if (packageRootStr != null) { 1540 packageRootPath = new Path(substitute(packageRootStr)); 1541 } 1542 locations[j] = new LibraryLocation(libPath, sourcePath, packageRootPath, url); 1543 } 1544 } 1545 standin.setLibraryLocations(locations); 1546 vmDefs.removeVM(standin); 1548 vmDefs.addVM(standin); 1549 } 1550 fgContributedVMs.add(id); 1551 } else { 1552 abort(MessageFormat.format("Illegal element {0} in vmInstalls extension contributed by {1}", new String []{element.getName(), element.getContributor().getName()}), null); 1554 } 1555 } catch (CoreException e) { 1556 LaunchingPlugin.log(e); 1557 } 1558 } 1559 } 1560 1561 1569 private static String substitute(String expression) throws CoreException { 1570 return VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(expression); 1571 } 1572 1573 1581 public static boolean isContributedVMInstall(String id) { 1582 getVMInstallTypes(); return fgContributedVMs.contains(id); 1584 } 1585 1586 1592 public static LibraryLocation[] getLibraryLocations(IVMInstall vm) { 1593 IPath[] libraryPaths; 1594 IPath[] sourcePaths; 1595 IPath[] sourceRootPaths; 1596 URL [] javadocLocations; 1597 LibraryLocation[] locations= vm.getLibraryLocations(); 1598 if (locations == null) { 1599 URL defJavaDocLocation = vm.getJavadocLocation(); 1600 LibraryLocation[] dflts= vm.getVMInstallType().getDefaultLibraryLocations(vm.getInstallLocation()); 1601 libraryPaths = new IPath[dflts.length]; 1602 sourcePaths = new IPath[dflts.length]; 1603 sourceRootPaths = new IPath[dflts.length]; 1604 javadocLocations= new URL [dflts.length]; 1605 for (int i = 0; i < dflts.length; i++) { 1606 libraryPaths[i]= dflts[i].getSystemLibraryPath(); 1607 if (defJavaDocLocation == null) { 1608 javadocLocations[i]= dflts[i].getJavadocLocation(); 1609 } else { 1610 javadocLocations[i]= defJavaDocLocation; 1611 } 1612 if (!libraryPaths[i].toFile().isFile()) { 1613 libraryPaths[i]= Path.EMPTY; 1614 } 1615 1616 sourcePaths[i]= dflts[i].getSystemLibrarySourcePath(); 1617 if (sourcePaths[i].toFile().isFile()) { 1618 sourceRootPaths[i]= dflts[i].getPackageRootPath(); 1619 } else { 1620 sourcePaths[i]= Path.EMPTY; 1621 sourceRootPaths[i]= Path.EMPTY; 1622 } 1623 } 1624 } else { 1625 libraryPaths = new IPath[locations.length]; 1626 sourcePaths = new IPath[locations.length]; 1627 sourceRootPaths = new IPath[locations.length]; 1628 javadocLocations= new URL [locations.length]; 1629 for (int i = 0; i < locations.length; i++) { 1630 libraryPaths[i]= locations[i].getSystemLibraryPath(); 1631 sourcePaths[i]= locations[i].getSystemLibrarySourcePath(); 1632 sourceRootPaths[i]= locations[i].getPackageRootPath(); 1633 javadocLocations[i]= locations[i].getJavadocLocation(); 1634 } 1635 } 1636 locations = new LibraryLocation[sourcePaths.length]; 1637 for (int i = 0; i < sourcePaths.length; i++) { 1638 locations[i] = new LibraryLocation(libraryPaths[i], sourcePaths[i], sourceRootPaths[i], javadocLocations[i]); 1639 } 1640 return locations; 1641 } 1642 1643 1649 private static VMStandin detectEclipseRuntime() { 1650 VMStandin detectedVMStandin = null; 1651 IVMInstallType[] vmTypes= getVMInstallTypes(); 1653 for (int i = 0; i < vmTypes.length; i++) { 1654 1655 File detectedLocation= vmTypes[i].detectInstallLocation(); 1656 if (detectedLocation != null && detectedVMStandin == null) { 1657 1658 long unique = System.currentTimeMillis(); 1660 IVMInstallType vmType = vmTypes[i]; 1661 while (vmType.findVMInstall(String.valueOf(unique)) != null) { 1662 unique++; 1663 } 1664 1665 String vmID = String.valueOf(unique); 1667 detectedVMStandin = new VMStandin(vmType, vmID); 1668 detectedVMStandin.setInstallLocation(detectedLocation); 1669 detectedVMStandin.setName(generateDetectedVMName(detectedVMStandin)); 1670 if (vmType instanceof AbstractVMInstallType) { 1671 AbstractVMInstallType abs = (AbstractVMInstallType)vmType; 1672 URL url = abs.getDefaultJavadocLocation(detectedLocation); 1673 detectedVMStandin.setJavadocLocation(url); 1674 } 1675 } 1676 } 1677 return detectedVMStandin; 1678 } 1679 1680 1688 private static boolean equals(String optionName, Map defaultOptions, Map options) { 1689 if (defaultOptions.containsKey(optionName)) { 1690 return options.containsKey(optionName) && 1691 equals(defaultOptions.get(optionName), options.get(optionName)); 1692 } else { 1693 return !options.containsKey(optionName); 1694 } 1695 } 1696 1697 1704 private static boolean equals(Object o1, Object o2) { 1705 if (o1 == null) { 1706 return o2 == null; 1707 } else { 1708 return o1.equals(o2); 1709 } 1710 } 1711 1712 1715 private static String generateDetectedVMName(IVMInstall vm) { 1716 String name = vm.getInstallLocation().getName(); 1717 name = name.trim(); 1718 if (name.length() == 0) { 1719 name = LaunchingMessages.JavaRuntime_25; 1720 } 1721 return name; 1722 } 1723 1724 1730 public static IClasspathEntry getJREVariableEntry() { 1731 return JavaCore.newVariableEntry( 1732 new Path(JRELIB_VARIABLE), 1733 new Path(JRESRC_VARIABLE), 1734 new Path(JRESRCROOT_VARIABLE) 1735 ); 1736 } 1737 1738 1745 public static IClasspathEntry getDefaultJREContainerEntry() { 1746 return JavaCore.newContainerEntry(newDefaultJREContainerPath()); 1747 } 1748 1749 1756 public static IPath newDefaultJREContainerPath() { 1757 return new Path(JRE_CONTAINER); 1758 } 1759 1760 1768 public static IPath newJREContainerPath(IVMInstall vm) { 1769 return newJREContainerPath(vm.getVMInstallType().getId(), vm.getName()); 1770 } 1771 1772 1781 public static IPath newJREContainerPath(String typeId, String name) { 1782 IPath path = newDefaultJREContainerPath(); 1783 path = path.append(typeId); 1784 path = path.append(name); 1785 return path; 1786 } 1787 1788 1796 public static IPath newJREContainerPath(IExecutionEnvironment environment) { 1797 IPath path = newDefaultJREContainerPath(); 1798 path = path.append(StandardVMType.ID_STANDARD_VM_TYPE); 1799 path = path.append(JREContainerInitializer.encodeEnvironmentId(environment.getId())); 1800 return path; 1801 } 1802 1803 1812 public static IVMInstall getVMInstall(IPath jreContainerPath) { 1813 return JREContainerInitializer.resolveVM(jreContainerPath); 1814 } 1815 1816 1824 public static String getVMInstallTypeId(IPath jreContainerPath) { 1825 if (JREContainerInitializer.isExecutionEnvironment(jreContainerPath)) { 1826 return null; 1827 } 1828 return JREContainerInitializer.getVMTypeId(jreContainerPath); 1829 } 1830 1831 1839 public static String getVMInstallName(IPath jreContainerPath) { 1840 if (JREContainerInitializer.isExecutionEnvironment(jreContainerPath)) { 1841 return null; 1842 } 1843 return JREContainerInitializer.getVMName(jreContainerPath); 1844 } 1845 1846 1854 public static String getExecutionEnvironmentId(IPath jreContainerPath) { 1855 return JREContainerInitializer.getExecutionEnvironmentId(jreContainerPath); 1856 } 1857 1858 1882 public static IRuntimeClasspathEntry computeJREEntry(ILaunchConfiguration configuration) throws CoreException { 1883 String jreAttr = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, (String )null); 1884 IPath containerPath = null; 1885 if (jreAttr == null) { 1886 String type = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, (String )null); 1887 if (type == null) { 1888 IJavaProject proj = getJavaProject(configuration); 1890 if (proj == null) { 1891 containerPath = newDefaultJREContainerPath(); 1892 } else { 1893 return computeJREEntry(proj); 1894 } 1895 } else { 1896 String name = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME, (String )null); 1897 if (name != null) { 1898 containerPath = newDefaultJREContainerPath().append(type).append(name); 1899 } 1900 } 1901 } else { 1902 containerPath = Path.fromPortableString(jreAttr); 1903 } 1904 if (containerPath != null) { 1905 return newRuntimeContainerClasspathEntry(containerPath, IRuntimeClasspathEntry.STANDARD_CLASSES); 1906 } 1907 return null; 1908 } 1909 1910 1921 public static IRuntimeClasspathEntry computeJREEntry(IJavaProject project) throws CoreException { 1922 IClasspathEntry[] rawClasspath = project.getRawClasspath(); 1923 IRuntimeClasspathEntryResolver2 resolver = null; 1924 for (int i = 0; i < rawClasspath.length; i++) { 1925 IClasspathEntry entry = rawClasspath[i]; 1926 switch (entry.getEntryKind()) { 1927 case IClasspathEntry.CPE_VARIABLE: 1928 resolver = getVariableResolver(entry.getPath().segment(0)); 1929 if (resolver != null) { 1930 if (resolver.isVMInstallReference(entry)) { 1931 return newRuntimeClasspathEntry(entry); 1932 } 1933 } 1934 break; 1935 case IClasspathEntry.CPE_CONTAINER: 1936 resolver = getContainerResolver(entry.getPath().segment(0)); 1937 if (resolver != null) { 1938 if (resolver.isVMInstallReference(entry)) { 1939 IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), project); 1940 if (container != null) { 1941 switch (container.getKind()) { 1942 case IClasspathContainer.K_APPLICATION: 1943 break; 1944 case IClasspathContainer.K_DEFAULT_SYSTEM: 1945 return newRuntimeContainerClasspathEntry(entry.getPath(), IRuntimeClasspathEntry.STANDARD_CLASSES); 1946 case IClasspathContainer.K_SYSTEM: 1947 return newRuntimeContainerClasspathEntry(entry.getPath(), IRuntimeClasspathEntry.BOOTSTRAP_CLASSES); 1948 } 1949 } 1950 } 1951 } 1952 break; 1953 } 1954 1955 } 1956 return null; 1957 } 1958 1959 1966 public static boolean isVMInstallReference(IRuntimeClasspathEntry entry) { 1967 IClasspathEntry classpathEntry = entry.getClasspathEntry(); 1968 if (classpathEntry != null) { 1969 switch (classpathEntry.getEntryKind()) { 1970 case IClasspathEntry.CPE_VARIABLE: 1971 IRuntimeClasspathEntryResolver2 resolver = getVariableResolver(classpathEntry.getPath().segment(0)); 1972 if (resolver != null) { 1973 return resolver.isVMInstallReference(classpathEntry); 1974 } 1975 break; 1976 case IClasspathEntry.CPE_CONTAINER: 1977 resolver = getContainerResolver(classpathEntry.getPath().segment(0)); 1978 if (resolver != null) { 1979 return resolver.isVMInstallReference(classpathEntry); 1980 } 1981 break; 1982 } 1983 } 1984 return false; 1985 } 1986 1987 1995 public static IVMConnector getVMConnector(String id) { 1996 return LaunchingPlugin.getDefault().getVMConnector(id); 1997 } 1998 1999 2005 public static IVMConnector[] getVMConnectors() { 2006 return LaunchingPlugin.getDefault().getVMConnectors(); 2007 } 2008 2009 2015 public static Preferences getPreferences() { 2016 return LaunchingPlugin.getDefault().getPluginPreferences(); 2017 } 2018 2019 2024 public static void savePreferences() { 2025 LaunchingPlugin.getDefault().savePluginPreferences(); 2026 } 2027 2028 2035 public static void addVariableResolver(IRuntimeClasspathEntryResolver resolver, String variableName) { 2036 Map map = getVariableResolvers(); 2037 map.put(variableName, resolver); 2038 } 2039 2040 2047 public static void addContainerResolver(IRuntimeClasspathEntryResolver resolver, String containerIdentifier) { 2048 Map map = getContainerResolvers(); 2049 map.put(containerIdentifier, resolver); 2050 } 2051 2052 2055 private static Map getVariableResolvers() { 2056 if (fgVariableResolvers == null) { 2057 initializeResolvers(); 2058 } 2059 return fgVariableResolvers; 2060 } 2061 2062 2065 private static Map getContainerResolvers() { 2066 if (fgContainerResolvers == null) { 2067 initializeResolvers(); 2068 } 2069 return fgContainerResolvers; 2070 } 2071 2072 2075 private static Map getEntryResolvers() { 2076 if (fgRuntimeClasspathEntryResolvers == null) { 2077 initializeResolvers(); 2078 } 2079 return fgRuntimeClasspathEntryResolvers; 2080 } 2081 2082 2085 private static void initializeResolvers() { 2086 IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(LaunchingPlugin.ID_PLUGIN, EXTENSION_POINT_RUNTIME_CLASSPATH_ENTRY_RESOLVERS); 2087 IConfigurationElement[] extensions = point.getConfigurationElements(); 2088 fgVariableResolvers = new HashMap (extensions.length); 2089 fgContainerResolvers = new HashMap (extensions.length); 2090 fgRuntimeClasspathEntryResolvers = new HashMap (extensions.length); 2091 for (int i = 0; i < extensions.length; i++) { 2092 RuntimeClasspathEntryResolver res = new RuntimeClasspathEntryResolver(extensions[i]); 2093 String variable = res.getVariableName(); 2094 String container = res.getContainerId(); 2095 String entryId = res.getRuntimeClasspathEntryId(); 2096 if (variable != null) { 2097 fgVariableResolvers.put(variable, res); 2098 } 2099 if (container != null) { 2100 fgContainerResolvers.put(container, res); 2101 } 2102 if (entryId != null) { 2103 fgRuntimeClasspathEntryResolvers.put(entryId, res); 2104 } 2105 } 2106 } 2107 2108 2111 private static Map getClasspathProviders() { 2112 if (fgPathProviders == null) { 2113 initializeProviders(); 2114 } 2115 return fgPathProviders; 2116 } 2117 2118 2121 private static void initializeProviders() { 2122 IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(LaunchingPlugin.ID_PLUGIN, EXTENSION_POINT_RUNTIME_CLASSPATH_PROVIDERS); 2123 IConfigurationElement[] extensions = point.getConfigurationElements(); 2124 fgPathProviders = new HashMap (extensions.length); 2125 for (int i = 0; i < extensions.length; i++) { 2126 RuntimeClasspathProvider res = new RuntimeClasspathProvider(extensions[i]); 2127 fgPathProviders.put(res.getIdentifier(), res); 2128 } 2129 } 2130 2131 2139 private static IRuntimeClasspathEntryResolver2 getVariableResolver(String variableName) { 2140 return (IRuntimeClasspathEntryResolver2)getVariableResolvers().get(variableName); 2141 } 2142 2143 2151 private static IRuntimeClasspathEntryResolver2 getContainerResolver(String containerId) { 2152 return (IRuntimeClasspathEntryResolver2)getContainerResolvers().get(containerId); 2153 } 2154 2155 2162 private static IRuntimeClasspathEntryResolver getContributedResolver(String typeId) { 2163 IRuntimeClasspathEntryResolver resolver = (IRuntimeClasspathEntryResolver)getEntryResolvers().get(typeId); 2164 if (resolver == null) { 2165 return new DefaultEntryResolver(); 2166 } 2167 return resolver; 2168 } 2169 2170 2177 public static void addVMInstallChangedListener(IVMInstallChangedListener listener) { 2178 fgVMListeners.add(listener); 2179 } 2180 2181 2188 public static void removeVMInstallChangedListener(IVMInstallChangedListener listener) { 2189 fgVMListeners.remove(listener); 2190 } 2191 2192 2197 private static void notifyDefaultVMChanged(IVMInstall previous, IVMInstall current) { 2198 Object [] listeners = fgVMListeners.getListeners(); 2199 for (int i = 0; i < listeners.length; i++) { 2200 IVMInstallChangedListener listener = (IVMInstallChangedListener)listeners[i]; 2201 listener.defaultVMInstallChanged(previous, current); 2202 } 2203 } 2204 2205 2211 public static void fireVMChanged(PropertyChangeEvent event) { 2212 Object [] listeners = fgVMListeners.getListeners(); 2213 for (int i = 0; i < listeners.length; i++) { 2214 IVMInstallChangedListener listener = (IVMInstallChangedListener)listeners[i]; 2215 listener.vmChanged(event); 2216 } 2217 } 2218 2219 2225 public static void fireVMAdded(IVMInstall vm) { 2226 if (!fgInitializingVMs) { 2227 Object [] listeners = fgVMListeners.getListeners(); 2228 for (int i = 0; i < listeners.length; i++) { 2229 IVMInstallChangedListener listener = (IVMInstallChangedListener)listeners[i]; 2230 listener.vmAdded(vm); 2231 } 2232 } 2233 } 2234 2235 2241 public static void fireVMRemoved(IVMInstall vm) { 2242 Object [] listeners = fgVMListeners.getListeners(); 2243 for (int i = 0; i < listeners.length; i++) { 2244 IVMInstallChangedListener listener = (IVMInstallChangedListener)listeners[i]; 2245 listener.vmRemoved(vm); 2246 } 2247 } 2248 2249 2258 public static String getProjectOutputDirectory(ILaunchConfiguration config) { 2259 try { 2260 if (config != null) { 2261 IJavaProject javaProject = JavaRuntime.getJavaProject(config); 2262 if (javaProject != null) { 2263 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 2264 IPath outputLocation = javaProject.getOutputLocation(); 2265 IResource resource = root.findMember(outputLocation); 2266 if (resource != null) { 2267 IPath path = resource.getFullPath(); 2268 if (path != null) { 2269 return path.makeRelative().toString(); 2270 } 2271 } 2272 } 2273 } 2274 } catch (CoreException ce) { 2275 } 2276 return null; 2277 } 2278 2279 2290 public static ISourceContainer[] getSourceContainers(IRuntimeClasspathEntry[] entries) { 2291 return JavaSourceLookupUtil.translate(entries); 2292 } 2293 2294 2309 public static String [] computeJavaLibraryPath(IJavaProject project, boolean requiredProjects) throws CoreException { 2310 Set visited = new HashSet (); 2311 List entries = new ArrayList (); 2312 gatherJavaLibraryPathEntries(project, requiredProjects, visited, entries); 2313 List resolved = new ArrayList (entries.size()); 2314 Iterator iterator = entries.iterator(); 2315 IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager(); 2316 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 2317 while (iterator.hasNext()) { 2318 String entry = (String ) iterator.next(); 2319 String resolvedEntry = manager.performStringSubstitution(entry); 2320 IPath path = new Path(resolvedEntry); 2321 if (path.isAbsolute()) { 2322 File file = path.toFile(); 2323 resolved.add(file.getAbsolutePath()); 2324 } else { 2325 IResource resource = root.findMember(path); 2326 if (resource != null) { 2327 IPath location = resource.getLocation(); 2328 if (location != null) { 2329 resolved.add(location.toFile().getAbsolutePath()); 2330 } 2331 } 2332 } 2333 } 2334 return (String [])resolved.toArray(new String [resolved.size()]); 2335 } 2336 2337 2348 private static void gatherJavaLibraryPathEntries(IJavaProject project, boolean requiredProjects, Set visited, List entries) throws CoreException { 2349 if (visited.contains(project)) { 2350 return; 2351 } 2352 visited.add(project); 2353 IClasspathEntry[] rawClasspath = project.getRawClasspath(); 2354 IClasspathEntry[] required = processJavaLibraryPathEntries(project, requiredProjects, rawClasspath, entries); 2355 if (required != null) { 2356 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 2357 for (int i = 0; i < required.length; i++) { 2358 IClasspathEntry entry = required[i]; 2359 String projectName = entry.getPath().segment(0); 2360 IProject p = root.getProject(projectName); 2361 if (p.exists()) { 2362 IJavaProject requiredProject = JavaCore.create(p); 2363 if (requiredProject != null) { 2364 gatherJavaLibraryPathEntries(requiredProject, requiredProjects, visited, entries); 2365 } 2366 } 2367 } 2368 } 2369 } 2370 2371 2384 private static IClasspathEntry[] processJavaLibraryPathEntries(IJavaProject project, boolean collectRequired, IClasspathEntry[] classpathEntries, List entries) throws CoreException { 2385 List req = null; 2386 for (int i = 0; i < classpathEntries.length; i++) { 2387 IClasspathEntry entry = classpathEntries[i]; 2388 IClasspathAttribute[] extraAttributes = entry.getExtraAttributes(); 2389 for (int j = 0; j < extraAttributes.length; j++) { 2390 String [] paths = getLibraryPaths(extraAttributes[j]); 2391 if (paths != null) { 2392 for (int k = 0; k < paths.length; k++) { 2393 entries.add(paths[k]); 2394 } 2395 } 2396 } 2397 if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { 2398 IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), project); 2399 if (container != null) { 2400 IClasspathEntry[] requiredProjects = processJavaLibraryPathEntries(project, collectRequired, container.getClasspathEntries(), entries); 2401 if (requiredProjects != null) { 2402 if (req == null) { 2403 req = new ArrayList (); 2404 } 2405 for (int j = 0; j < requiredProjects.length; j++) { 2406 req.add(requiredProjects[j]); 2407 } 2408 } 2409 } 2410 } else if (collectRequired && entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) { 2411 if (req == null) { 2412 req = new ArrayList (); 2413 } 2414 req.add(entry); 2415 } 2416 } 2417 if (req != null) { 2418 return (IClasspathEntry[]) req.toArray(new IClasspathEntry[req.size()]); 2419 } 2420 return null; 2421 } 2422 2423 2441 public static IClasspathAttribute newLibraryPathsAttribute(String [] paths) { 2442 StringBuffer value = new StringBuffer (); 2443 for (int i = 0; i < paths.length; i++) { 2444 value.append(paths[i]); 2445 if (i < (paths.length - 1)) { 2446 value.append("|"); } 2448 } 2449 return JavaCore.newClasspathAttribute(CLASSPATH_ATTR_LIBRARY_PATH_ENTRY, value.toString()); 2450 } 2451 2452 2473 public static String [] getLibraryPaths(IClasspathAttribute attribute) { 2474 if (CLASSPATH_ATTR_LIBRARY_PATH_ENTRY.equals(attribute.getName())) { 2475 String value = attribute.getValue(); 2476 return value.split("\\|"); } 2478 return null; 2479 } 2480 2481 2487 public static IExecutionEnvironmentsManager getExecutionEnvironmentsManager() { 2488 return EnvironmentsManager.getDefault(); 2489 } 2490 2491 2497 private static void initializeVMs() { 2498 VMDefinitionsContainer vmDefs = null; 2499 boolean setPref = false; 2500 boolean updateCompliance = false; 2501 synchronized (fgVMLock) { 2502 if (fgVMTypes == null) { 2503 try { 2504 fgInitializingVMs = true; 2505 initializeVMTypeExtensions(); 2507 try { 2508 vmDefs = new VMDefinitionsContainer(); 2509 setPref = addPersistedVMs(vmDefs); 2511 2512 if (vmDefs.getValidVMList().isEmpty()) { 2514 VMListener listener = new VMListener(); 2517 addVMInstallChangedListener(listener); 2518 setPref = true; 2519 VMStandin runtime = detectEclipseRuntime(); 2520 removeVMInstallChangedListener(listener); 2521 if (!listener.isChanged()) { 2522 if (runtime != null) { 2523 updateCompliance = true; 2524 vmDefs.addVM(runtime); 2525 vmDefs.setDefaultVMInstallCompositeID(getCompositeIdFromVM(runtime)); 2526 } 2527 } else { 2528 addPersistedVMs(vmDefs); 2530 vmDefs.setDefaultVMInstallCompositeID(fgDefaultVMId); 2531 updateCompliance = fgDefaultVMId != null; 2532 } 2533 } 2534 addVMExtensions(vmDefs); 2536 String defId = vmDefs.getDefaultVMInstallCompositeID(); 2538 boolean validDef = false; 2539 if (defId != null) { 2540 Iterator iterator = vmDefs.getValidVMList().iterator(); 2541 while (iterator.hasNext()) { 2542 IVMInstall vm = (IVMInstall) iterator.next(); 2543 if (getCompositeIdFromVM(vm).equals(defId)) { 2544 validDef = true; 2545 break; 2546 } 2547 } 2548 } 2549 if (!validDef) { 2550 setPref = true; 2552 List list = vmDefs.getValidVMList(); 2553 if (!list.isEmpty()) { 2554 IVMInstall vm = (IVMInstall) list.get(0); 2555 vmDefs.setDefaultVMInstallCompositeID(getCompositeIdFromVM(vm)); 2556 } 2557 } 2558 fgDefaultVMId = vmDefs.getDefaultVMInstallCompositeID(); 2559 fgDefaultVMConnectorId = vmDefs.getDefaultVMInstallConnectorTypeID(); 2560 2561 List vmList = vmDefs.getValidVMList(); 2563 Iterator vmListIterator = vmList.iterator(); 2564 while (vmListIterator.hasNext()) { 2565 VMStandin vmStandin = (VMStandin) vmListIterator.next(); 2566 vmStandin.convertToRealVM(); 2567 } 2568 2569 2570 } catch (IOException e) { 2571 LaunchingPlugin.log(e); 2572 } 2573 } finally { 2574 fgInitializingVMs = false; 2575 } 2576 } 2577 } 2578 if (vmDefs != null) { 2579 IVMInstallType[] installTypes = getVMInstallTypes(); 2581 for (int i = 0; i < installTypes.length; i++) { 2582 IVMInstallType type = installTypes[i]; 2583 IVMInstall[] installs = type.getVMInstalls(); 2584 for (int j = 0; j < installs.length; j++) { 2585 fireVMAdded(installs[j]); 2586 } 2587 } 2588 2589 if (setPref) { 2591 try { 2592 String xml = vmDefs.getAsXML(); 2593 LaunchingPlugin.getDefault().getPluginPreferences().setValue(PREF_VM_XML, xml); 2594 } catch (ParserConfigurationException e) { 2595 LaunchingPlugin.log(e); 2596 } catch (IOException e) { 2597 LaunchingPlugin.log(e); 2598 } catch (TransformerException e) { 2599 LaunchingPlugin.log(e); 2600 } 2601 2602 } 2603 2604 if (updateCompliance) { 2606 updateCompliance(getDefaultVMInstall()); 2607 } 2608 } 2609 } 2610 2611 2616 private static void updateCompliance(IVMInstall vm) { 2617 if (vm instanceof IVMInstall2) { 2618 String javaVersion = ((IVMInstall2)vm).getJavaVersion(); 2619 if (javaVersion != null) { 2620 String compliance = null; 2621 if (javaVersion.startsWith(JavaCore.VERSION_1_5)) { 2622 compliance = JavaCore.VERSION_1_5; 2623 } else if (javaVersion.startsWith(JavaCore.VERSION_1_6)) { 2624 compliance = JavaCore.VERSION_1_6; 2625 } else if (javaVersion.startsWith(JavaCore.VERSION_1_7)) { 2626 compliance = JavaCore.VERSION_1_7; 2627 } 2628 if (compliance != null) { 2629 Hashtable defaultOptions = JavaCore.getDefaultOptions(); 2630 Hashtable options = JavaCore.getOptions(); 2631 boolean isDefault = 2632 equals(JavaCore.COMPILER_COMPLIANCE, defaultOptions, options) && 2633 equals(JavaCore.COMPILER_SOURCE, defaultOptions, options) && 2634 equals(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, defaultOptions, options) && 2635 equals(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, defaultOptions, options) && 2636 equals(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, defaultOptions, options); 2637 if (isDefault) { 2640 JavaCore.setComplianceOptions(compliance, options); 2641 JavaCore.setOptions(options); 2642 } 2643 } 2644 } 2645 } 2646 } 2647 2648} 2649 | Popular Tags |