1 11 package org.eclipse.jdt.internal.launching; 12 13 14 import java.io.BufferedInputStream ; 15 import java.io.BufferedOutputStream ; 16 import java.io.ByteArrayInputStream ; 17 import java.io.ByteArrayOutputStream ; 18 import java.io.File ; 19 import java.io.FileInputStream ; 20 import java.io.FileOutputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.Hashtable ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import javax.xml.parsers.DocumentBuilder ; 33 import javax.xml.parsers.DocumentBuilderFactory ; 34 import javax.xml.parsers.FactoryConfigurationError ; 35 import javax.xml.parsers.ParserConfigurationException ; 36 import javax.xml.transform.OutputKeys ; 37 import javax.xml.transform.Transformer ; 38 import javax.xml.transform.TransformerException ; 39 import javax.xml.transform.TransformerFactory ; 40 import javax.xml.transform.dom.DOMSource ; 41 import javax.xml.transform.stream.StreamResult ; 42 43 import org.eclipse.core.resources.IMarker; 44 import org.eclipse.core.resources.IResource; 45 import org.eclipse.core.resources.IResourceChangeEvent; 46 import org.eclipse.core.resources.IResourceChangeListener; 47 import org.eclipse.core.resources.IResourceDelta; 48 import org.eclipse.core.resources.ISaveContext; 49 import org.eclipse.core.resources.ISaveParticipant; 50 import org.eclipse.core.resources.IWorkspaceRunnable; 51 import org.eclipse.core.resources.ResourcesPlugin; 52 import org.eclipse.core.runtime.CoreException; 53 import org.eclipse.core.runtime.FileLocator; 54 import org.eclipse.core.runtime.IConfigurationElement; 55 import org.eclipse.core.runtime.IExtensionPoint; 56 import org.eclipse.core.runtime.IPath; 57 import org.eclipse.core.runtime.IProgressMonitor; 58 import org.eclipse.core.runtime.IStatus; 59 import org.eclipse.core.runtime.MultiStatus; 60 import org.eclipse.core.runtime.Path; 61 import org.eclipse.core.runtime.Platform; 62 import org.eclipse.core.runtime.Plugin; 63 import org.eclipse.core.runtime.Preferences; 64 import org.eclipse.core.runtime.Status; 65 import org.eclipse.core.runtime.Preferences.PropertyChangeEvent; 66 import org.eclipse.core.runtime.jobs.Job; 67 import org.eclipse.debug.core.DebugEvent; 68 import org.eclipse.debug.core.DebugPlugin; 69 import org.eclipse.debug.core.IDebugEventSetListener; 70 import org.eclipse.debug.core.ILaunch; 71 import org.eclipse.debug.core.ILaunchConfiguration; 72 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 73 import org.eclipse.debug.core.ILaunchesListener; 74 import org.eclipse.debug.core.model.IDebugTarget; 75 import org.eclipse.debug.core.model.IProcess; 76 import org.eclipse.jdt.core.IClasspathEntry; 77 import org.eclipse.jdt.core.IJavaProject; 78 import org.eclipse.jdt.core.JavaCore; 79 import org.eclipse.jdt.launching.IRuntimeClasspathEntry2; 80 import org.eclipse.jdt.launching.IVMConnector; 81 import org.eclipse.jdt.launching.IVMInstall; 82 import org.eclipse.jdt.launching.IVMInstallChangedListener; 83 import org.eclipse.jdt.launching.JavaRuntime; 84 import org.eclipse.jdt.launching.VMStandin; 85 import org.eclipse.jdt.launching.environments.IExecutionEnvironment; 86 import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager; 87 import org.eclipse.jdt.launching.sourcelookup.ArchiveSourceLocation; 88 import org.osgi.framework.BundleContext; 89 import org.w3c.dom.Document ; 90 import org.w3c.dom.Element ; 91 import org.w3c.dom.Node ; 92 import org.w3c.dom.NodeList ; 93 import org.xml.sax.InputSource ; 94 import org.xml.sax.SAXException ; 95 import org.xml.sax.helpers.DefaultHandler ; 96 97 import com.ibm.icu.text.MessageFormat; 98 99 public class LaunchingPlugin extends Plugin implements Preferences.IPropertyChangeListener, IVMInstallChangedListener, IResourceChangeListener, ILaunchesListener, IDebugEventSetListener { 100 101 104 public static final String ID_PLUGIN= "org.eclipse.jdt.launching"; 106 109 public static final String ID_EXTENSION_POINT_VM_CONNECTORS = "vmConnectors"; 111 114 public static final String ID_EXTENSION_POINT_RUNTIME_CLASSPATH_ENTRIES = "runtimeClasspathEntries"; 116 121 public static final String ID_JRE_CONTAINER_MARKER = ID_PLUGIN + ".jreContainerMarker"; 123 private static LaunchingPlugin fgLaunchingPlugin; 124 125 private HashMap fVMConnectors = null; 126 127 130 private HashMap fClasspathEntryExtensions = null; 131 132 private String fOldVMPrefString = EMPTY_STRING; 133 134 private boolean fIgnoreVMDefPropertyChangeEvents = false; 135 136 private static final String EMPTY_STRING = ""; 138 142 private static Map fgLibraryInfoMap = null; 143 144 148 private boolean fBatchingChanges = false; 149 150 153 private static DocumentBuilder fgXMLParser = null; 154 155 158 public static boolean DEBUG = false; 159 160 163 class VMChanges implements IVMInstallChangedListener { 164 165 private boolean fDefaultChanged = false; 167 168 private HashMap fRenamedContainerIds = new HashMap (); 170 171 178 private IPath getContainerId(IVMInstall vm) { 179 if (vm != null) { 180 String name = vm.getName(); 181 if (name != null) { 182 IPath path = new Path(JavaRuntime.JRE_CONTAINER); 183 path = path.append(new Path(vm.getVMInstallType().getId())); 184 path = path.append(new Path(name)); 185 return path; 186 } 187 } 188 return null; 189 } 190 191 194 public void defaultVMInstallChanged(IVMInstall previous, IVMInstall current) { 195 fDefaultChanged = true; 196 } 197 198 201 public void vmAdded(IVMInstall vm) { 202 } 203 204 207 public void vmChanged(org.eclipse.jdt.launching.PropertyChangeEvent event) { 208 String property = event.getProperty(); 209 IVMInstall vm = (IVMInstall)event.getSource(); 210 if (property.equals(IVMInstallChangedListener.PROPERTY_NAME)) { 211 IPath newId = getContainerId(vm); 212 IPath oldId = new Path(JavaRuntime.JRE_CONTAINER); 213 oldId = oldId.append(vm.getVMInstallType().getId()); 214 String oldName = (String )event.getOldValue(); 215 if (oldName != null) { 217 oldId = oldId.append(oldName); 218 fRenamedContainerIds.put(oldId, newId); 219 try { 221 ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(); 222 String container = null; 223 ILaunchConfigurationWorkingCopy wc = null; 224 IPath cpath = null; 225 for(int i = 0; i < configs.length; i++) { 226 container = configs[i].getAttribute(JavaRuntime.JRE_CONTAINER, (String )null); 227 if(container != null) { 228 cpath = new Path(container); 229 if(cpath.lastSegment().equals(oldName)) { 230 cpath = cpath.removeLastSegments(1).append(newId.lastSegment()).addTrailingSeparator(); 231 wc = configs[i].getWorkingCopy(); 232 wc.setAttribute(JavaRuntime.JRE_CONTAINER, cpath.toString()); 233 wc.doSave(); 234 } 235 } 236 } 237 } catch (CoreException e) {} 238 } 239 } 240 } 241 242 245 public void vmRemoved(IVMInstall vm) { 246 } 247 248 252 public void process() { 253 JREUpdateJob job = new JREUpdateJob(this); 254 job.schedule(); 255 } 256 257 protected void doit(IProgressMonitor monitor) throws CoreException { 258 IWorkspaceRunnable runnable = new IWorkspaceRunnable() { 259 public void run(IProgressMonitor monitor1) throws CoreException { 260 IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects(); 261 monitor1.beginTask(LaunchingMessages.LaunchingPlugin_0, projects.length + 1); 262 rebind(monitor1, projects); 263 monitor1.done(); 264 } 265 }; 266 JavaCore.run(runnable, null, monitor); 267 } 268 269 274 private void rebind(IProgressMonitor monitor, IJavaProject[] projects) throws CoreException { 275 276 if (fDefaultChanged) { 277 JavaClasspathVariablesInitializer initializer = new JavaClasspathVariablesInitializer(); 279 initializer.initialize(JavaRuntime.JRELIB_VARIABLE); 280 initializer.initialize(JavaRuntime.JRESRC_VARIABLE); 281 initializer.initialize(JavaRuntime.JRESRCROOT_VARIABLE); 282 } 283 monitor.worked(1); 284 285 for (int i = 0; i < projects.length; i++) { 287 IJavaProject project = projects[i]; 288 IClasspathEntry[] entries = project.getRawClasspath(); 289 boolean replace = false; 290 for (int j = 0; j < entries.length; j++) { 291 IClasspathEntry entry = entries[j]; 292 switch (entry.getEntryKind()) { 293 case IClasspathEntry.CPE_CONTAINER: 294 IPath reference = entry.getPath(); 295 IPath newBinding = null; 296 String firstSegment = reference.segment(0); 297 if (JavaRuntime.JRE_CONTAINER.equals(firstSegment)) { 298 if (reference.segmentCount() > 1) { 299 IPath renamed = (IPath)fRenamedContainerIds.get(reference); 300 if (renamed != null) { 301 newBinding = renamed; 304 } 305 } 306 JREContainerInitializer initializer = new JREContainerInitializer(); 307 if (newBinding == null){ 308 initializer.initialize(reference, project); 310 } else { 311 IClasspathEntry newEntry = JavaCore.newContainerEntry(newBinding, entry.isExported()); 313 entries[j] = newEntry; 314 replace = true; 315 } 316 } 317 break; 318 default: 319 break; 320 } 321 } 322 if (replace) { 323 project.setRawClasspath(entries, null); 324 } 325 monitor.worked(1); 326 } 327 328 } 329 330 } 331 332 class JREUpdateJob extends Job { 333 private VMChanges fChanges; 334 335 public JREUpdateJob(VMChanges changes) { 336 super(LaunchingMessages.LaunchingPlugin_1); 337 fChanges = changes; 338 setSystem(true); 339 } 340 341 344 protected IStatus run(IProgressMonitor monitor) { 345 try { 346 fChanges.doit(monitor); 347 } catch (CoreException e) { 348 return e.getStatus(); 349 } 350 return Status.OK_STATUS; 351 } 352 353 } 354 355 358 public LaunchingPlugin() { 359 super(); 360 fgLaunchingPlugin = this; 361 } 362 363 370 public static LibraryInfo getLibraryInfo(String javaInstallPath) { 371 if (fgLibraryInfoMap == null) { 372 restoreLibraryInfo(); 373 } 374 return (LibraryInfo) fgLibraryInfoMap.get(javaInstallPath); 375 } 376 377 384 public static void setLibraryInfo(String javaInstallPath, LibraryInfo info) { 385 if (fgLibraryInfoMap == null) { 386 restoreLibraryInfo(); 387 } 388 if (info == null) { 389 fgLibraryInfoMap.remove(javaInstallPath); 390 } else { 391 fgLibraryInfoMap.put(javaInstallPath, info); 392 } 393 saveLibraryInfo(); 394 } 395 396 400 public static File getFileInPlugin(IPath path) { 401 try { 402 URL installURL = 403 new URL (getDefault().getBundle().getEntry("/"), path.toString()); URL localURL = FileLocator.toFileURL(installURL); 405 return new File (localURL.getFile()); 406 } catch (IOException ioe) { 407 return null; 408 } 409 } 410 411 414 public static String getUniqueIdentifier() { 415 return ID_PLUGIN; 416 } 417 418 422 public static LaunchingPlugin getDefault() { 423 if(fgLaunchingPlugin == null) { 424 fgLaunchingPlugin = new LaunchingPlugin(); 425 } 426 return fgLaunchingPlugin; 427 } 428 429 433 public static void log(IStatus status) { 434 getDefault().getLog().log(status); 435 } 436 437 441 public static void log(String message) { 442 log(new Status(IStatus.ERROR, getUniqueIdentifier(), IStatus.ERROR, message, null)); 443 } 444 445 449 public static void log(Throwable e) { 450 log(new Status(IStatus.ERROR, getUniqueIdentifier(), IStatus.ERROR, e.getMessage(), e)); 451 } 452 453 459 public void stop(BundleContext context) throws Exception { 460 try { 461 DebugPlugin.getDefault().getLaunchManager().removeLaunchListener(this); 462 DebugPlugin.getDefault().removeDebugEventListener(this); 463 ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); 464 ArchiveSourceLocation.closeArchives(); 465 getPluginPreferences().removePropertyChangeListener(this); 466 JavaRuntime.removeVMInstallChangedListener(this); 467 JavaRuntime.saveVMConfiguration(); 468 fgXMLParser = null; 469 ResourcesPlugin.getWorkspace().removeSaveParticipant(this); 470 } finally { 471 super.stop(context); 472 } 473 } 474 475 478 public void start(BundleContext context) throws Exception { 479 super.start(context); 480 DEBUG = "true".equals(Platform.getDebugOption("org.eclipse.jdt.launching/debug")); ResourcesPlugin.getWorkspace().addSaveParticipant(this, new ISaveParticipant() { 482 public void doneSaving(ISaveContext context1) {} 483 public void prepareToSave(ISaveContext context1) throws CoreException {} 484 public void rollback(ISaveContext context1) {} 485 public void saving(ISaveContext context1) throws CoreException { 486 savePluginPreferences(); 487 } 488 489 }); 490 String launchFilter = "*." + ILaunchConfiguration.LAUNCH_CONFIGURATION_FILE_EXTENSION; Hashtable optionsMap = JavaCore.getOptions(); 493 String filters= (String )optionsMap.get("org.eclipse.jdt.core.builder.resourceCopyExclusionFilter"); boolean modified = false; 495 if (filters == null || filters.length() == 0) { 496 filters= launchFilter; 497 modified = true; 498 } else if (filters.indexOf(launchFilter) == -1) { 499 filters= filters + ',' + launchFilter; 500 modified = true; 501 } 502 503 if (modified) { 504 optionsMap.put("org.eclipse.jdt.core.builder.resourceCopyExclusionFilter", filters); JavaCore.setOptions(optionsMap); 506 } 507 508 getPluginPreferences().setDefault(JavaRuntime.PREF_CONNECT_TIMEOUT, JavaRuntime.DEF_CONNECT_TIMEOUT); 510 getPluginPreferences().addPropertyChangeListener(this); 511 512 JavaRuntime.addVMInstallChangedListener(this); 513 ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.PRE_DELETE | IResourceChangeEvent.PRE_CLOSE | IResourceChangeEvent.PRE_BUILD); 514 DebugPlugin.getDefault().getLaunchManager().addLaunchListener(this); 515 DebugPlugin.getDefault().addDebugEventListener(this); 516 } 517 518 525 public IVMConnector getVMConnector(String id) { 526 if (fVMConnectors == null) { 527 initializeVMConnectors(); 528 } 529 return (IVMConnector)fVMConnectors.get(id); 530 } 531 532 537 public IVMConnector[] getVMConnectors() { 538 if (fVMConnectors == null) { 539 initializeVMConnectors(); 540 } 541 return (IVMConnector[])fVMConnectors.values().toArray(new IVMConnector[fVMConnectors.size()]); 542 } 543 544 547 private void initializeVMConnectors() { 548 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(ID_PLUGIN, ID_EXTENSION_POINT_VM_CONNECTORS); 549 IConfigurationElement[] configs= extensionPoint.getConfigurationElements(); 550 MultiStatus status= new MultiStatus(getUniqueIdentifier(), IStatus.OK, "Exception occurred reading vmConnectors extensions.", null); fVMConnectors = new HashMap (configs.length); 552 for (int i= 0; i < configs.length; i++) { 553 try { 554 IVMConnector vmConnector= (IVMConnector)configs[i].createExecutableExtension("class"); fVMConnectors.put(vmConnector.getIdentifier(), vmConnector); 556 } catch (CoreException e) { 557 status.add(e.getStatus()); 558 } 559 } 560 if (!status.isOK()) { 561 LaunchingPlugin.log(status); 562 } 563 } 564 565 572 public IRuntimeClasspathEntry2 newRuntimeClasspathEntry(String id) throws CoreException { 573 if (fClasspathEntryExtensions == null) { 574 initializeRuntimeClasspathExtensions(); 575 } 576 IConfigurationElement config = (IConfigurationElement) fClasspathEntryExtensions.get(id); 577 if (config == null) { 578 abort(MessageFormat.format(LaunchingMessages.LaunchingPlugin_32, new String []{id}), null); 579 } 580 return (IRuntimeClasspathEntry2) config.createExecutableExtension("class"); } 582 583 586 private void initializeRuntimeClasspathExtensions() { 587 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(LaunchingPlugin.ID_PLUGIN, ID_EXTENSION_POINT_RUNTIME_CLASSPATH_ENTRIES); 588 IConfigurationElement[] configs= extensionPoint.getConfigurationElements(); 589 fClasspathEntryExtensions = new HashMap (configs.length); 590 for (int i= 0; i < configs.length; i++) { 591 fClasspathEntryExtensions.put(configs[i].getAttribute("id"), configs[i]); } 593 } 594 595 601 public void propertyChange(PropertyChangeEvent event) { 602 String property = event.getProperty(); 603 if (property.equals(JavaRuntime.PREF_VM_XML)) { 604 if (!isIgnoreVMDefPropertyChangeEvents()) { 605 processVMPrefsChanged((String )event.getOldValue(), (String )event.getNewValue()); 606 } 607 } 608 } 609 610 621 protected void processVMPrefsChanged(String oldValue, String newValue) { 622 623 fBatchingChanges = true; 625 VMChanges vmChanges = null; 626 try { 627 628 String oldPrefString; 629 String newPrefString; 630 631 if (newValue == null || newValue.equals(EMPTY_STRING)) { 633 fOldVMPrefString = oldValue; 634 return; 635 } 636 else if (oldValue == null || oldValue.equals(EMPTY_STRING)) { 639 oldPrefString = fOldVMPrefString; 640 newPrefString = newValue; 641 } 642 else { 644 oldPrefString = oldValue; 645 newPrefString = newValue; 646 } 647 648 vmChanges = new VMChanges(); 649 JavaRuntime.addVMInstallChangedListener(vmChanges); 650 651 VMDefinitionsContainer oldResults = getVMDefinitions(oldPrefString); 653 654 VMDefinitionsContainer newResults = getVMDefinitions(newPrefString); 656 657 List deleted = oldResults.getVMList(); 659 List current = newResults.getValidVMList(); 660 deleted.removeAll(current); 661 662 Iterator deletedIterator = deleted.iterator(); 665 while (deletedIterator.hasNext()) { 666 VMStandin deletedVMStandin = (VMStandin) deletedIterator.next(); 667 deletedVMStandin.getVMInstallType().disposeVMInstall(deletedVMStandin.getId()); 668 } 669 670 Iterator iter = current.iterator(); 673 while (iter.hasNext()) { 674 VMStandin standin = (VMStandin)iter.next(); 675 standin.convertToRealVM(); 676 } 677 678 String newDefaultId = newResults.getDefaultVMInstallCompositeID(); 681 if (newDefaultId != null) { 682 IVMInstall newDefaultVM = JavaRuntime.getVMFromCompositeId(newDefaultId); 683 if (newDefaultVM != null) { 684 try { 685 JavaRuntime.setDefaultVMInstall(newDefaultVM, null, false); 686 } catch (CoreException ce) { 687 log(ce); 688 } 689 } 690 } 691 692 } finally { 693 fBatchingChanges = false; 695 if (vmChanges != null) { 696 JavaRuntime.removeVMInstallChangedListener(vmChanges); 697 vmChanges.process(); 698 } 699 } 700 701 } 702 703 710 private VMDefinitionsContainer getVMDefinitions(String xml) { 711 if (xml.length() > 0) { 712 try { 713 ByteArrayInputStream stream = new ByteArrayInputStream (xml.getBytes("UTF8")); return VMDefinitionsContainer.parseXMLIntoContainer(stream); 715 } catch (IOException e) { 716 LaunchingPlugin.log(e); 717 } 718 } 719 return new VMDefinitionsContainer(); 720 } 721 722 725 public void defaultVMInstallChanged(IVMInstall previous, IVMInstall current) { 726 if (!fBatchingChanges) { 727 VMChanges changes = new VMChanges(); 728 changes.defaultVMInstallChanged(previous, current); 729 changes.process(); 730 } 731 } 732 733 736 public void vmAdded(IVMInstall vm) { 737 } 738 739 742 public void vmChanged(org.eclipse.jdt.launching.PropertyChangeEvent event) { 743 if (!fBatchingChanges) { 744 VMChanges changes = new VMChanges(); 745 changes.vmChanged(event); 746 changes.process(); 747 } 748 } 749 750 753 public void vmRemoved(IVMInstall vm) { 754 if (!fBatchingChanges) { 755 VMChanges changes = new VMChanges(); 756 changes.vmRemoved(vm); 757 changes.process(); 758 } 759 } 760 761 768 public void resourceChanged(IResourceChangeEvent event) { 769 ArchiveSourceLocation.closeArchives(); 770 if (event.getType() == IResourceChangeEvent.PRE_BUILD) { 771 IResourceDelta delta = event.getDelta(); 772 IResourceDelta[] projectDeltas = delta.getAffectedChildren(); 773 for (int i = 0, length = projectDeltas.length; i < length; i++) { 774 IResourceDelta projectDelta = projectDeltas[i]; 775 IResourceDelta classpathDelta = projectDelta.findMember(new Path(".classpath")); if (classpathDelta != null || (projectDelta.getFlags() & IResourceDelta.DESCRIPTION) > 0) { 777 IJavaProject project = (IJavaProject) JavaCore.create(projectDelta.getResource()); 778 if (project != null && project.exists()) { 779 try { 780 IClasspathEntry[] rawClasspath = project.getRawClasspath(); 781 for (int j = 0; j < rawClasspath.length; j++) { 782 IClasspathEntry entry = rawClasspath[j]; 783 if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { 784 IPath path = entry.getPath(); 785 if (JavaRuntime.JRE_CONTAINER.equals(path.segment(0))) { 786 IVMInstall vm = JREContainerInitializer.resolveVM(path); 787 validateEnvironment(path, project, vm); 788 } 789 } 790 } 791 } catch (CoreException e) { 792 LaunchingPlugin.log(e); 793 } 794 } 795 } 796 } 797 } 798 } 799 800 804 public void setIgnoreVMDefPropertyChangeEvents(boolean ignore) { 805 fIgnoreVMDefPropertyChangeEvents = ignore; 806 } 807 808 812 public boolean isIgnoreVMDefPropertyChangeEvents() { 813 return fIgnoreVMDefPropertyChangeEvents; 814 } 815 816 827 private static String getLibraryInfoAsXML() throws ParserConfigurationException , IOException , TransformerException { 828 829 Document doc = getDocument(); 830 Element config = doc.createElement("libraryInfos"); doc.appendChild(config); 832 833 Iterator locations = fgLibraryInfoMap.keySet().iterator(); 835 while (locations.hasNext()) { 836 String home = (String )locations.next(); 837 LibraryInfo info = (LibraryInfo) fgLibraryInfoMap.get(home); 838 Element locationElemnet = infoAsElement(doc, info); 839 locationElemnet.setAttribute("home", home); config.appendChild(locationElemnet); 841 } 842 843 return LaunchingPlugin.serializeDocument(doc); 845 } 846 847 852 public static Document getDocument() throws ParserConfigurationException { 853 DocumentBuilderFactory dfactory= DocumentBuilderFactory.newInstance(); 854 DocumentBuilder docBuilder= dfactory.newDocumentBuilder(); 855 Document doc= docBuilder.newDocument(); 856 return doc; 857 } 858 859 866 private static Element infoAsElement(Document doc, LibraryInfo info) { 867 Element libraryElement = doc.createElement("libraryInfo"); libraryElement.setAttribute("version", info.getVersion()); appendPathElements(doc, "bootpath", libraryElement, info.getBootpath()); appendPathElements(doc, "extensionDirs", libraryElement, info.getExtensionDirs()); appendPathElements(doc, "endorsedDirs", libraryElement, info.getEndorsedDirs()); return libraryElement; 873 } 874 875 884 private static void appendPathElements(Document doc, String elementType, Element libraryElement, String [] paths) { 885 if (paths.length > 0) { 886 Element child = doc.createElement(elementType); 887 libraryElement.appendChild(child); 888 for (int i = 0; i < paths.length; i++) { 889 String path = paths[i]; 890 Element entry = doc.createElement("entry"); child.appendChild(entry); 892 entry.setAttribute("path", path); } 894 } 895 } 896 897 900 private static void saveLibraryInfo() { 901 OutputStream stream= null; 902 try { 903 String xml = getLibraryInfoAsXML(); 904 IPath libPath = getDefault().getStateLocation(); 905 libPath = libPath.append("libraryInfos.xml"); File file = libPath.toFile(); 907 if (!file.exists()) { 908 file.createNewFile(); 909 } 910 stream = new BufferedOutputStream (new FileOutputStream (file)); 911 stream.write(xml.getBytes("UTF8")); } catch (IOException e) { 913 log(e); 914 } catch (ParserConfigurationException e) { 915 log(e); 916 } catch (TransformerException e) { 917 log(e); 918 } finally { 919 if (stream != null) { 920 try { 921 stream.close(); 922 } catch (IOException e1) { 923 } 924 } 925 } 926 } 927 928 931 private static void restoreLibraryInfo() { 932 fgLibraryInfoMap = new HashMap (10); 933 IPath libPath = getDefault().getStateLocation(); 934 libPath = libPath.append("libraryInfos.xml"); File file = libPath.toFile(); 936 if (file.exists()) { 937 try { 938 InputStream stream = new BufferedInputStream (new FileInputStream (file)); 939 DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 940 parser.setErrorHandler(new DefaultHandler ()); 941 Element root = parser.parse(new InputSource (stream)).getDocumentElement(); 942 if(!root.getNodeName().equals("libraryInfos")) { return; 944 } 945 946 NodeList list = root.getChildNodes(); 947 int length = list.getLength(); 948 for (int i = 0; i < length; ++i) { 949 Node node = list.item(i); 950 short type = node.getNodeType(); 951 if (type == Node.ELEMENT_NODE) { 952 Element element = (Element) node; 953 String nodeName = element.getNodeName(); 954 if (nodeName.equalsIgnoreCase("libraryInfo")) { String version = element.getAttribute("version"); String location = element.getAttribute("home"); String [] bootpath = getPathsFromXML(element, "bootpath"); String [] extDirs = getPathsFromXML(element, "extensionDirs"); String [] endDirs = getPathsFromXML(element, "endorsedDirs"); if (location != null) { 961 LibraryInfo info = new LibraryInfo(version, bootpath, extDirs, endDirs); 962 fgLibraryInfoMap.put(location, info); 963 } 964 } 965 } 966 } 967 } catch (IOException e) { 968 log(e); 969 } catch (ParserConfigurationException e) { 970 log(e); 971 } catch (SAXException e) { 972 log(e); 973 } 974 } 975 } 976 977 983 private static String [] getPathsFromXML(Element lib, String pathType) { 984 List paths = new ArrayList (); 985 NodeList list = lib.getChildNodes(); 986 int length = list.getLength(); 987 for (int i = 0; i < length; ++i) { 988 Node node = list.item(i); 989 short type = node.getNodeType(); 990 if (type == Node.ELEMENT_NODE) { 991 Element element = (Element) node; 992 String nodeName = element.getNodeName(); 993 if (nodeName.equalsIgnoreCase(pathType)) { 994 NodeList entries = element.getChildNodes(); 995 int numEntries = entries.getLength(); 996 for (int j = 0; j < numEntries; j++) { 997 Node n = entries.item(j); 998 short t = n.getNodeType(); 999 if (t == Node.ELEMENT_NODE) { 1000 Element entryElement = (Element)n; 1001 String name = entryElement.getNodeName(); 1002 if (name.equals("entry")) { String path = entryElement.getAttribute("path"); if (path != null && path.length() > 0) { 1005 paths.add(path); 1006 } 1007 } 1008 } 1009 } 1010 } 1011 } 1012 } 1013 return (String [])paths.toArray(new String [paths.size()]); 1014 } 1015 1016 1022 public void launchesRemoved(ILaunch[] launches) { 1023 ArchiveSourceLocation.closeArchives(); 1024 } 1025 1026 1029 public void launchesAdded(ILaunch[] launches) { 1030 } 1031 1032 1035 public void launchesChanged(ILaunch[] launches) { 1036 } 1037 1038 1044 public void handleDebugEvents(DebugEvent[] events) { 1045 for (int i = 0; i < events.length; i++) { 1046 DebugEvent event = events[i]; 1047 if (event.getKind() == DebugEvent.TERMINATE) { 1048 Object source = event.getSource(); 1049 if (source instanceof IDebugTarget || source instanceof IProcess) { 1050 ArchiveSourceLocation.closeArchives(); 1051 } 1052 } 1053 } 1054 } 1055 1056 1063 public static String serializeDocument(Document doc) throws IOException , TransformerException { 1064 ByteArrayOutputStream s= new ByteArrayOutputStream (); 1065 1066 TransformerFactory factory= TransformerFactory.newInstance(); 1067 Transformer transformer= factory.newTransformer(); 1068 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 1071 DOMSource source= new DOMSource (doc); 1072 StreamResult outputTarget= new StreamResult (s); 1073 transformer.transform(source, outputTarget); 1074 1075 return s.toString("UTF8"); } 1077 1078 1085 public static DocumentBuilder getParser() throws CoreException { 1086 if (fgXMLParser == null) { 1087 try { 1088 fgXMLParser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 1089 fgXMLParser.setErrorHandler(new DefaultHandler ()); 1090 } catch (ParserConfigurationException e) { 1091 abort(LaunchingMessages.LaunchingPlugin_33, e); 1092 } catch (FactoryConfigurationError e) { 1093 abort(LaunchingMessages.LaunchingPlugin_34, e); 1094 } 1095 } 1096 return fgXMLParser; 1097 } 1098 1099 1106 protected static void abort(String message, Throwable exception) throws CoreException { 1107 IStatus status = new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), 0, message, exception); 1108 throw new CoreException(status); 1109 } 1110 1111 1117 private void validateEnvironment(IPath containerPath, final IJavaProject project, IVMInstall vm) { 1118 try { 1119 project.getProject().deleteMarkers(ID_JRE_CONTAINER_MARKER, false, IResource.DEPTH_ZERO); 1120 } catch (CoreException e) { 1121 LaunchingPlugin.log(e); 1122 } 1123 String id = JREContainerInitializer.getExecutionEnvironmentId(containerPath); 1124 if (id != null) { 1125 IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager(); 1126 final IExecutionEnvironment environment = manager.getEnvironment(id); 1127 if (environment != null) { 1128 if (vm == null) { 1129 String message = MessageFormat.format( 1130 LaunchingMessages.LaunchingPlugin_38, 1131 new String []{environment.getId()}); 1132 createJREContainerProblem(project, message, IMarker.SEVERITY_ERROR); 1133 } else if (!environment.isStrictlyCompatible(vm)) { 1134 IVMInstall[] compatibleVMs = environment.getCompatibleVMs(); 1137 int exact = 0; 1138 for (int i = 0; i < compatibleVMs.length; i++) { 1139 if (environment.isStrictlyCompatible(compatibleVMs[i])) { 1140 exact++; 1141 } 1142 } 1143 String message = null; 1144 if (exact == 0) { 1145 message = MessageFormat.format( 1146 LaunchingMessages.LaunchingPlugin_35, 1147 new String []{environment.getId()}); 1148 } else { 1149 message = MessageFormat.format( 1150 LaunchingMessages.LaunchingPlugin_36, 1151 new String []{environment.getId()}); 1152 } 1153 createJREContainerProblem(project, message, IMarker.SEVERITY_WARNING); 1154 } 1155 } 1156 } 1157 } 1158 1159 1165 private void createJREContainerProblem(IJavaProject javaProject, String message, int severity) { 1166 try { 1167 IMarker marker = javaProject.getProject().createMarker(ID_JRE_CONTAINER_MARKER); 1168 marker.setAttributes( 1169 new String [] { 1170 IMarker.MESSAGE, 1171 IMarker.SEVERITY, 1172 IMarker.LOCATION}, 1173 new Object [] { 1174 message, 1175 new Integer (severity), 1176 LaunchingMessages.LaunchingPlugin_37 1177 }); 1178 } catch (CoreException e) { 1179 return; 1180 } 1181 } 1182} 1183 1184 1185 | Popular Tags |