1 11 package org.eclipse.jdt.internal.launching; 12 13 14 import java.io.BufferedInputStream ; 15 import java.io.File ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.net.MalformedURLException ; 19 import java.net.URL ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import javax.xml.parsers.DocumentBuilder ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.parsers.ParserConfigurationException ; 30 import javax.xml.transform.TransformerException ; 31 32 import org.eclipse.core.runtime.IPath; 33 import org.eclipse.core.runtime.Path; 34 import org.eclipse.jdt.launching.IVMInstall; 35 import org.eclipse.jdt.launching.IVMInstall2; 36 import org.eclipse.jdt.launching.IVMInstallType; 37 import org.eclipse.jdt.launching.JavaRuntime; 38 import org.eclipse.jdt.launching.LibraryLocation; 39 import org.eclipse.jdt.launching.VMStandin; 40 import org.w3c.dom.Document ; 41 import org.w3c.dom.Element ; 42 import org.w3c.dom.Node ; 43 import org.w3c.dom.NodeList ; 44 import org.xml.sax.InputSource ; 45 import org.xml.sax.SAXException ; 46 import org.xml.sax.helpers.DefaultHandler ; 47 48 65 public class VMDefinitionsContainer { 66 67 70 private Map fVMTypeToVMMap; 71 72 75 private List fVMList; 76 77 80 private List fInvalidVMList; 81 82 86 private String fDefaultVMInstallCompositeID; 87 88 91 private String fDefaultVMInstallConnectorTypeID; 92 93 96 public VMDefinitionsContainer() { 97 fVMTypeToVMMap = new HashMap (10); 98 fInvalidVMList = new ArrayList (10); 99 fVMList = new ArrayList (10); 100 } 101 102 112 public void addVM(IVMInstall vm) { 113 if (!fVMList.contains(vm)) { 114 IVMInstallType vmInstallType = vm.getVMInstallType(); 115 List vmList = (List ) fVMTypeToVMMap.get(vmInstallType); 116 if (vmList == null) { 117 vmList = new ArrayList (3); 118 fVMTypeToVMMap.put(vmInstallType, vmList); 119 } 120 vmList.add(vm); 121 File installLocation = vm.getInstallLocation(); 122 if (installLocation == null || !vmInstallType.validateInstallLocation(installLocation).isOK()) { 123 fInvalidVMList.add(vm); 124 } 125 fVMList.add(vm); 126 } 127 } 128 129 139 public void addVMList(List vmList) { 140 Iterator iterator = vmList.iterator(); 141 while (iterator.hasNext()) { 142 IVMInstall vm = (IVMInstall) iterator.next(); 143 addVM(vm); 144 } 145 } 146 147 154 public Map getVMTypeToVMMap() { 155 return fVMTypeToVMMap; 156 } 157 158 165 public List getVMList() { 166 return fVMList; 167 } 168 169 175 public List getValidVMList() { 176 List vms = getVMList(); 177 List resultList = new ArrayList (vms.size()); 178 resultList.addAll(vms); 179 resultList.removeAll(fInvalidVMList); 180 return resultList; 181 } 182 183 191 public String getDefaultVMInstallCompositeID(){ 192 return fDefaultVMInstallCompositeID; 193 } 194 195 203 public void setDefaultVMInstallCompositeID(String id){ 204 fDefaultVMInstallCompositeID = id; 205 } 206 207 212 public String getDefaultVMInstallConnectorTypeID() { 213 return fDefaultVMInstallConnectorTypeID; 214 } 215 216 221 public void setDefaultVMInstallConnectorTypeID(String id){ 222 fDefaultVMInstallConnectorTypeID = id; 223 } 224 225 238 public String getAsXML() throws ParserConfigurationException , IOException , TransformerException { 239 240 Document doc = LaunchingPlugin.getDocument(); 242 Element config = doc.createElement("vmSettings"); doc.appendChild(config); 244 245 if (getDefaultVMInstallCompositeID() != null) { 247 config.setAttribute("defaultVM", getDefaultVMInstallCompositeID()); } 249 250 if (getDefaultVMInstallConnectorTypeID() != null) { 252 config.setAttribute("defaultVMConnector", getDefaultVMInstallConnectorTypeID()); } 254 255 Set vmInstallTypeSet = getVMTypeToVMMap().keySet(); 257 Iterator keyIterator = vmInstallTypeSet.iterator(); 258 while (keyIterator.hasNext()) { 259 IVMInstallType vmInstallType = (IVMInstallType) keyIterator.next(); 260 Element vmTypeElement = vmTypeAsElement(doc, vmInstallType); 261 config.appendChild(vmTypeElement); 262 } 263 264 return LaunchingPlugin.serializeDocument(doc); 266 } 267 268 271 private Element vmTypeAsElement(Document doc, IVMInstallType vmType) { 272 273 Element element= doc.createElement("vmType"); element.setAttribute("id", vmType.getId()); 277 List vmList = (List ) getVMTypeToVMMap().get(vmType); 279 Iterator vmIterator = vmList.iterator(); 280 while (vmIterator.hasNext()) { 281 IVMInstall vm = (IVMInstall) vmIterator.next(); 282 Element vmElement = vmAsElement(doc, vm); 283 element.appendChild(vmElement); 284 } 285 286 return element; 287 } 288 289 292 private Element vmAsElement(Document doc, IVMInstall vm) { 293 294 Element element= doc.createElement("vm"); element.setAttribute("id", vm.getId()); element.setAttribute("name", vm.getName()); 299 String installPath= ""; File installLocation= vm.getInstallLocation(); 302 if (installLocation != null) { 303 installPath= installLocation.getAbsolutePath(); 304 } 305 element.setAttribute("path", installPath); 307 LibraryLocation[] libraryLocations= vm.getLibraryLocations(); 309 if (libraryLocations != null) { 310 Element libLocationElement = libraryLocationsAsElement(doc, libraryLocations); 311 element.appendChild(libLocationElement); 312 } 313 314 URL url = vm.getJavadocLocation(); 316 if (url != null) { 317 element.setAttribute("javadocURL", url.toExternalForm()); } 319 320 if (vm instanceof IVMInstall2) { 321 String vmArgs = ((IVMInstall2)vm).getVMArgs(); 322 if (vmArgs != null && vmArgs.length() > 0) { 323 element.setAttribute("vmargs", vmArgs); } 325 } else { 326 String [] vmArgs = vm.getVMArguments(); 327 if (vmArgs != null && vmArgs.length > 0) { 328 StringBuffer buffer = new StringBuffer (); 329 for (int i = 0; i < vmArgs.length; i++) { 330 buffer.append(vmArgs[i] + " "); } 332 element.setAttribute("vmargs", buffer.toString()); } 334 } 335 336 return element; 337 } 338 339 343 private static Element libraryLocationsAsElement(Document doc, LibraryLocation[] locations) { 344 Element root = doc.createElement("libraryLocations"); for (int i = 0; i < locations.length; i++) { 346 Element element = doc.createElement("libraryLocation"); element.setAttribute("jreJar", locations[i].getSystemLibraryPath().toString()); element.setAttribute("jreSrc", locations[i].getSystemLibrarySourcePath().toString()); 350 IPath packageRootPath = locations[i].getPackageRootPath(); 351 if (packageRootPath != null) { 352 element.setAttribute("pkgRoot", packageRootPath.toString()); } 354 355 URL url= locations[i].getJavadocLocation(); 356 if (url != null) { 357 element.setAttribute("jreJavadoc", url.toExternalForm()); } 359 root.appendChild(element); 360 } 361 return root; 362 } 363 364 public static VMDefinitionsContainer parseXMLIntoContainer(InputStream inputStream) throws IOException { 365 VMDefinitionsContainer container = new VMDefinitionsContainer(); 366 parseXMLIntoContainer(inputStream, container); 367 return container; 368 } 369 370 394 public static void parseXMLIntoContainer(InputStream inputStream, VMDefinitionsContainer container) throws IOException { 395 396 InputStream stream= new BufferedInputStream (inputStream); 398 399 Element config= null; 401 try { 402 DocumentBuilder parser= DocumentBuilderFactory.newInstance().newDocumentBuilder(); 403 parser.setErrorHandler(new DefaultHandler ()); 404 config = parser.parse(new InputSource (stream)).getDocumentElement(); 405 } catch (SAXException e) { 406 throw new IOException (LaunchingMessages.JavaRuntime_badFormat); 407 } catch (ParserConfigurationException e) { 408 stream.close(); 409 throw new IOException (LaunchingMessages.JavaRuntime_badFormat); 410 } finally { 411 stream.close(); 412 } 413 414 if (!config.getNodeName().equalsIgnoreCase("vmSettings")) { throw new IOException (LaunchingMessages.JavaRuntime_badFormat); 417 } 418 419 container.setDefaultVMInstallCompositeID(config.getAttribute("defaultVM")); container.setDefaultVMInstallConnectorTypeID(config.getAttribute("defaultVMConnector")); 423 NodeList list = config.getChildNodes(); 425 int length = list.getLength(); 426 for (int i = 0; i < length; ++i) { 427 Node node = list.item(i); 428 short type = node.getNodeType(); 429 if (type == Node.ELEMENT_NODE) { 430 Element vmTypeElement = (Element ) node; 431 if (vmTypeElement.getNodeName().equalsIgnoreCase("vmType")) { populateVMTypes(vmTypeElement, container); 433 } 434 } 435 } 436 } 437 438 442 private static void populateVMTypes(Element vmTypeElement, VMDefinitionsContainer container) { 443 444 String id = vmTypeElement.getAttribute("id"); IVMInstallType vmType= JavaRuntime.getVMInstallType(id); 447 if (vmType != null) { 448 NodeList vmNodeList = vmTypeElement.getElementsByTagName("vm"); for (int i = 0; i < vmNodeList.getLength(); ++i) { 451 populateVMForType(vmType, (Element ) vmNodeList.item(i), container); 452 } 453 } else { 454 LaunchingPlugin.log("VM type element with unknown id."); } 456 } 457 458 462 private static void populateVMForType(IVMInstallType vmType, Element vmElement, VMDefinitionsContainer container) { 463 String id= vmElement.getAttribute("id"); if (id != null) { 465 466 String installPath= vmElement.getAttribute("path"); if (installPath == null) { 469 return; 470 } 471 472 VMStandin vmStandin = new VMStandin(vmType, id); 474 vmStandin.setName(vmElement.getAttribute("name")); File installLocation= new File (installPath); 476 vmStandin.setInstallLocation(installLocation); 477 container.addVM(vmStandin); 478 479 NodeList list = vmElement.getChildNodes(); 482 int length = list.getLength(); 483 for (int i = 0; i < length; ++i) { 484 Node node = list.item(i); 485 short type = node.getNodeType(); 486 if (type == Node.ELEMENT_NODE) { 487 Element subElement = (Element )node; 488 String subElementName = subElement.getNodeName(); 489 if (subElementName.equals("libraryLocation")) { LibraryLocation loc = getLibraryLocation(subElement); 491 vmStandin.setLibraryLocations(new LibraryLocation[]{loc}); 492 break; 493 } else if (subElementName.equals("libraryLocations")) { setLibraryLocations(vmStandin, subElement); 495 break; 496 } 497 } 498 } 499 500 String externalForm = vmElement.getAttribute("javadocURL"); if (externalForm != null && externalForm.length() > 0) { 503 try { 504 vmStandin.setJavadocLocation(new URL (externalForm)); 505 } catch (MalformedURLException e) { 506 LaunchingPlugin.log(e); 507 } 508 } 509 510 String vmArgs = vmElement.getAttribute("vmargs"); if (vmArgs != null && vmArgs.length() >0) { 513 vmStandin.setVMArgs(vmArgs); 514 } 515 } else { 516 LaunchingPlugin.log("id attribute missing from VM element specification."); } 518 } 519 520 524 private static LibraryLocation getLibraryLocation(Element libLocationElement) { 525 String jreJar= libLocationElement.getAttribute("jreJar"); String jreSrc= libLocationElement.getAttribute("jreSrc"); String pkgRoot= libLocationElement.getAttribute("pkgRoot"); String jreJavadoc= libLocationElement.getAttribute("jreJavadoc"); URL javadocURL= null; 530 if (jreJavadoc.length() == 0) { 531 jreJavadoc= null; 532 } else { 533 try { 534 javadocURL= new URL (jreJavadoc); 535 } catch (MalformedURLException e) { 536 LaunchingPlugin.log("Library location element is specified incorrectly."); } 538 } 539 if (jreJar != null && jreSrc != null && pkgRoot != null) { 540 return new LibraryLocation(new Path(jreJar), new Path(jreSrc), new Path(pkgRoot), javadocURL); 541 } 542 LaunchingPlugin.log("Library location element is specified incorrectly."); return null; 544 } 545 546 550 private static void setLibraryLocations(IVMInstall vm, Element libLocationsElement) { 551 NodeList list = libLocationsElement.getChildNodes(); 552 int length = list.getLength(); 553 List locations = new ArrayList (length); 554 for (int i = 0; i < length; ++i) { 555 Node node = list.item(i); 556 short type = node.getNodeType(); 557 if (type == Node.ELEMENT_NODE) { 558 Element libraryLocationElement= (Element )node; 559 if (libraryLocationElement.getNodeName().equals("libraryLocation")) { locations.add(getLibraryLocation(libraryLocationElement)); 561 } 562 } 563 } 564 vm.setLibraryLocations((LibraryLocation[])locations.toArray(new LibraryLocation[locations.size()])); 565 } 566 567 572 public void removeVM(IVMInstall vm) { 573 fVMList.remove(vm); 574 fInvalidVMList.remove(vm); 575 List list = (List ) fVMTypeToVMMap.get(vm.getVMInstallType()); 576 if (list != null) { 577 list.remove(vm); 578 } 579 } 580 581 } 582 | Popular Tags |