1 12 package org.eclipse.debug.internal.core; 13 14 15 import java.io.File ; 16 import java.io.IOException ; 17 import java.io.StringReader ; 18 import java.util.ArrayList ; 19 import java.util.HashSet ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import javax.xml.parsers.DocumentBuilder ; 25 import javax.xml.parsers.DocumentBuilderFactory ; 26 import javax.xml.parsers.ParserConfigurationException ; 27 import javax.xml.transform.TransformerException ; 28 29 import org.eclipse.core.resources.IContainer; 30 import org.eclipse.core.resources.IFile; 31 import org.eclipse.core.resources.IResource; 32 import org.eclipse.core.resources.IWorkspaceRoot; 33 import org.eclipse.core.resources.IncrementalProjectBuilder; 34 import org.eclipse.core.resources.ResourcesPlugin; 35 import org.eclipse.core.runtime.CoreException; 36 import org.eclipse.core.runtime.IPath; 37 import org.eclipse.core.runtime.IProgressMonitor; 38 import org.eclipse.core.runtime.IStatus; 39 import org.eclipse.core.runtime.NullProgressMonitor; 40 import org.eclipse.core.runtime.Path; 41 import org.eclipse.core.runtime.PlatformObject; 42 import org.eclipse.core.runtime.Status; 43 import org.eclipse.core.runtime.SubProgressMonitor; 44 import org.eclipse.debug.core.DebugException; 45 import org.eclipse.debug.core.DebugPlugin; 46 import org.eclipse.debug.core.ILaunch; 47 import org.eclipse.debug.core.ILaunchConfiguration; 48 import org.eclipse.debug.core.ILaunchConfigurationType; 49 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 50 import org.eclipse.debug.core.ILaunchDelegate; 51 import org.eclipse.debug.core.IStatusHandler; 52 import org.eclipse.debug.core.Launch; 53 import org.eclipse.debug.core.model.ILaunchConfigurationDelegate; 54 import org.eclipse.debug.core.model.ILaunchConfigurationDelegate2; 55 import org.eclipse.debug.core.model.IPersistableSourceLocator; 56 import org.eclipse.debug.core.sourcelookup.IPersistableSourceLocator2; 57 import org.w3c.dom.Document ; 58 import org.w3c.dom.Element ; 59 import org.xml.sax.InputSource ; 60 import org.xml.sax.SAXException ; 61 import org.xml.sax.helpers.DefaultHandler ; 62 63 import com.ibm.icu.text.MessageFormat; 64 65 70 public class LaunchConfiguration extends PlatformObject implements ILaunchConfiguration { 71 72 80 public static final String ATTR_MAPPED_RESOURCE_PATHS = DebugPlugin.getUniqueIdentifier() + ".MAPPED_RESOURCE_PATHS"; 82 89 public static final String ATTR_MAPPED_RESOURCE_TYPES = DebugPlugin.getUniqueIdentifier() + ".MAPPED_RESOURCE_TYPES"; 91 96 public static final String ATTR_LAUNCH_MODES = DebugPlugin.getUniqueIdentifier() + ".LAUNCH_MODES"; 98 106 public static final String ATTR_PREFERRED_LAUNCHERS = DebugPlugin.getUniqueIdentifier() + ".preferred_launchers"; 108 113 protected static final IStatus promptStatus = new Status(IStatus.INFO, "org.eclipse.debug.ui", 200, "", null); 115 119 protected static final IStatus delegateNotAvailable = new Status(IStatus.INFO, "org.eclipse.debug.core", 226, "", null); 121 126 protected static final IStatus duplicateDelegates = new Status(IStatus.INFO, "org.eclipse.debug.core", 227, "", null); 128 132 private IPath fLocation; 133 134 140 protected LaunchConfiguration(IPath location) { 141 setLocation(location); 142 } 143 144 152 protected LaunchConfiguration(String memento) throws CoreException { 153 Exception ex = null; 154 try { 155 Element root = null; 156 DocumentBuilder parser = 157 DocumentBuilderFactory.newInstance().newDocumentBuilder(); 158 parser.setErrorHandler(new DefaultHandler ()); 159 StringReader reader = new StringReader (memento); 160 InputSource source = new InputSource (reader); 161 root = parser.parse(source).getDocumentElement(); 162 163 String localString = root.getAttribute(IConfigurationElementConstants.LOCAL); 164 String path = root.getAttribute(IConfigurationElementConstants.PATH); 165 166 String message = null; 167 if (path == null) { 168 message = DebugCoreMessages.LaunchConfiguration_Invalid_launch_configuration_memento__missing_path_attribute_3; 169 } else if (localString == null) { 170 message = DebugCoreMessages.LaunchConfiguration_Invalid_launch_configuration_memento__missing_local_attribute_4; 171 } 172 if (message != null) { 173 IStatus s = newStatus(message, DebugException.INTERNAL_ERROR, null); 174 throw new CoreException(s); 175 } 176 177 IPath location = null; 178 boolean local = (Boolean.valueOf(localString)).booleanValue(); 179 if (local) { 180 location = LaunchManager.LOCAL_LAUNCH_CONFIGURATION_CONTAINER_PATH.append(path); 181 } else { 182 location = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(path)).getLocation(); 183 } 184 setLocation(location); 185 if (location == null) { 186 IStatus s = newStatus(MessageFormat.format(DebugCoreMessages.LaunchConfiguration_Unable_to_restore_location_for_launch_configuration_from_memento___0__1, new String []{path}), DebugPlugin.INTERNAL_ERROR, null); 187 throw new CoreException(s); 188 } 189 return; 190 } catch (ParserConfigurationException e) { 191 ex = e; 192 } catch (SAXException e) { 193 ex = e; 194 } catch (IOException e) { 195 ex = e; 196 } 197 IStatus s = newStatus(DebugCoreMessages.LaunchConfiguration_Exception_occurred_parsing_memento_5, DebugException.INTERNAL_ERROR, ex); 198 throw new CoreException(s); 199 } 200 201 204 public boolean contentsEqual(ILaunchConfiguration object) { 205 try { 206 if (object instanceof LaunchConfiguration) { 207 LaunchConfiguration otherConfig = (LaunchConfiguration) object; 208 return getName().equals(otherConfig.getName()) 209 && getType().equals(otherConfig.getType()) 210 && getLocation().equals(otherConfig.getLocation()) 211 && getInfo().equals(otherConfig.getInfo()); 212 } 213 return false; 214 } catch (CoreException ce) { 215 return false; 216 } 217 } 218 219 222 public ILaunchConfigurationWorkingCopy copy(String name) throws CoreException { 223 ILaunchConfigurationWorkingCopy copy = new LaunchConfigurationWorkingCopy(this, name); 224 return copy; 225 } 226 227 230 public void delete() throws CoreException { 231 if (exists()) { 232 if (isLocal()) { 233 if (!(getLocation().toFile().delete())) { 234 throw new DebugException( 235 new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 236 DebugException.REQUEST_FAILED, DebugCoreMessages.LaunchConfiguration_Failed_to_delete_launch_configuration__1, null) 237 ); 238 } 239 getLaunchManager().launchConfigurationDeleted(this); 242 } else { 243 IFile file = getFile(); 246 if (file != null) { 247 if (file.isReadOnly()) { 249 IStatus status = ResourcesPlugin.getWorkspace().validateEdit(new IFile[] {file}, null); 250 if (!status.isOK()) { 251 throw new CoreException(status); 252 } 253 } 254 file.delete(true, null); 255 } else { 256 } 258 } 259 } 260 } 261 262 272 public boolean equals(Object object) { 273 if (object instanceof ILaunchConfiguration) { 274 if (isWorkingCopy()) { 275 return this == object; 276 } 277 ILaunchConfiguration config = (ILaunchConfiguration) object; 278 if (!config.isWorkingCopy()) { 279 return config.getLocation().equals(getLocation()); 280 } 281 } 282 return false; 283 } 284 285 288 public boolean exists() { 289 if (isLocal()) { 290 return getLocation().toFile().exists(); 291 } 292 293 IFile file = getFile(); 294 return file != null && file.exists(); 295 } 296 297 300 public boolean getAttribute(String attributeName, boolean defaultValue) throws CoreException { 301 return getInfo().getBooleanAttribute(attributeName, defaultValue); 302 } 303 304 307 public int getAttribute(String attributeName, int defaultValue) throws CoreException { 308 return getInfo().getIntAttribute(attributeName, defaultValue); 309 } 310 311 314 public List getAttribute(String attributeName, List defaultValue) throws CoreException { 315 return getInfo().getListAttribute(attributeName, defaultValue); 316 } 317 318 321 public Set getAttribute(String attributeName, Set defaultValue) throws CoreException { 322 return getInfo().getSetAttribute(attributeName, defaultValue); 323 } 324 325 328 public Map getAttribute(String attributeName, Map defaultValue) throws CoreException { 329 return getInfo().getMapAttribute(attributeName, defaultValue); 330 } 331 332 335 public String getAttribute(String attributeName, String defaultValue) throws CoreException { 336 return getInfo().getStringAttribute(attributeName, defaultValue); 337 } 338 339 342 public Map getAttributes() throws CoreException { 343 LaunchConfigurationInfo info = getInfo(); 344 return info.getAttributes(); 345 } 346 347 350 public String getCategory() throws CoreException { 351 return getType().getCategory(); 352 } 353 354 363 protected IContainer getContainer() { 364 IFile file = getFile(); 365 if (file != null) { 366 return file.getParent(); 367 } 368 return null; 369 } 370 371 374 public IFile getFile() { 375 if (isLocal()) { 376 return null; 377 } 378 IFile[] files= ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(getLocation()); 379 if (files.length > 0) { 380 return files[0]; 381 } 382 return null; 383 } 384 385 393 protected LaunchConfigurationInfo getInfo() throws CoreException { 394 return getLaunchManager().getInfo(this); 395 } 396 397 401 private String getLastLocationSegment() { 402 String name = getLocation().lastSegment(); 403 if (name.length() > LAUNCH_CONFIGURATION_FILE_EXTENSION.length()) { 404 name = name.substring(0, name.length() - (LAUNCH_CONFIGURATION_FILE_EXTENSION.length() + 1)); 405 } 406 return name; 407 } 408 409 414 protected LaunchManager getLaunchManager() { 415 return (LaunchManager)DebugPlugin.getDefault().getLaunchManager(); 416 } 417 418 421 public IPath getLocation() { 422 return fLocation; 423 } 424 425 428 public IResource[] getMappedResources() throws CoreException { 429 List paths = getAttribute(ATTR_MAPPED_RESOURCE_PATHS, (List )null); 430 if (paths == null || paths.size() == 0) { 431 return null; 432 } 433 List types = getAttribute(ATTR_MAPPED_RESOURCE_TYPES, (List )null); 434 if (types == null || types.size() != paths.size()) { 435 throw new CoreException(newStatus(DebugCoreMessages.LaunchConfiguration_10, DebugPlugin.INTERNAL_ERROR, null)); 436 } 437 ArrayList list = new ArrayList (); 438 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 439 for(int i = 0; i < paths.size(); i++) { 440 String pathStr = (String ) paths.get(i); 441 String typeStr= (String ) types.get(i); 442 int type = -1; 443 try { 444 type = Integer.decode(typeStr).intValue(); 445 } catch (NumberFormatException e) { 446 throw new CoreException(newStatus(DebugCoreMessages.LaunchConfiguration_10, DebugPlugin.INTERNAL_ERROR, e)); 447 } 448 IPath path = Path.fromPortableString(pathStr); 449 IResource res = null; 450 switch (type) { 451 case IResource.FILE: 452 res = root.getFile(path); 453 break; 454 case IResource.PROJECT: 455 res = root.getProject(pathStr); 456 break; 457 case IResource.FOLDER: 458 res = root.getFolder(path); 459 break; 460 case IResource.ROOT: 461 res = root; 462 break; 463 default: 464 throw new CoreException(newStatus(DebugCoreMessages.LaunchConfiguration_10, DebugPlugin.INTERNAL_ERROR, null)); 465 } 466 if(res != null) { 467 list.add(res); 468 } 469 } 470 if (list.isEmpty()) { 471 return null; 472 } 473 return (IResource[])list.toArray(new IResource[list.size()]); 474 } 475 476 479 public String getMemento() throws CoreException { 480 IPath relativePath = null; 481 if (isLocal()) { 482 IPath rootPath = LaunchManager.LOCAL_LAUNCH_CONFIGURATION_CONTAINER_PATH; 483 IPath configPath = getLocation(); 484 relativePath = configPath.removeFirstSegments(rootPath.segmentCount()); 485 relativePath = relativePath.setDevice(null); 486 } else { 487 IFile file = getFile(); 488 if (file == null) { 489 IStatus status = newStatus(MessageFormat.format(DebugCoreMessages.LaunchConfiguration_Unable_to_generate_memento_for__0___shared_file_does_not_exist__1, new String []{getName()}), DebugException.INTERNAL_ERROR, null); 491 throw new CoreException(status); 492 } 493 relativePath = file.getFullPath(); 494 } 495 Exception e= null; 496 try { 497 Document doc = LaunchManager.getDocument(); 498 Element node = doc.createElement(IConfigurationElementConstants.LAUNCH_CONFIGURATION); 499 doc.appendChild(node); 500 node.setAttribute(IConfigurationElementConstants.LOCAL, (Boolean.valueOf(isLocal())).toString()); 501 node.setAttribute(IConfigurationElementConstants.PATH, relativePath.toString()); 502 return LaunchManager.serializeDocument(doc); 503 } catch (IOException ioe) { 504 e= ioe; 505 } catch (ParserConfigurationException pce) { 506 e= pce; 507 } catch (TransformerException te) { 508 e= te; 509 } 510 IStatus status = newStatus(DebugCoreMessages.LaunchConfiguration_Exception_occurred_creating_launch_configuration_memento_9, DebugException.INTERNAL_ERROR, e); 511 throw new CoreException(status); 512 } 513 514 517 public String getName() { 518 return getLastLocationSegment(); 519 } 520 521 public Set getModes() throws CoreException { 522 Set options = getAttribute(ATTR_LAUNCH_MODES, (Set )null); 523 return (options != null ? new HashSet (options) : new HashSet (0)); 524 } 525 526 529 public ILaunchConfigurationType getType() throws CoreException { 530 return getInfo().getType(); 531 } 532 533 536 public ILaunchConfigurationWorkingCopy getWorkingCopy() throws CoreException { 537 return new LaunchConfigurationWorkingCopy(this); 538 } 539 540 543 public int hashCode() { 544 return getLocation().hashCode(); 545 } 546 547 553 protected void initializeSourceLocator(ILaunch launch) throws CoreException { 554 if (launch.getSourceLocator() == null) { 555 String type = getAttribute(ATTR_SOURCE_LOCATOR_ID, (String )null); 556 if (type == null) { 557 type = getType().getSourceLocatorId(); 558 } 559 if (type != null) { 560 IPersistableSourceLocator locator = getLaunchManager().newSourceLocator(type); 561 String memento = getAttribute(ATTR_SOURCE_LOCATOR_MEMENTO, (String )null); 562 if (memento == null) { 563 locator.initializeDefaults(this); 564 } else { 565 if(locator instanceof IPersistableSourceLocator2) 566 ((IPersistableSourceLocator2)locator).initializeFromMemento(memento, this); 567 else 568 locator.initializeFromMemento(memento); 569 } 570 launch.setSourceLocator(locator); 571 } 572 } 573 } 574 575 578 public boolean isLocal() { 579 IPath localPath = LaunchManager.LOCAL_LAUNCH_CONFIGURATION_CONTAINER_PATH; 580 return localPath.isPrefixOf(getLocation()); 581 } 582 583 586 public boolean isMigrationCandidate() throws CoreException { 587 return ((LaunchConfigurationType)getType()).isMigrationCandidate(this); 588 } 589 590 593 public boolean isWorkingCopy() { 594 return false; 595 } 596 597 600 public ILaunch launch(String mode, IProgressMonitor monitor) throws CoreException { 601 return launch(mode, monitor, false); 602 } 603 604 607 public ILaunch launch(String mode, IProgressMonitor monitor, boolean build) throws CoreException { 608 return launch(mode, monitor, build, true); 609 } 610 611 614 public ILaunch launch(String mode, IProgressMonitor monitor, boolean build, boolean register) throws CoreException { 615 if (monitor == null) { 616 monitor = new NullProgressMonitor(); 617 } 618 626 if (build) { 627 monitor.beginTask("", 23); } 629 else { 630 monitor.beginTask("", 13); } 632 monitor.subTask(DebugCoreMessages.LaunchConfiguration_9); 633 try { 634 Set modes = getModes(); 636 modes.add(mode); 637 ILaunchDelegate[] delegates = getType().getDelegates(modes); 638 ILaunchConfigurationDelegate delegate = null; 639 if (delegates.length == 1) { 640 delegate = delegates[0].getDelegate(); 641 } else if (delegates.length == 0) { 642 monitor.setCanceled(true); 643 IStatusHandler handler = DebugPlugin.getDefault().getStatusHandler(promptStatus); 644 if (handler != null) { 645 handler.handleStatus(delegateNotAvailable, new Object [] {this, mode}); 646 } 647 IStatus status = new Status(IStatus.CANCEL, DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR, "No launch delegate found - launch canceled", null); throw new CoreException(status); 649 } else { 650 ILaunchDelegate del = getPreferredDelegate(modes); 651 if(del == null) { 652 del = getType().getPreferredDelegate(modes); 653 } 654 if(del == null) { 655 IStatusHandler handler = DebugPlugin.getDefault().getStatusHandler(promptStatus); 656 IStatus status = null; 657 if (handler != null) { 658 status = (IStatus) handler.handleStatus(duplicateDelegates, new Object [] {this, mode}); 659 } 660 if(status != null && status.isOK()) { 661 del = getPreferredDelegate(modes); 662 if(del == null) { 663 del = getType().getPreferredDelegate(modes); 664 } 665 if(del != null) { 666 delegate = del.getDelegate(); 667 } 668 else { 669 monitor.setCanceled(true); 670 status = new Status(IStatus.CANCEL, DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR, "Duplicate launcher detected - launch canceled", null); throw new CoreException(status); 672 } 673 } 674 else { 675 monitor.setCanceled(true); 676 status = new Status(IStatus.CANCEL, DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR, "Duplicate launcher detected - launch canceled", null); throw new CoreException(status); 678 } 679 } 680 else { 681 delegate = del.getDelegate(); 682 } 683 } 684 685 ILaunchConfigurationDelegate2 delegate2 = null; 686 if (delegate instanceof ILaunchConfigurationDelegate2) { 687 delegate2 = (ILaunchConfigurationDelegate2) delegate; 688 } 689 ILaunch launch = null; 691 if (delegate2 != null) { 692 launch = delegate2.getLaunch(this, mode); 693 } 694 if (launch == null) { 695 launch = new Launch(this, mode, null); 696 } else { 697 if (!mode.equals(launch.getLaunchMode())) { 699 IStatus status = new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR, 700 MessageFormat.format(DebugCoreMessages.LaunchConfiguration_13, new String []{mode, launch.getLaunchMode()}), null); 701 throw new CoreException(status); 702 } 703 } 704 boolean captureOutput = getAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, true); 705 if(!captureOutput) { 706 launch.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, "false"); } else { 708 launch.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, null); 709 } 710 String attribute = getAttribute(DebugPlugin.ATTR_CONSOLE_ENCODING, (String )null); 711 launch.setAttribute(DebugPlugin.ATTR_CONSOLE_ENCODING, attribute); 712 713 monitor.subTask(DebugCoreMessages.LaunchConfiguration_8); 715 716 if (delegate2 != null) { 717 if (!(delegate2.preLaunchCheck(this, mode, new SubProgressMonitor(monitor, 1)))) { 718 monitor.setCanceled(true); 720 return launch; 721 } 722 } 723 else { 724 monitor.worked(1); 725 } 726 if (build) { 728 IProgressMonitor buildMonitor = new SubProgressMonitor(monitor, 10, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK); 729 buildMonitor.beginTask(DebugCoreMessages.LaunchConfiguration_7, 10); 730 buildMonitor.subTask(DebugCoreMessages.LaunchConfiguration_6); 731 if (delegate2 != null) { 732 build = delegate2.buildForLaunch(this, mode, new SubProgressMonitor(buildMonitor, 7)); 733 } 734 if (build) { 735 buildMonitor.subTask(DebugCoreMessages.LaunchConfiguration_5); 736 ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.INCREMENTAL_BUILD, new SubProgressMonitor(buildMonitor, 3)); 737 } 738 else { 739 buildMonitor.worked(3); 740 } 741 } 742 monitor.subTask(DebugCoreMessages.LaunchConfiguration_4); 744 if (delegate2 != null) { 745 if (!(delegate2.finalLaunchCheck(this, mode, new SubProgressMonitor(monitor, 1)))) { 746 monitor.setCanceled(true); 748 return launch; 749 } 750 } 751 else { 752 monitor.worked(1); 753 } 754 if (register) { 755 getLaunchManager().addLaunch(launch); 756 } 757 758 try { 759 monitor.subTask(DebugCoreMessages.LaunchConfiguration_3); 761 initializeSourceLocator(launch); 762 monitor.worked(1); 763 764 765 monitor.subTask(DebugCoreMessages.LaunchConfiguration_2); 766 delegate.launch(this, mode, launch, new SubProgressMonitor(monitor, 10)); 767 } catch (CoreException e) { 768 if (!launch.hasChildren()) { 770 getLaunchManager().removeLaunch(launch); 771 } 772 monitor.setCanceled(true); 773 throw e; 774 } 775 if (monitor.isCanceled()) { 776 getLaunchManager().removeLaunch(launch); 777 } 778 return launch; 779 } 780 finally { 781 monitor.done(); 782 } 783 } 784 785 788 public void migrate() throws CoreException { 789 ((LaunchConfigurationType)getType()).migrate(this); 790 } 791 792 801 protected IStatus newStatus(String message, int code, Throwable e) { 802 return new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), code, message, e); 803 } 804 805 812 private void setLocation(IPath location) { 813 fLocation = location; 814 } 815 816 819 public boolean supportsMode(String mode) throws CoreException { 820 return getType().supportsMode(mode); 821 } 822 823 826 public boolean isReadOnly() { 827 if(!isLocal()) { 828 IFile file = getFile(); 829 if(file != null) { 830 return file.isReadOnly(); 831 } 832 } 833 else { 834 File file = getLocation().toFile(); 835 if(file != null) { 836 return file.exists() && !file.canWrite(); 837 } 838 } 839 return false; 840 } 841 842 845 public ILaunchDelegate getPreferredDelegate(Set modes) throws CoreException { 846 Map delegates = getAttribute(LaunchConfiguration.ATTR_PREFERRED_LAUNCHERS, (Map )null); 847 if(delegates != null) { 848 String id = (String ) delegates.get(modes.toString()); 849 if(id != null) { 850 return getLaunchManager().getLaunchDelegate(id); 851 } 852 } 853 return null; 854 } 855 856 859 public String toString() { 860 return getName(); 861 } 862 863 } 864 865 | Popular Tags |