1 11 package org.eclipse.jdt.internal.launching; 12 13 14 import java.io.File ; 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.Path; 22 import org.eclipse.core.runtime.Status; 23 import org.eclipse.jdt.core.ClasspathContainerInitializer; 24 import org.eclipse.jdt.core.IClasspathAttribute; 25 import org.eclipse.jdt.core.IClasspathContainer; 26 import org.eclipse.jdt.core.IClasspathEntry; 27 import org.eclipse.jdt.core.IJavaProject; 28 import org.eclipse.jdt.core.JavaCore; 29 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 30 import org.eclipse.jdt.launching.IVMInstall; 31 import org.eclipse.jdt.launching.IVMInstallType; 32 import org.eclipse.jdt.launching.JavaRuntime; 33 import org.eclipse.jdt.launching.LibraryLocation; 34 import org.eclipse.jdt.launching.VMStandin; 35 import org.eclipse.jdt.launching.environments.IExecutionEnvironment; 36 import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager; 37 38 import com.ibm.icu.text.MessageFormat; 39 40 43 public class JREContainerInitializer extends ClasspathContainerInitializer { 44 45 48 public void initialize(IPath containerPath, IJavaProject project) throws CoreException { 49 if (JREContainer.DEBUG_JRE_CONTAINER) { 50 System.out.println("<JRE_CONTAINER> initialize()"); System.out.println("\tPath: " + containerPath.toString()); System.out.println("\tProj: " + project.getProject().getName()); } 54 int size = containerPath.segmentCount(); 55 if (size > 0) { 56 if (containerPath.segment(0).equals(JavaRuntime.JRE_CONTAINER)) { 57 IVMInstall vm = resolveVM(containerPath); 58 JREContainer container = null; 59 if (vm != null) { 60 if (JREContainer.DEBUG_JRE_CONTAINER) { 61 System.out.println("\tResolved VM: " + vm.getName()); } 63 container = new JREContainer(vm, containerPath, project); 64 } else { 65 if (JREContainer.DEBUG_JRE_CONTAINER) { 66 System.out.println("\t*** FAILED RESOLVE VM ***"); } 68 } 69 JavaCore.setClasspathContainer(containerPath, new IJavaProject[] {project}, new IClasspathContainer[] {container}, null); 70 } else { 71 if (JREContainer.DEBUG_JRE_CONTAINER) { 72 System.out.println("\t*** INVALID JRE CONTAINER PATH ***"); } 74 } 75 } else { 76 if (JREContainer.DEBUG_JRE_CONTAINER) { 77 System.out.println("\t*** NO SEGMENTS IN CONTAINER PATH ***"); } 79 } 80 } 81 82 86 public static IVMInstall resolveVM(IPath containerPath) { 87 IVMInstall vm = null; 88 if (containerPath.segmentCount() > 1) { 89 String id = getExecutionEnvironmentId(containerPath); 91 if (id != null) { 92 if (JREContainer.DEBUG_JRE_CONTAINER) { 93 System.out.println("<JRE_CONTAINER> resolveVM(IPath)"); System.out.println("\tEE: " + id); } 96 IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager(); 97 IExecutionEnvironment environment = manager.getEnvironment(id); 98 if (environment != null) { 99 vm = resolveVM(environment); 100 } else { 101 if (JREContainer.DEBUG_JRE_CONTAINER) { 102 System.out.println("\t*** NO ENVIRONMENT ***"); } 104 } 105 } else { 106 String vmTypeId = getVMTypeId(containerPath); 107 String vmName = getVMName(containerPath); 108 IVMInstallType vmType = JavaRuntime.getVMInstallType(vmTypeId); 109 if (vmType != null) { 110 vm = vmType.findVMInstallByName(vmName); 111 } 112 } 113 } else { 114 vm = JavaRuntime.getDefaultVMInstall(); 116 } 117 return vm; 118 } 119 120 128 public static IVMInstall resolveVM(IExecutionEnvironment environment) { 129 if (JREContainer.DEBUG_JRE_CONTAINER) { 130 System.out.println("<JRE_CONTAINER> resolveVM(IExecutionEnvironment)"); } 132 IVMInstall vm = environment.getDefaultVM(); 133 if (vm == null) { 134 IVMInstall[] installs = environment.getCompatibleVMs(); 135 if (installs.length == 0 && JREContainer.DEBUG_JRE_CONTAINER) { 137 System.out.println("\t*** NO COMPATIBLE VMS ***"); } 139 for (int i = 0; i < installs.length; i++) { 140 IVMInstall install = installs[i]; 141 if (environment.isStrictlyCompatible(install)) { 142 vm = install; 143 if (installs.length == 0 && JREContainer.DEBUG_JRE_CONTAINER) { 144 System.out.println("\tPerfect Match: " + vm.getName()); } 146 break; 147 } 148 } 149 if (vm == null && installs.length > 0) { 151 vm = installs[0]; 152 if (installs.length == 0 && JREContainer.DEBUG_JRE_CONTAINER) { 153 System.out.println("\tFirst Match: " + vm.getName()); } 155 } 156 } else { 157 if (JREContainer.DEBUG_JRE_CONTAINER) { 158 System.out.println("\tUser Default VM: " + vm.getName()); } 160 } 161 return vm; 162 } 163 164 171 public static String getExecutionEnvironmentId(IPath path) { 172 String name = getVMName(path); 173 if (name != null) { 174 name = decodeEnvironmentId(name); 175 IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager(); 176 IExecutionEnvironment environment = manager.getEnvironment(name); 177 if (environment != null) { 178 return environment.getId(); 179 } 180 } 181 return null; 182 } 183 184 190 public static boolean isExecutionEnvironment(IPath path) { 191 return getExecutionEnvironmentId(path) != null; 192 } 193 194 200 public static String encodeEnvironmentId(String id) { 201 return id.replace('/', '%'); 202 } 203 204 public static String decodeEnvironmentId(String id) { 205 return id.replace('%', '/'); 206 } 207 208 213 public static String getVMTypeId(IPath path) { 214 return path.segment(1); 215 } 216 217 222 public static String getVMName(IPath path) { 223 return path.segment(2); 224 } 225 226 231 public boolean canUpdateClasspathContainer(IPath containerPath, IJavaProject project) { 232 if (containerPath != null && containerPath.segmentCount() > 0) { 233 if (JavaRuntime.JRE_CONTAINER.equals(containerPath.segment(0))) { 234 return resolveVM(containerPath) != null; 235 } 236 } 237 return false; 238 } 239 240 private static final IStatus READ_ONLY= new Status(IStatus.ERROR, LaunchingPlugin.ID_PLUGIN, ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY, new String (), null); 241 private static final IStatus NOT_SUPPORTED= new Status(IStatus.ERROR, LaunchingPlugin.ID_PLUGIN, ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED, new String (), null); 242 243 246 public IStatus getAccessRulesStatus(IPath containerPath, IJavaProject project) { 247 return READ_ONLY; 248 } 249 250 253 public IStatus getSourceAttachmentStatus(IPath containerPath, IJavaProject project) { 254 return Status.OK_STATUS; 255 } 256 257 260 public IStatus getAttributeStatus(IPath containerPath, IJavaProject project, String attributeKey) { 261 if (attributeKey.equals(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME)) { 262 return Status.OK_STATUS; 263 } 264 if (attributeKey.equals(JavaRuntime.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY)) { 265 return Status.OK_STATUS; 266 } 267 return NOT_SUPPORTED; 268 } 269 270 273 public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject project, IClasspathContainer containerSuggestion) throws CoreException { 274 IVMInstall vm = resolveVM(containerPath); 275 if (vm == null) { 276 IStatus status = new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), IJavaLaunchConfigurationConstants.ERR_VM_INSTALL_DOES_NOT_EXIST, MessageFormat.format(LaunchingMessages.JREContainerInitializer_JRE_referenced_by_classpath_container__0__does_not_exist__1, new String []{containerPath.toString()}), null); 277 throw new CoreException(status); 278 } 279 281 IClasspathEntry[] entries = containerSuggestion.getClasspathEntries(); 282 LibraryLocation[] libs = new LibraryLocation[entries.length]; 283 for (int i = 0; i < entries.length; i++) { 284 IClasspathEntry entry = entries[i]; 285 if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { 286 IPath path = entry.getPath(); 287 File lib = path.toFile(); 288 if (lib.exists() && lib.isFile()) { 289 IPath srcPath = entry.getSourceAttachmentPath(); 290 if (srcPath == null) { 291 srcPath = Path.EMPTY; 292 } 293 IPath rootPath = entry.getSourceAttachmentRootPath(); 294 if (rootPath == null) { 295 rootPath = Path.EMPTY; 296 } 297 URL javadocLocation = null; 298 IClasspathAttribute[] extraAttributes = entry.getExtraAttributes(); 299 for (int j = 0; j < extraAttributes.length; j++) { 300 IClasspathAttribute attribute = extraAttributes[j]; 301 if (attribute.getName().equals(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME)) { 302 String url = attribute.getValue(); 303 if (url != null && url.trim().length() > 0) { 304 try { 305 javadocLocation = new URL (url); 306 } catch (MalformedURLException e) { 307 LaunchingPlugin.log(e); 308 } 309 } 310 } 311 } 312 libs[i] = new LibraryLocation(path, srcPath, rootPath, javadocLocation); 313 } else { 314 IStatus status = new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR, MessageFormat.format(LaunchingMessages.JREContainerInitializer_Classpath_entry__0__does_not_refer_to_an_existing_library__2, new String []{entry.getPath().toString()}), null); 315 throw new CoreException(status); 316 } 317 } else { 318 IStatus status = new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR, MessageFormat.format(LaunchingMessages.JREContainerInitializer_Classpath_entry__0__does_not_refer_to_a_library__3, new String []{entry.getPath().toString()}), null); 319 throw new CoreException(status); 320 } 321 } 322 VMStandin standin = new VMStandin(vm); 323 standin.setLibraryLocations(libs); 324 standin.convertToRealVM(); 325 JavaRuntime.saveVMConfiguration(); 326 } 327 328 331 public String getDescription(IPath containerPath, IJavaProject project) { 332 String tag = getExecutionEnvironmentId(containerPath); 333 if (tag == null && containerPath.segmentCount() > 2) { 334 tag = getVMName(containerPath); 335 } 336 if (tag != null) { 337 return MessageFormat.format(LaunchingMessages.JREContainer_JRE_System_Library_1, new String []{tag}); 338 } 339 return LaunchingMessages.JREContainerInitializer_Default_System_Library_1; 340 } 341 } 342 | Popular Tags |