1 14 package org.eclipse.debug.internal.core; 15 16 17 import java.io.BufferedInputStream ; 18 import java.io.BufferedReader ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileNotFoundException ; 23 import java.io.FilenameFilter ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Collection ; 30 import java.util.Comparator ; 31 import java.util.Enumeration ; 32 import java.util.HashMap ; 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.Map ; 37 import java.util.Properties ; 38 import java.util.Set ; 39 import java.util.Map.Entry; 40 41 import javax.xml.parsers.DocumentBuilder ; 42 import javax.xml.parsers.DocumentBuilderFactory ; 43 import javax.xml.parsers.ParserConfigurationException ; 44 import javax.xml.transform.OutputKeys ; 45 import javax.xml.transform.Transformer ; 46 import javax.xml.transform.TransformerException ; 47 import javax.xml.transform.TransformerFactory ; 48 import javax.xml.transform.dom.DOMSource ; 49 import javax.xml.transform.stream.StreamResult ; 50 51 import org.eclipse.core.resources.IContainer; 52 import org.eclipse.core.resources.IFile; 53 import org.eclipse.core.resources.IProject; 54 import org.eclipse.core.resources.IResource; 55 import org.eclipse.core.resources.IResourceChangeEvent; 56 import org.eclipse.core.resources.IResourceChangeListener; 57 import org.eclipse.core.resources.IResourceDelta; 58 import org.eclipse.core.resources.IResourceDeltaVisitor; 59 import org.eclipse.core.resources.IResourceProxy; 60 import org.eclipse.core.resources.IResourceProxyVisitor; 61 import org.eclipse.core.resources.ResourcesPlugin; 62 import org.eclipse.core.runtime.CoreException; 63 import org.eclipse.core.runtime.IConfigurationElement; 64 import org.eclipse.core.runtime.IExtensionPoint; 65 import org.eclipse.core.runtime.IPath; 66 import org.eclipse.core.runtime.ISafeRunnable; 67 import org.eclipse.core.runtime.IStatus; 68 import org.eclipse.core.runtime.ListenerList; 69 import org.eclipse.core.runtime.Platform; 70 import org.eclipse.core.runtime.PlatformObject; 71 import org.eclipse.core.runtime.Preferences; 72 import org.eclipse.core.runtime.SafeRunner; 73 import org.eclipse.core.runtime.Status; 74 import org.eclipse.core.variables.VariablesPlugin; 75 import org.eclipse.debug.core.DebugException; 76 import org.eclipse.debug.core.DebugPlugin; 77 import org.eclipse.debug.core.ILaunch; 78 import org.eclipse.debug.core.ILaunchConfiguration; 79 import org.eclipse.debug.core.ILaunchConfigurationListener; 80 import org.eclipse.debug.core.ILaunchConfigurationType; 81 import org.eclipse.debug.core.ILaunchDelegate; 82 import org.eclipse.debug.core.ILaunchListener; 83 import org.eclipse.debug.core.ILaunchManager; 84 import org.eclipse.debug.core.ILaunchMode; 85 import org.eclipse.debug.core.ILaunchesListener; 86 import org.eclipse.debug.core.ILaunchesListener2; 87 import org.eclipse.debug.core.model.IDebugTarget; 88 import org.eclipse.debug.core.model.IDisconnect; 89 import org.eclipse.debug.core.model.IPersistableSourceLocator; 90 import org.eclipse.debug.core.model.IProcess; 91 import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector; 92 import org.eclipse.debug.core.sourcelookup.ISourceContainerType; 93 import org.eclipse.debug.core.sourcelookup.ISourcePathComputer; 94 import org.eclipse.debug.internal.core.sourcelookup.SourceContainerType; 95 import org.eclipse.debug.internal.core.sourcelookup.SourcePathComputer; 96 import org.eclipse.osgi.service.environment.Constants; 97 import org.w3c.dom.Document ; 98 import org.w3c.dom.Element ; 99 import org.w3c.dom.Node ; 100 import org.w3c.dom.NodeList ; 101 import org.xml.sax.InputSource ; 102 import org.xml.sax.SAXException ; 103 import org.xml.sax.helpers.DefaultHandler ; 104 105 import com.ibm.icu.text.MessageFormat; 106 107 112 public class LaunchManager extends PlatformObject implements ILaunchManager, IResourceChangeListener { 113 114 protected static final String PREF_PREFERRED_DELEGATES = DebugPlugin.getUniqueIdentifier() + ".PREFERRED_DELEGATES"; 116 121 public static final String PREF_DELETE_CONFIGS_ON_PROJECT_DELETE = DebugPlugin.getUniqueIdentifier() + ".PREF_DELETE_CONFIGS_ON_PROJECT_DELETE"; 123 128 private static final String DEBUG_UI = "org.eclipse.debug.ui"; 130 135 protected static final String EMPTY_STRING = ""; 137 142 protected static final IStatus promptStatus = new Status(IStatus.INFO, DEBUG_UI, 200, EMPTY_STRING, null); 143 144 147 private StepFilterManager fStepFilterManager = null; 148 149 153 class ConfigurationNotifier implements ISafeRunnable { 154 155 private ILaunchConfigurationListener fListener; 156 private int fType; 157 private ILaunchConfiguration fConfiguration; 158 159 162 public void handleException(Throwable exception) { 163 IStatus status = new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR, "An exception occurred during launch configuration change notification.", exception); DebugPlugin.log(status); 165 } 166 167 173 public void notify(ILaunchConfiguration configuration, int update) { 174 fConfiguration = configuration; 175 fType = update; 176 if (fLaunchConfigurationListeners.size() > 0) { 177 Object [] listeners = fLaunchConfigurationListeners.getListeners(); 178 for (int i = 0; i < listeners.length; i++) { 179 fListener = (ILaunchConfigurationListener)listeners[i]; 180 SafeRunner.run(this); 181 } 182 } 183 fConfiguration = null; 184 fListener = null; 185 } 186 187 190 public void run() throws Exception { 191 switch (fType) { 192 case ADDED: 193 fListener.launchConfigurationAdded(fConfiguration); 194 break; 195 case REMOVED: 196 fListener.launchConfigurationRemoved(fConfiguration); 197 break; 198 case CHANGED: 199 fListener.launchConfigurationChanged(fConfiguration); 200 break; 201 } 202 } 203 } 204 205 209 class LaunchesNotifier implements ISafeRunnable { 210 211 private ILaunchesListener fListener; 212 private int fType; 213 private ILaunch[] fNotifierLaunches; 214 private ILaunch[] fRegistered; 215 216 219 public void handleException(Throwable exception) { 220 IStatus status = new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR, "An exception occurred during launch change notification.", exception); DebugPlugin.log(status); 222 } 223 224 230 public void notify(ILaunch[] launches, int update) { 231 fNotifierLaunches = launches; 232 fType = update; 233 fRegistered = null; 234 Object [] copiedListeners= fLaunchesListeners.getListeners(); 235 for (int i= 0; i < copiedListeners.length; i++) { 236 fListener = (ILaunchesListener)copiedListeners[i]; 237 SafeRunner.run(this); 238 } 239 fNotifierLaunches = null; 240 fRegistered = null; 241 fListener = null; 242 } 243 244 247 public void run() throws Exception { 248 switch (fType) { 249 case ADDED: 250 fListener.launchesAdded(fNotifierLaunches); 251 break; 252 case REMOVED: 253 fListener.launchesRemoved(fNotifierLaunches); 254 break; 255 case CHANGED: 256 case TERMINATE: 257 if (fRegistered == null) { 258 List registered = null; 259 for (int j = 0; j < fNotifierLaunches.length; j++) { 260 if (isRegistered(fNotifierLaunches[j])) { 261 if (registered != null) { 262 registered.add(fNotifierLaunches[j]); 263 } 264 } else { 265 if (registered == null) { 266 registered = new ArrayList (fNotifierLaunches.length); 267 for (int k = 0; k < j; k++) { 268 registered.add(fNotifierLaunches[k]); 269 } 270 } 271 } 272 } 273 if (registered == null) { 274 fRegistered = fNotifierLaunches; 275 } else { 276 fRegistered = (ILaunch[])registered.toArray(new ILaunch[registered.size()]); 277 } 278 } 279 if (fRegistered.length > 0) { 280 if (fType == CHANGED) { 281 fListener.launchesChanged(fRegistered); 282 } 283 if (fType == TERMINATE && fListener instanceof ILaunchesListener2) { 284 ((ILaunchesListener2)fListener).launchesTerminated(fRegistered); 285 } 286 } 287 break; 288 } 289 } 290 } 291 292 295 class LaunchManagerVisitor implements IResourceDeltaVisitor { 296 297 301 private Map fFileToConfig = new HashMap (); 302 303 304 309 public void preDelete(IProject project) { 310 List list = findLaunchConfigurations(project); 311 Iterator configs = list.iterator(); 312 while (configs.hasNext()) { 313 ILaunchConfiguration configuration = (ILaunchConfiguration) configs.next(); 314 IFile file = configuration.getFile(); 315 if (file != null) { 316 fFileToConfig.put(file, configuration); 317 } 318 } 319 } 320 321 324 public void reset() { 325 fFileToConfig.clear(); 326 } 327 328 331 public boolean visit(IResourceDelta delta) { 332 if (delta == null) { 333 return false; 334 } 335 if (0 != (delta.getFlags() & IResourceDelta.OPEN)) { 336 if (delta.getResource() instanceof IProject) { 337 IProject project = (IProject)delta.getResource(); 338 339 if (project.isOpen()) { 340 LaunchManager.this.projectOpened(project); 341 } else { 342 LaunchManager.this.projectClosed(project); 343 } 344 } 345 return false; 346 } 347 IResource resource = delta.getResource(); 348 if (resource instanceof IFile) { 349 IFile file = (IFile)resource; 350 if (ILaunchConfiguration.LAUNCH_CONFIGURATION_FILE_EXTENSION.equals(file.getFileExtension())) { 351 IPath configPath = file.getLocation(); 352 ILaunchConfiguration handle = null; 353 if (configPath == null) { 355 handle = (ILaunchConfiguration) fFileToConfig.get(file); 356 } else { 357 handle = new LaunchConfiguration(configPath); 358 } 359 if (handle != null) { 360 switch (delta.getKind()) { 361 case IResourceDelta.ADDED : 362 LaunchManager.this.launchConfigurationAdded(handle); 363 break; 364 case IResourceDelta.REMOVED : 365 LaunchManager.this.launchConfigurationDeleted(handle); 366 break; 367 case IResourceDelta.CHANGED : 368 LaunchManager.this.launchConfigurationChanged(handle); 369 break; 370 } 371 } 372 } 373 return false; 374 } else if (resource instanceof IProject) { 375 if (isDeleteConfigurations()) { 376 if(delta.getKind() == IResourceDelta.REMOVED && delta.getFlags() != IResourceDelta.MOVED_TO) { 377 IProject project = (IProject) resource; 378 ArrayList configs = collectAssociatedLaunches(project); 379 if(configs.size() > 0) { 380 for(Iterator iter = configs.iterator(); iter.hasNext();) { 381 try { 382 ((ILaunchConfiguration)iter.next()).delete(); 383 } catch (CoreException e) { 384 DebugPlugin.log(e.getStatus()); 385 } 386 } 387 } 388 } 389 } 390 } 391 return true; 392 } 393 } 394 395 399 class LaunchNotifier implements ISafeRunnable { 400 401 private ILaunchListener fListener; 402 private int fType; 403 private ILaunch fLaunch; 404 405 408 public void handleException(Throwable exception) { 409 IStatus status = new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR, "An exception occurred during launch change notification.", exception); DebugPlugin.log(status); 411 } 412 413 419 public void notify(ILaunch launch, int update) { 420 fLaunch = launch; 421 fType = update; 422 Object [] copiedListeners= fListeners.getListeners(); 423 for (int i= 0; i < copiedListeners.length; i++) { 424 fListener = (ILaunchListener)copiedListeners[i]; 425 SafeRunner.run(this); 426 } 427 fLaunch = null; 428 fListener = null; 429 } 430 431 434 public void run() throws Exception { 435 switch (fType) { 436 case ADDED: 437 fListener.launchAdded(fLaunch); 438 break; 439 case REMOVED: 440 fListener.launchRemoved(fLaunch); 441 break; 442 case CHANGED: 443 if (isRegistered(fLaunch)) { 444 fListener.launchChanged(fLaunch); 445 } 446 break; 447 } 448 } 449 } 450 451 455 class ResourceProxyVisitor implements IResourceProxyVisitor { 456 457 private List fList; 458 459 protected ResourceProxyVisitor(List list) { 460 fList= list; 461 } 462 465 public boolean visit(IResourceProxy proxy) { 466 if (proxy.getType() == IResource.FILE) { 467 if (ILaunchConfiguration.LAUNCH_CONFIGURATION_FILE_EXTENSION.equalsIgnoreCase(proxy.requestFullPath().getFileExtension())) { 468 fList.add(proxy.requestResource()); 469 } 470 return false; 471 } 472 return true; 473 } 474 } 475 476 481 class PreferredDelegate { 482 private ILaunchDelegate fDelegate = null; 483 private String fTypeid = null; 484 private Set fModes = null; 485 486 public PreferredDelegate(ILaunchDelegate delegate, String typeid, Set modes) { 487 fDelegate = delegate; 488 fTypeid = typeid; 489 fModes = modes; 490 } 491 492 public String getTypeId() { 493 return fTypeid; 494 } 495 496 public Set getModes() { 497 return fModes; 498 } 499 500 public ILaunchDelegate getDelegate() { 501 return fDelegate; 502 } 503 } 504 505 508 public static final int ADDED = 0; 509 public static final int REMOVED= 1; 510 public static final int CHANGED= 2; 511 public static final int TERMINATE= 3; 512 513 517 private static HashMap fgNativeEnv= null; 518 private static HashMap fgNativeEnvCasePreserved= null; 519 520 524 protected static final IPath LOCAL_LAUNCH_CONFIGURATION_CONTAINER_PATH = 525 DebugPlugin.getDefault().getStateLocation().append(".launches"); 532 public static Document getDocument() throws ParserConfigurationException { 533 DocumentBuilderFactory dfactory= DocumentBuilderFactory.newInstance(); 534 DocumentBuilder docBuilder= dfactory.newDocumentBuilder(); 535 Document doc= docBuilder.newDocument(); 536 return doc; 537 } 538 539 548 public static String serializeDocument(Document doc) throws TransformerException , IOException { 549 ByteArrayOutputStream s = new ByteArrayOutputStream (); 550 TransformerFactory factory = TransformerFactory.newInstance(); 551 Transformer transformer = factory.newTransformer(); 552 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource (doc); 555 StreamResult outputTarget = new StreamResult (s); 556 transformer.transform(source, outputTarget); 557 return s.toString("UTF8"); } 559 560 564 private List fLaunchConfigurationTypes = null; 565 566 570 private Map fLaunchConfigurations = new HashMap (10); 571 572 575 private String [] fSortedConfigNames = null; 576 577 581 private List fLaunchConfigurationIndex = null; 582 583 587 private Map fComparators = null; 588 589 593 private Map fLaunchModes = null; 594 595 598 private HashMap fLaunchDelegates = null; 599 600 605 private Set fPreferredDelegates = null; 606 607 610 private List fLaunches= new ArrayList (10); 611 614 private Set fLaunchSet = new HashSet (10); 615 616 619 private ListenerList fListeners = new ListenerList(); 620 621 625 private ListenerList fLaunchesListeners = new ListenerList(); 626 627 631 private LaunchManagerVisitor fgVisitor; 632 633 636 private boolean fListening = false; 637 638 641 private ListenerList fLaunchConfigurationListeners = new ListenerList(); 642 643 648 private Map fSourceLocators = null; 649 650 653 private ILaunchConfiguration fFrom; 654 655 private ILaunchConfiguration fTo; 656 657 661 private Map sourceContainerTypes; 662 663 667 private Map sourcePathComputers; 668 669 672 public void addLaunch(ILaunch launch) { 673 if (internalAddLaunch(launch)) { 674 fireUpdate(launch, ADDED); 675 fireUpdate(new ILaunch[] {launch}, ADDED); 676 } 677 } 678 679 682 public void addLaunchConfigurationListener(ILaunchConfigurationListener listener) { 683 fLaunchConfigurationListeners.add(listener); 684 } 685 686 689 public void addLaunches(ILaunch[] launches) { 690 List added = new ArrayList (launches.length); 691 for (int i = 0; i < launches.length; i++) { 692 if (internalAddLaunch(launches[i])) { 693 added.add(launches[i]); 694 } 695 } 696 if (!added.isEmpty()) { 697 ILaunch[] addedLaunches = (ILaunch[])added.toArray(new ILaunch[added.size()]); 698 fireUpdate(addedLaunches, ADDED); 699 for (int i = 0; i < addedLaunches.length; i++) { 700 fireUpdate(launches[i], ADDED); 701 } 702 } 703 } 704 705 708 public void addLaunchListener(ILaunchesListener listener) { 709 fLaunchesListeners.add(listener); 710 } 711 712 715 public void addLaunchListener(ILaunchListener listener) { 716 fListeners.add(listener); 717 } 718 719 732 private void cacheNativeEnvironment(Map cache) { 733 try { 734 String nativeCommand= null; 735 boolean isWin9xME= false; String fileName= null; 737 if (Platform.getOS().equals(Constants.OS_WIN32)) { 738 String osName= System.getProperty("os.name"); isWin9xME= osName != null && (osName.startsWith("Windows 9") || osName.startsWith("Windows ME")); if (isWin9xME) { 741 IPath stateLocation= DebugPlugin.getDefault().getStateLocation(); 744 fileName= stateLocation.toOSString() + File.separator + "env.txt"; nativeCommand= "command.com /C set > " + fileName; } else { 747 nativeCommand= "cmd.exe /C set"; } 750 } else if (!Platform.getOS().equals(Constants.OS_UNKNOWN)){ 751 nativeCommand= "env"; } 753 if (nativeCommand == null) { 754 return; 755 } 756 Process process= Runtime.getRuntime().exec(nativeCommand); 757 if (isWin9xME) { 758 Properties p= new Properties (); 760 File file= new File (fileName); 761 InputStream stream = new BufferedInputStream (new FileInputStream (file)); 762 p.load(stream); 763 stream.close(); 764 if (!file.delete()) { 765 file.deleteOnExit(); } 767 for (Enumeration enumeration = p.keys(); enumeration.hasMoreElements();) { 768 String key= (String ) enumeration.nextElement(); 772 cache.put(key, p.get(key)); 774 } 775 } else { 776 InputStream stream = process.getInputStream(); 780 InputStreamReader isreader = new InputStreamReader (stream); 781 BufferedReader reader = new BufferedReader (isreader); 782 String line = reader.readLine(); 783 String key = null; 784 String value = null; 785 while (line != null) { 786 int func = line.indexOf("=()"); if(func > 0) { 788 key = line.substring(0, func); 789 value = line.substring(func+1); 791 while(line != null && !line.equals("}")) { line = reader.readLine(); 793 if(line != null) { 794 value += line; 795 } 796 } 797 line = reader.readLine(); 798 } 799 else { 800 int separator = line.indexOf('='); 801 if (separator > 0) { 802 key = line.substring(0, separator); 803 value = line.substring(separator + 1); 804 line = reader.readLine(); 805 if(line != null) { 806 separator = line.indexOf('='); 808 while(separator < 0) { 809 value += line.trim(); 810 line = reader.readLine(); 811 if(line == null) { 812 break; 814 } 815 separator = line.indexOf('='); 816 } 817 } 818 } 819 } 820 if(key != null) { 821 cache.put(key, value); 822 key = null; 823 value = null; 824 } 825 else { 826 line = reader.readLine(); 827 } 828 } 829 reader.close(); 830 } 831 } catch (IOException e) { 832 } 835 } 836 837 840 private void clearAllLaunchConfigurations() { 841 if (fLaunchConfigurationTypes != null) { 842 fLaunchConfigurationTypes.clear(); 843 } 844 if (fLaunchConfigurationIndex != null) { 845 fLaunchConfigurationIndex.clear(); 846 } 847 } 848 849 852 protected void clearConfigNameCache() { 853 fSortedConfigNames = null; 854 } 855 856 859 protected DebugException createDebugException(String message, Throwable throwable) { 860 return new DebugException( 861 new Status( 862 IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 863 DebugException.REQUEST_FAILED, message, throwable 864 ) 865 ); 866 } 867 868 874 protected LaunchConfigurationInfo createInfoFromXML(InputStream stream) throws CoreException, 875 ParserConfigurationException , 876 IOException , 877 SAXException { 878 Element root = null; 879 DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 880 parser.setErrorHandler(new DefaultHandler ()); 881 root = parser.parse(new InputSource (stream)).getDocumentElement(); 882 LaunchConfigurationInfo info = new LaunchConfigurationInfo(); 883 info.initializeFromXML(root); 884 return info; 885 } 886 887 896 protected List findLaunchConfigurations(IContainer container) { 897 List list = new ArrayList (10); 898 if (container instanceof IProject && !((IProject)container).isOpen()) { 899 return list; 900 } 901 ResourceProxyVisitor visitor= new ResourceProxyVisitor(list); 902 try { 903 container.accept(visitor, IResource.NONE); 904 } catch (CoreException ce) { 905 } 907 Iterator iter = list.iterator(); 908 List configs = new ArrayList (list.size()); 909 IFile file = null; 910 while (iter.hasNext()) { 911 file = (IFile)iter.next(); 912 configs.add(getLaunchConfiguration(file)); 913 } 914 return configs; 915 } 916 917 924 protected List findLocalLaunchConfigurations() { 925 IPath containerPath = LOCAL_LAUNCH_CONFIGURATION_CONTAINER_PATH; 926 List configs = new ArrayList (10); 927 final File directory = containerPath.toFile(); 928 if (directory.isDirectory()) { 929 FilenameFilter filter = new FilenameFilter () { 930 public boolean accept(File dir, String name) { 931 return dir.equals(directory) && 932 name.endsWith(ILaunchConfiguration.LAUNCH_CONFIGURATION_FILE_EXTENSION); 933 } 934 }; 935 String [] files = directory.list(filter); 936 LaunchConfiguration config = null; 937 for (int i = 0; i < files.length; i++) { 938 config = new LaunchConfiguration(containerPath.append(files[i])); 939 configs.add(config); 940 } 941 } 942 return configs; 943 } 944 945 952 public void fireUpdate(ILaunch launch, int update) { 953 new LaunchNotifier().notify(launch, update); 954 } 955 956 963 public void fireUpdate(ILaunch[] launches, int update) { 964 new LaunchesNotifier().notify(launches, update); 965 } 966 967 970 public String generateUniqueLaunchConfigurationNameFrom(String baseName) { 971 int index = 1; 972 int length= baseName.length(); 973 int copyIndex = baseName.lastIndexOf(" ("); if (copyIndex > -1 && length > copyIndex + 2 && baseName.charAt(length - 1) == ')') { 975 String trailer = baseName.substring(copyIndex + 2, length -1); 976 if (isNumber(trailer)) { 977 try { 978 index = Integer.parseInt(trailer); 979 baseName = baseName.substring(0, copyIndex); 980 } 981 catch (NumberFormatException nfe) {} 982 } 983 } 984 String newName = baseName; 985 while (isExistingLaunchConfigurationName(newName)) { 986 newName = MessageFormat.format(DebugCoreMessages.LaunchManager_31, new String [] {baseName, Integer.toString(index)}); 987 index++; 988 } 989 990 return newName; 991 } 992 993 1011 public String generateUniqueLaunchConfigurationNameFrom(String basename, Set reservednames) { 1012 if(reservednames == null) { 1013 return generateUniqueLaunchConfigurationNameFrom(basename); 1014 } 1015 int index = 1; 1016 int length= basename.length(); 1017 String base = basename; 1018 int copyIndex = base.lastIndexOf(" ("); if (copyIndex > -1 && length > copyIndex + 2 && base.charAt(length - 1) == ')') { 1020 String trailer = base.substring(copyIndex + 2, length -1); 1021 if (isNumber(trailer)) { 1022 try { 1023 index = Integer.parseInt(trailer); 1024 base = base.substring(0, copyIndex); 1025 } 1026 catch (NumberFormatException nfe) {} 1027 } 1028 } 1029 String newname = base; 1030 StringBuffer buffer = null; 1031 while (isExistingLaunchConfigurationName(newname) || reservednames.contains(newname)) { 1032 buffer = new StringBuffer (base); 1033 buffer.append(" ("); buffer.append(String.valueOf(index)); 1035 index++; 1036 buffer.append(')'); 1037 newname = buffer.toString(); 1038 } 1039 return newname; 1040 } 1041 1042 1048 private synchronized List getAllLaunchConfigurations() { 1049 if (fLaunchConfigurationIndex == null) { 1050 try { 1051 fLaunchConfigurationIndex = new ArrayList (20); 1052 List configs = findLocalLaunchConfigurations(); 1053 verifyConfigurations(configs, fLaunchConfigurationIndex); 1054 configs = findLaunchConfigurations(ResourcesPlugin.getWorkspace().getRoot()); 1055 verifyConfigurations(configs, fLaunchConfigurationIndex); 1056 } finally { 1057 hookResourceChangeListener(); 1058 } 1059 } 1060 return fLaunchConfigurationIndex; 1061 } 1062 1063 1068 protected String [] getAllSortedConfigNames() { 1069 if (fSortedConfigNames == null) { 1070 ILaunchConfiguration[] configs = getLaunchConfigurations(); 1071 fSortedConfigNames = new String [configs.length]; 1072 for (int i = 0; i < configs.length; i++) { 1073 fSortedConfigNames[i] = configs[i].getName(); 1074 } 1075 Arrays.sort(fSortedConfigNames); 1076 } 1077 return fSortedConfigNames; 1078 } 1079 1080 1087 protected Comparator getComparator(String attributeName) { 1088 Map map = getComparators(); 1089 return (Comparator )map.get(attributeName); 1090 } 1091 1092 1095 protected Map getComparators() { 1096 initializeComparators(); 1097 return fComparators; 1098 } 1099 1100 1108 protected List getConfigsFromXML(Element root) throws CoreException { 1109 DebugException invalidFormat = 1110 new DebugException( 1111 new Status( 1112 IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 1113 DebugException.REQUEST_FAILED, DebugCoreMessages.LaunchManager_Invalid_launch_configuration_index__18, null 1114 ) 1115 ); 1116 1117 if (!root.getNodeName().equalsIgnoreCase("launchConfigurations")) { throw invalidFormat; 1119 } 1120 1121 List configs = new ArrayList (4); 1123 NodeList list = root.getChildNodes(); 1124 int length = list.getLength(); 1125 Node node = null; 1126 Element entry = null; 1127 String memento = null; 1128 for (int i = 0; i < length; ++i) { 1129 node = list.item(i); 1130 short type = node.getNodeType(); 1131 if (type == Node.ELEMENT_NODE) { 1132 entry = (Element) node; 1133 if (!entry.getNodeName().equals("launchConfiguration")) { throw invalidFormat; 1135 } 1136 memento = entry.getAttribute("memento"); if (memento == null) { 1138 throw invalidFormat; 1139 } 1140 configs.add(getLaunchConfiguration(memento)); 1141 } 1142 } 1143 return configs; 1144 } 1145 1146 protected ConfigurationNotifier getConfigurationNotifier() { 1147 return new ConfigurationNotifier(); 1148 } 1149 1150 1153 public IDebugTarget[] getDebugTargets() { 1154 synchronized (fLaunches) { 1155 List allTargets= new ArrayList (fLaunches.size()); 1156 if (fLaunches.size() > 0) { 1157 Iterator e = fLaunches.iterator(); 1158 IDebugTarget[] targets = null; 1159 while (e.hasNext()) { 1160 targets = ((ILaunch) e.next()).getDebugTargets(); 1161 for (int i = 0; i < targets.length; i++) { 1162 allTargets.add(targets[i]); 1163 } 1164 } 1165 } 1166 return (IDebugTarget[])allTargets.toArray(new IDebugTarget[allTargets.size()]); 1167 } 1168 } 1169 1170 1175 private LaunchManagerVisitor getDeltaVisitor() { 1176 if (fgVisitor == null) { 1177 fgVisitor= new LaunchManagerVisitor(); 1178 } 1179 return fgVisitor; 1180 } 1181 1182 1183 1186 public String [] getEnvironment(ILaunchConfiguration configuration) throws CoreException { 1187 Map configEnv = configuration.getAttribute(ATTR_ENVIRONMENT_VARIABLES, (Map ) null); 1188 if (configEnv == null) { 1189 return null; 1190 } 1191 Map env = new HashMap (); 1192 boolean append = configuration.getAttribute(ATTR_APPEND_ENVIRONMENT_VARIABLES, true); 1194 if (append) { 1195 env.putAll(getNativeEnvironmentCasePreserved()); 1196 } 1197 1198 Iterator iter= configEnv.entrySet().iterator(); 1200 boolean win32= Platform.getOS().equals(Constants.OS_WIN32); 1201 Map.Entry entry = null; 1202 String key = null; 1203 String value = null; 1204 Object nativeValue = null; 1205 Iterator envIter = null; 1206 Map.Entry nativeEntry = null; 1207 String nativeKey = null; 1208 while (iter.hasNext()) { 1209 entry = (Map.Entry ) iter.next(); 1210 key = (String ) entry.getKey(); 1211 value = (String ) entry.getValue(); 1212 if (value != null) { 1214 value = VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(value); 1215 } 1216 boolean added= false; 1217 if (win32) { 1218 nativeValue = env.get(key); 1220 if (nativeValue != null) { 1221 env.put(key, value); 1223 } else { 1224 envIter = env.entrySet().iterator(); 1228 while (envIter.hasNext()) { 1229 nativeEntry = (Map.Entry ) envIter.next(); 1230 nativeKey = (String ) (nativeEntry).getKey(); 1231 if (nativeKey.equalsIgnoreCase(key)) { 1232 nativeEntry.setValue(value); 1233 added = true; 1234 break; 1235 } 1236 } 1237 } 1238 } 1239 if (!added) { 1240 env.put(key, value); 1241 } 1242 } 1243 1244 iter = env.entrySet().iterator(); 1245 List strings = new ArrayList (env.size()); 1246 StringBuffer buffer = null; 1247 while (iter.hasNext()) { 1248 entry = (Map.Entry ) iter.next(); 1249 buffer = new StringBuffer ((String ) entry.getKey()); 1250 buffer.append('=').append((String ) entry.getValue()); 1251 strings.add(buffer.toString()); 1252 } 1253 return (String []) strings.toArray(new String [strings.size()]); 1254 } 1255 1256 1265 protected LaunchConfigurationInfo getInfo(ILaunchConfiguration config) throws CoreException { 1266 LaunchConfigurationInfo info = (LaunchConfigurationInfo)fLaunchConfigurations.get(config); 1267 if (info == null) { 1268 if (config.exists()) { 1269 InputStream stream = null; 1270 try { 1271 if (config.isLocal()) { 1272 IPath path = config.getLocation(); 1273 File file = path.toFile(); 1274 stream = new BufferedInputStream (new FileInputStream (file)); 1275 } else { 1276 IFile file = ((LaunchConfiguration) config).getFile(); 1277 if (file == null) { 1278 throw createDebugException(MessageFormat.format(DebugCoreMessages.LaunchManager_30, new String [] {config.getName()}), null); 1279 } 1280 stream = file.getContents(true); 1281 } 1282 info = createInfoFromXML(stream); 1283 synchronized (this) { 1284 fLaunchConfigurations.put(config, info); 1285 } 1286 } catch (FileNotFoundException e) { 1287 throwException(config, e); 1288 } catch (SAXException e) { 1289 throwException(config, e); 1290 } catch (ParserConfigurationException e) { 1291 throwException(config, e); 1292 } catch (IOException e) { 1293 throwException(config, e); 1294 } finally { 1295 if (stream != null) { 1296 try { 1297 stream.close(); 1298 } catch (IOException e) { 1299 throwException(config, e); 1300 } 1301 } 1302 } 1303 1304 } else { 1305 throw createDebugException( 1306 MessageFormat.format(DebugCoreMessages.LaunchManager_does_not_exist, new String []{config.getName(), config.getLocation().toOSString()}), null); 1307 } 1308 } 1309 return info; 1310 } 1311 1312 1315 public ILaunchConfiguration getLaunchConfiguration(IFile file) { 1316 hookResourceChangeListener(); 1317 return new LaunchConfiguration(file.getLocation()); 1318 } 1319 1320 1323 public ILaunchConfiguration getLaunchConfiguration(String memento) throws CoreException { 1324 hookResourceChangeListener(); 1325 return new LaunchConfiguration(memento); 1326 } 1327 1328 1331 public synchronized ILaunchConfiguration[] getLaunchConfigurations() { 1332 List allConfigs = getAllLaunchConfigurations(); 1333 return (ILaunchConfiguration[])allConfigs.toArray(new ILaunchConfiguration[allConfigs.size()]); 1334 } 1335 1336 1339 public synchronized ILaunchConfiguration[] getLaunchConfigurations(ILaunchConfigurationType type) throws CoreException { 1340 Iterator iter = getAllLaunchConfigurations().iterator(); 1341 List configs = new ArrayList (); 1342 ILaunchConfiguration config = null; 1343 while (iter.hasNext()) { 1344 config = (ILaunchConfiguration)iter.next(); 1345 if (config.getType().equals(type)) { 1346 configs.add(config); 1347 } 1348 } 1349 return (ILaunchConfiguration[])configs.toArray(new ILaunchConfiguration[configs.size()]); 1350 } 1351 1352 1360 protected synchronized List getLaunchConfigurations(IProject project) { 1361 Iterator iter = getAllLaunchConfigurations().iterator(); 1362 List configs = new ArrayList (); 1363 ILaunchConfiguration config = null; 1364 IFile file = null; 1365 while (iter.hasNext()) { 1366 config = (ILaunchConfiguration)iter.next(); 1367 file = config.getFile(); 1368 if (file != null && file.getProject().equals(project)) { 1369 configs.add(config); 1370 } 1371 } 1372 return configs; 1373 } 1374 1375 1378 public ILaunchConfigurationType getLaunchConfigurationType(String id) { 1379 ILaunchConfigurationType[] types = getLaunchConfigurationTypes(); 1380 for(int i = 0; i < types.length; i++) { 1381 if (types[i].getIdentifier().equals(id)) { 1382 return types[i]; 1383 } 1384 } 1385 return null; 1386 } 1387 1388 1391 public ILaunchConfigurationType[] getLaunchConfigurationTypes() { 1392 initializeLaunchConfigurationTypes(); 1393 return (ILaunchConfigurationType[])fLaunchConfigurationTypes.toArray(new ILaunchConfigurationType[fLaunchConfigurationTypes.size()]); 1394 } 1395 1396 1399 public ILaunch[] getLaunches() { 1400 synchronized (fLaunches) { 1401 return (ILaunch[])fLaunches.toArray(new ILaunch[fLaunches.size()]); 1402 } 1403 } 1404 1405 1408 public ILaunchMode getLaunchMode(String mode) { 1409 initializeLaunchModes(); 1410 return (ILaunchMode) fLaunchModes.get(mode); 1411 } 1412 1413 1416 public ILaunchMode[] getLaunchModes() { 1417 initializeLaunchModes(); 1418 Collection collection = fLaunchModes.values(); 1419 return (ILaunchMode[]) collection.toArray(new ILaunchMode[collection.size()]); 1420 } 1421 1422 1430 public ILaunchDelegate[] getLaunchDelegates() { 1431 initializeLaunchDelegates(); 1432 Collection col = fLaunchDelegates.values(); 1433 return (ILaunchDelegate[]) col.toArray(new ILaunchDelegate[col.size()]); 1434 } 1435 1436 1445 public LaunchDelegate[] getLaunchDelegates(String typeid) { 1446 initializeLaunchDelegates(); 1447 ArrayList list = new ArrayList (); 1448 LaunchDelegate ld = null; 1449 for(Iterator iter = fLaunchDelegates.keySet().iterator(); iter.hasNext();) { 1450 ld = (LaunchDelegate) fLaunchDelegates.get(iter.next()); 1451 if(ld.getLaunchConfigurationTypeId().equals(typeid)) { 1452 list.add(ld); 1453 } 1454 } 1455 return (LaunchDelegate[]) list.toArray(new LaunchDelegate[list.size()]); 1456 } 1457 1458 1466 public ILaunchDelegate getLaunchDelegate(String id) { 1467 if(id != null) { 1468 ILaunchDelegate[] delegates = getLaunchDelegates(); 1469 for(int i = 0; i < delegates.length; i++) { 1470 if(id.equals(delegates[i].getId())) { 1471 return delegates[i]; 1472 } 1473 } 1474 } 1475 return null; 1476 } 1477 1478 1483 private synchronized void initializeLaunchDelegates() { 1484 if(fLaunchDelegates == null) { 1485 fLaunchDelegates = new HashMap (); 1486 IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(DebugPlugin.getUniqueIdentifier(), DebugPlugin.EXTENSION_POINT_LAUNCH_DELEGATES); 1488 IConfigurationElement[] infos = extensionPoint.getConfigurationElements(); 1489 LaunchDelegate delegate = null; 1490 for(int i = 0; i < infos.length; i++) { 1491 delegate = new LaunchDelegate(infos[i]); 1492 fLaunchDelegates.put(delegate.getId(), delegate); 1493 } 1494 extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(DebugPlugin.getUniqueIdentifier(), DebugPlugin.EXTENSION_POINT_LAUNCH_CONFIGURATION_TYPES); 1496 infos = extensionPoint.getConfigurationElements(); 1497 for(int i = 0; i < infos.length; i++) { 1498 if(infos[i].getAttribute(IConfigurationElementConstants.DELEGATE) != null) { 1500 delegate = new LaunchDelegate(infos[i]); 1501 fLaunchDelegates.put(delegate.getId(), delegate); 1502 } 1503 } 1504 } 1505 } 1506 1507 1517 private synchronized void initializePreferredDelegates() { 1518 if(fPreferredDelegates == null) { 1519 fPreferredDelegates = new HashSet (); 1520 Preferences prefs = DebugPlugin.getDefault().getPluginPreferences(); 1521 String preferred = prefs.getString(LaunchManager.PREF_PREFERRED_DELEGATES); 1522 if(!EMPTY_STRING.equals(preferred)) { 1523 try { 1524 Element root = DebugPlugin.parseDocument(preferred); 1525 NodeList nodes = root.getElementsByTagName(IConfigurationElementConstants.DELEGATE); 1526 Element element = null; 1527 String typeid = null; 1528 Set modeset = null; 1529 List modesets = null; 1530 LaunchDelegate[] extensions = null; 1531 ILaunchDelegate delegate = null; 1532 for(int i = 0; i < nodes.getLength(); i++) { 1533 element = (Element) nodes.item(i); 1534 typeid = element.getAttribute(IConfigurationElementConstants.TYPE_ID); 1535 extensions = getLaunchDelegates(typeid); 1536 for(int j = 0; j < extensions.length; j++) { 1537 if(element.getAttribute(IConfigurationElementConstants.ID).equals(extensions[j].getId())) { 1538 modesets = extensions[j].getModes(); 1539 String [] modes = element.getAttribute(IConfigurationElementConstants.MODES).split(","); modeset = new HashSet (Arrays.asList(modes)); 1541 if(modesets.contains(modeset)) { 1542 delegate = extensions[j]; 1543 break; 1544 } 1545 } 1546 } 1547 if(delegate != null & !EMPTY_STRING.equals(typeid) & modeset != null) { 1549 fPreferredDelegates.add(new PreferredDelegate(delegate, typeid, modeset)); 1550 } 1551 delegate = null; 1552 } 1553 } 1554 catch (CoreException e) {DebugPlugin.log(e);} 1555 } 1556 } 1557 } 1558 1559 1567 protected ILaunchDelegate getPreferredDelegate(String typeid, Set modes) { 1568 initializePreferredDelegates(); 1569 PreferredDelegate pd = null; 1570 for(Iterator iter = fPreferredDelegates.iterator(); iter.hasNext();) { 1571 pd = (PreferredDelegate) iter.next(); 1572 if(pd.getModes().equals(modes) & pd.getTypeId().equals(typeid)) { 1573 return pd.getDelegate(); 1574 } 1575 } 1576 return null; 1577 } 1578 1579 1584 protected synchronized List getLocalLaunchConfigurations() { 1585 Iterator iter = getAllLaunchConfigurations().iterator(); 1586 List configs = new ArrayList (); 1587 ILaunchConfiguration config = null; 1588 while (iter.hasNext()) { 1589 config = (ILaunchConfiguration)iter.next(); 1590 if (config.isLocal()) { 1591 configs.add(config); 1592 } 1593 } 1594 return configs; 1595 } 1596 1597 1603 public ILaunchConfiguration[] getMappedConfigurations(IResource resource) { 1604 List configurations = new ArrayList (); 1605 try { 1606 ILaunchConfiguration[] configs = getLaunchConfigurations(); 1607 IResource[] resources = null; 1608 for(int i = 0; i < configs.length; i++) { 1609 resources = configs[i].getMappedResources(); 1610 if(resources != null) { 1611 for(int j = 0; j < resources.length; j++) { 1612 if(resources[j].equals(resource)) { 1613 configurations.add(configs[i]); 1614 } 1615 } 1616 } 1617 } 1618 } 1619 catch(CoreException e) {DebugPlugin.log(e);} 1620 return (ILaunchConfiguration[])configurations.toArray(new ILaunchConfiguration[configurations.size()]); 1621 } 1622 1623 1626 public ILaunchConfiguration[] getMigrationCandidates() throws CoreException { 1627 List configs = new ArrayList (); 1628 ILaunchConfiguration[] candidates = getLaunchConfigurations(); 1629 for(int i = 0; i < candidates.length; i++) { 1630 if(candidates[i].isMigrationCandidate()) { 1631 configs.add(candidates[i]); 1632 } 1633 } 1634 return (ILaunchConfiguration[])configs.toArray(new ILaunchConfiguration[configs.size()]); 1635 } 1636 1637 1640 public ILaunchConfiguration getMovedFrom(ILaunchConfiguration addedConfiguration) { 1641 if (addedConfiguration.equals(fTo)) { 1642 return fFrom; 1643 } 1644 return null; 1645 } 1646 1647 1650 public ILaunchConfiguration getMovedTo(ILaunchConfiguration removedConfiguration) { 1651 if (removedConfiguration.equals(fFrom)) { 1652 return fTo; 1653 } 1654 return null; 1655 } 1656 1657 1660 public synchronized Map getNativeEnvironment() { 1661 if (fgNativeEnv == null) { 1662 Map casePreserved = getNativeEnvironmentCasePreserved(); 1663 if (Platform.getOS().equals(Constants.OS_WIN32)) { 1664 fgNativeEnv= new HashMap (); 1665 Iterator entries = casePreserved.entrySet().iterator(); 1666 Map.Entry entry = null; 1667 String key = null; 1668 while (entries.hasNext()) { 1669 entry = (Entry) entries.next(); 1670 key = ((String )entry.getKey()).toUpperCase(); 1671 fgNativeEnv.put(key, entry.getValue()); 1672 } 1673 } else { 1674 fgNativeEnv = new HashMap (casePreserved); 1675 } 1676 } 1677 return new HashMap (fgNativeEnv); 1678 } 1679 1680 1683 public synchronized Map getNativeEnvironmentCasePreserved() { 1684 if (fgNativeEnvCasePreserved == null) { 1685 fgNativeEnvCasePreserved= new HashMap (); 1686 cacheNativeEnvironment(fgNativeEnvCasePreserved); 1687 } 1688 return new HashMap (fgNativeEnvCasePreserved); 1689 } 1690 1691 1694 public IProcess[] getProcesses() { 1695 synchronized (fLaunches) { 1696 List allProcesses = new ArrayList (fLaunches.size()); 1697 Iterator e = fLaunches.iterator(); 1698 IProcess[] processes = null; 1699 while (e.hasNext()) { 1700 processes = ((ILaunch) e.next()).getProcesses(); 1701 for (int i= 0; i < processes.length; i++) { 1702 allProcesses.add(processes[i]); 1703 } 1704 } 1705 return (IProcess[])allProcesses.toArray(new IProcess[allProcesses.size()]); 1706 } 1707 } 1708 1709 1712 public ISourceContainerType getSourceContainerType(String id) { 1713 initializeSourceContainerTypes(); 1714 return (ISourceContainerType) sourceContainerTypes.get(id); 1715 } 1716 1717 1720 public ISourceContainerType[] getSourceContainerTypes() { 1721 initializeSourceContainerTypes(); 1722 Collection containers = sourceContainerTypes.values(); 1723 return (ISourceContainerType[]) containers.toArray(new ISourceContainerType[containers.size()]); 1724 } 1725 1726 1729 public ISourcePathComputer getSourcePathComputer(ILaunchConfiguration configuration) throws CoreException { 1730 String id = null; 1731 id = configuration.getAttribute(ISourcePathComputer.ATTR_SOURCE_PATH_COMPUTER_ID, (String )null); 1732 1733 if (id == null) { 1734 return configuration.getType().getSourcePathComputer(); 1736 } 1737 return getSourcePathComputer(id); 1738 } 1739 1740 1743 public ISourcePathComputer getSourcePathComputer(String id) { 1744 initializeSourceContainerTypes(); 1745 return (ISourcePathComputer) sourcePathComputers.get(id); 1746 } 1747 1748 1751 private synchronized void hookResourceChangeListener() { 1752 if (!fListening) { 1753 ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE | IResourceChangeEvent.PRE_DELETE); 1754 fListening = true; 1755 } 1756 } 1757 1758 1761 private synchronized void initializeComparators() { 1762 if (fComparators == null) { 1763 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(DebugPlugin.getUniqueIdentifier(), DebugPlugin.EXTENSION_POINT_LAUNCH_CONFIGURATION_COMPARATORS); 1764 IConfigurationElement[] infos= extensionPoint.getConfigurationElements(); 1765 fComparators = new HashMap (infos.length); 1766 IConfigurationElement configurationElement = null; 1767 String attr = null; 1768 for (int i= 0; i < infos.length; i++) { 1769 configurationElement = infos[i]; 1770 attr = configurationElement.getAttribute("attribute"); if (attr != null) { 1772 fComparators.put(attr, new LaunchConfigurationComparator(configurationElement)); 1773 } else { 1774 IStatus s = new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), DebugException.INTERNAL_ERROR, 1776 MessageFormat.format("Invalid launch configuration comparator extension defined by plug-in {0} - attribute not specified.", new String [] {configurationElement.getContributor().getName()}), null); DebugPlugin.log(s); 1778 } 1779 } 1780 } 1781 } 1782 1783 1786 private synchronized void initializeLaunchConfigurationTypes() { 1787 if (fLaunchConfigurationTypes == null) { 1788 hookResourceChangeListener(); 1789 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(DebugPlugin.getUniqueIdentifier(), DebugPlugin.EXTENSION_POINT_LAUNCH_CONFIGURATION_TYPES); 1790 IConfigurationElement[] infos = extensionPoint.getConfigurationElements(); 1791 fLaunchConfigurationTypes = new ArrayList (infos.length); 1792 for (int i= 0; i < infos.length; i++) { 1793 fLaunchConfigurationTypes.add(new LaunchConfigurationType(infos[i])); 1794 } 1795 } 1796 } 1797 1798 1805 private synchronized void initializeLaunchModes() { 1806 if (fLaunchModes == null) { 1807 try { 1808 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(DebugPlugin.getUniqueIdentifier(), DebugPlugin.EXTENSION_POINT_LAUNCH_MODES); 1809 IConfigurationElement[] infos= extensionPoint.getConfigurationElements(); 1810 fLaunchModes = new HashMap (); 1811 ILaunchMode mode = null; 1812 for (int i= 0; i < infos.length; i++) { 1813 mode = new LaunchMode(infos[i]); 1814 fLaunchModes.put(mode.getIdentifier(), mode); 1815 } 1816 } 1817 catch (CoreException e) {DebugPlugin.log(e);} 1818 } 1819 } 1820 1821 1824 private synchronized void initializeSourceContainerTypes() { 1825 if (sourceContainerTypes == null) { 1826 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(DebugPlugin.getUniqueIdentifier(), DebugPlugin.EXTENSION_POINT_SOURCE_CONTAINER_TYPES); 1827 IConfigurationElement[] extensions = extensionPoint.getConfigurationElements(); 1828 sourceContainerTypes = new HashMap (); 1829 for (int i = 0; i < extensions.length; i++) { 1830 sourceContainerTypes.put( 1831 extensions[i].getAttribute(IConfigurationElementConstants.ID), 1832 new SourceContainerType(extensions[i])); 1833 } 1834 extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(DebugPlugin.getUniqueIdentifier(), DebugPlugin.EXTENSION_POINT_SOURCE_PATH_COMPUTERS); 1835 extensions = extensionPoint.getConfigurationElements(); 1836 sourcePathComputers = new HashMap (); 1837 for (int i = 0; i < extensions.length; i++) { 1838 sourcePathComputers.put( 1839 extensions[i].getAttribute(IConfigurationElementConstants.ID), 1840 new SourcePathComputer(extensions[i])); 1841 } 1842 } 1843 } 1844 1845 1851 private synchronized void initializeSourceLocators() { 1852 if (fSourceLocators == null) { 1853 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(DebugPlugin.getUniqueIdentifier(), DebugPlugin.EXTENSION_POINT_SOURCE_LOCATORS); 1854 IConfigurationElement[] infos= extensionPoint.getConfigurationElements(); 1855 fSourceLocators= new HashMap (infos.length); 1856 IConfigurationElement configurationElement = null; 1857 String id = null; 1858 for (int i= 0; i < infos.length; i++) { 1859 configurationElement = infos[i]; 1860 id = configurationElement.getAttribute(IConfigurationElementConstants.ID); 1861 if (id != null) { 1862 fSourceLocators.put(id,configurationElement); 1863 } else { 1864 IStatus s = new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), DebugException.INTERNAL_ERROR, 1866 MessageFormat.format("Invalid source locator extension defined by plug-in \"{0}\": \"id\" not specified.", new String [] {configurationElement.getContributor().getName()} ), null); DebugPlugin.log(s); 1868 } 1869 } 1870 } 1871 } 1872 1873 1880 protected boolean internalAddLaunch(ILaunch launch) { 1881 getStepFilterManager(); 1883 synchronized (fLaunches) { 1884 if (fLaunches.contains(launch)) { 1885 return false; 1886 } 1887 fLaunches.add(launch); 1888 fLaunchSet.add(launch); 1889 return true; 1890 } 1891 } 1892 1893 1900 protected boolean internalRemoveLaunch(ILaunch launch) { 1901 if (launch == null) { 1902 return false; 1903 } 1904 synchronized (fLaunches) { 1905 fLaunchSet.remove(launch); 1906 return fLaunches.remove(launch); 1907 } 1908 } 1909 1912 public boolean isExistingLaunchConfigurationName(String name) { 1913 String [] sortedConfigNames = getAllSortedConfigNames(); 1914 int index = Arrays.binarySearch(sortedConfigNames, name); 1915 if (index < 0) { 1916 return false; 1917 } 1918 return true; 1919 } 1920 1921 1924 private boolean isNumber(String string) { 1925 int numChars= string.length(); 1926 if (numChars == 0) { 1927 return false; 1928 } 1929 for (int i= 0; i < numChars; i++) { 1930 if (!Character.isDigit(string.charAt(i))) { 1931 return false; 1932 } 1933 } 1934 return true; 1935 } 1936 1937 1943 private boolean isDeleteConfigurations() { 1944 return DebugPlugin.getDefault().getPluginPreferences().getBoolean(PREF_DELETE_CONFIGS_ON_PROJECT_DELETE); 1945 } 1946 1947 1950 public boolean isRegistered(ILaunch launch) { 1951 synchronized (fLaunches) { 1952 return fLaunchSet.contains(launch); 1953 } 1954 } 1955 1956 1963 protected boolean isValid(ILaunchConfiguration config) { 1964 try { 1965 config.getType(); 1966 } catch (CoreException e) { 1967 if (e.getStatus().getCode() != DebugException.MISSING_LAUNCH_CONFIGURATION_TYPE) { 1968 DebugPlugin.log(e); 1971 } 1972 return false; 1973 } 1974 return true; 1975 } 1976 1977 1984 protected void launchConfigurationAdded(ILaunchConfiguration config) { 1985 if (config.isWorkingCopy()) { 1986 return; 1987 } 1988 if (isValid(config)) { 1989 boolean added = false; 1990 synchronized (this) { 1991 List allConfigs = getAllLaunchConfigurations(); 1992 if (!allConfigs.contains(config)) { 1993 allConfigs.add(config); 1994 added = true; 1995 } 1996 } 1997 if (added) { 1998 getConfigurationNotifier().notify(config, ADDED); 1999 clearConfigNameCache(); 2000 } 2001 } else { 2002 launchConfigurationDeleted(config); 2003 } 2004 } 2005 2006 2015 protected void launchConfigurationChanged(ILaunchConfiguration config) { 2016 synchronized(this) { 2017 fLaunchConfigurations.remove(config); 2018 } 2019 clearConfigNameCache(); 2020 if (isValid(config)) { 2021 launchConfigurationAdded(config); 2025 getConfigurationNotifier().notify(config, CHANGED); 2026 } else { 2027 launchConfigurationDeleted(config); 2028 } 2029 } 2030 2031 2039 protected void launchConfigurationDeleted(ILaunchConfiguration config) { 2040 synchronized (this) { 2041 fLaunchConfigurations.remove(config); 2042 getAllLaunchConfigurations().remove(config); 2043 } 2044 getConfigurationNotifier().notify(config, REMOVED); 2045 clearConfigNameCache(); 2046 } 2047 2048 2051 public IPersistableSourceLocator newSourceLocator(String identifier) throws CoreException { 2052 initializeSourceLocators(); 2053 IConfigurationElement config = (IConfigurationElement)fSourceLocators.get(identifier); 2054 if (config == null) { 2055 throw new CoreException(new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), DebugException.INTERNAL_ERROR, 2056 MessageFormat.format(DebugCoreMessages.LaunchManager_Source_locator_does_not_exist___0__13, new String [] {identifier} ), null)); 2057 } 2058 IPersistableSourceLocator sourceLocator = (IPersistableSourceLocator)config.createExecutableExtension("class"); if (sourceLocator instanceof AbstractSourceLookupDirector) { 2060 ((AbstractSourceLookupDirector)sourceLocator).setId(identifier); 2061 } 2062 return sourceLocator; 2063 } 2064 2065 2072 protected void projectClosed(IProject project) { 2073 List configs = getLaunchConfigurations(project); 2074 if (!configs.isEmpty()) { 2075 Iterator iterator = configs.iterator(); 2076 while (iterator.hasNext()) { 2077 launchConfigurationDeleted((ILaunchConfiguration)iterator.next()); 2078 } 2079 } 2080 terminateMappedConfigurations(project); 2082 } 2083 2084 2091 protected void projectOpened(IProject project) { 2092 List configs = findLaunchConfigurations(project); 2093 if (!configs.isEmpty()) { 2094 Iterator iterator = configs.iterator(); 2095 while (iterator.hasNext()) { 2096 launchConfigurationAdded((ILaunchConfiguration) iterator.next()); 2097 } 2098 } 2099 } 2100 2101 2104 public void removeLaunch(final ILaunch launch) { 2105 if (internalRemoveLaunch(launch)) { 2106 fireUpdate(launch, REMOVED); 2107 fireUpdate(new ILaunch[] {launch}, REMOVED); 2108 } 2109 } 2110 2111 2114 public void removeLaunchConfigurationListener(ILaunchConfigurationListener listener) { 2115 fLaunchConfigurationListeners.remove(listener); 2116 } 2117 2118 2121 public void removeLaunches(ILaunch[] launches) { 2122 List removed = new ArrayList (launches.length); 2123 for (int i = 0; i < launches.length; i++) { 2124 if (internalRemoveLaunch(launches[i])) { 2125 removed.add(launches[i]); 2126 } 2127 } 2128 if (!removed.isEmpty()) { 2129 ILaunch[] removedLaunches = (ILaunch[])removed.toArray(new ILaunch[removed.size()]); 2130 fireUpdate(removedLaunches, REMOVED); 2131 for (int i = 0; i < removedLaunches.length; i++) { 2132 fireUpdate(removedLaunches[i], REMOVED); 2133 } 2134 } 2135 } 2136 2139 public void removeLaunchListener(ILaunchesListener listener) { 2140 fLaunchesListeners.remove(listener); 2141 } 2142 2143 2146 public void removeLaunchListener(ILaunchListener listener) { 2147 fListeners.remove(listener); 2148 } 2149 2150 2156 public void resourceChanged(IResourceChangeEvent event) { 2157 IResourceDelta delta = event.getDelta(); 2158 if (delta == null) { 2159 LaunchManagerVisitor visitor = getDeltaVisitor(); 2161 IResource resource = event.getResource(); 2162 if (resource instanceof IProject) { 2163 IProject project = (IProject) resource; 2164 visitor.preDelete(project); 2165 } 2166 } else { 2167 LaunchManagerVisitor visitor = getDeltaVisitor(); 2168 try { 2169 delta.accept(visitor); 2170 } catch (CoreException e) { 2171 DebugPlugin.log(e.getStatus()); 2172 } 2173 visitor.reset(); 2174 } 2175 } 2176 2177 2185 private ArrayList collectAssociatedLaunches(IProject project) { 2186 ArrayList list = new ArrayList (); 2187 try { 2188 ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(); 2189 IResource[] resources = null; 2190 for(int i = 0; i < configs.length; i++) { 2191 if(configs[i].isLocal()) { 2192 resources = configs[i].getMappedResources(); 2193 if(resources != null) { 2194 for(int j = 0; j < resources.length; j++){ 2195 if(project.equals(resources[j].getProject())) { 2196 list.add(configs[i]); 2197 } 2198 } 2199 } 2200 } 2201 } 2202 } catch (CoreException e) { 2203 DebugPlugin.log(e); 2204 } 2205 return list; 2206 } 2207 2208 2217 protected void setMovedFromTo(ILaunchConfiguration from, ILaunchConfiguration to) { 2218 fFrom = from; 2219 fTo = to; 2220 } 2221 2225 public void shutdown() { 2226 fListeners = new ListenerList(); 2227 fLaunchesListeners = new ListenerList(); 2228 fLaunchConfigurationListeners = new ListenerList(); 2229 ILaunch[] launches = getLaunches(); 2230 ILaunch launch = null; 2231 for (int i= 0; i < launches.length; i++) { 2232 launch = launches[i]; 2233 try { 2234 if (launch instanceof IDisconnect) { 2235 IDisconnect disconnect = (IDisconnect)launch; 2236 if (disconnect.canDisconnect()) { 2237 disconnect.disconnect(); 2238 } 2239 } 2240 if (launch.canTerminate()) { 2241 launch.terminate(); 2242 } 2243 } catch (DebugException e) { 2244 DebugPlugin.log(e); 2245 } 2246 } 2247 2248 persistPreferredLaunchDelegates(); 2249 clearAllLaunchConfigurations(); 2250 fStepFilterManager = null; 2251 ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); 2252 } 2253 2254 2259 private void persistPreferredLaunchDelegates() { 2260 Preferences prefs = DebugPlugin.getDefault().getPluginPreferences(); 2261 try { 2262 Document doc = DebugPlugin.newDocument(); 2263 Element root = doc.createElement(IConfigurationElementConstants.PREFERRED_DELEGATES); 2264 doc.appendChild(root); 2265 ILaunchConfigurationType[] types = getLaunchConfigurationTypes(); 2266 Map preferred = null; 2267 Element child = null; 2268 ILaunchDelegate delegate = null; 2269 Set modes = null; 2270 String modestr = EMPTY_STRING; 2271 for(int i = 0; i < types.length; i++) { 2272 preferred = ((LaunchConfigurationType)types[i]).getPreferredDelegates(); 2273 if(preferred != null && preferred.size() > 0) { 2274 for(Iterator iter = preferred.keySet().iterator(); iter.hasNext();) { 2275 modes = (Set ) iter.next(); 2276 delegate = (ILaunchDelegate) preferred.get(modes); 2277 if(delegate != null) { 2278 child = doc.createElement(IConfigurationElementConstants.DELEGATE); 2279 child.setAttribute(IConfigurationElementConstants.ID, delegate.getId()); 2280 child.setAttribute(IConfigurationElementConstants.TYPE_ID, types[i].getIdentifier()); 2281 for(Iterator iter2 = modes.iterator(); iter2.hasNext();) { 2282 modestr += iter2.next(); 2283 if(iter2.hasNext()) { 2284 modestr += ","; } 2286 } 2287 child.setAttribute(IConfigurationElementConstants.MODES, modestr); 2288 modestr = EMPTY_STRING; 2289 root.appendChild(child); 2290 } 2291 } 2292 } 2293 } 2294 String pref = null; 2295 if(root.hasChildNodes()) { 2296 pref = serializeDocument(doc); 2297 } 2298 if(pref != null) { 2299 prefs.setValue(PREF_PREFERRED_DELEGATES, pref); 2300 } 2301 } 2302 catch (CoreException e) {DebugPlugin.log(e);} 2303 catch (IOException ioe) {DebugPlugin.log(ioe);} 2304 catch (TransformerException te) {DebugPlugin.log(te);} 2305 } 2306 2307 2312 protected void terminateMappedConfigurations(IResource resource) { 2313 ILaunch[] launches = getLaunches(); 2314 ILaunchConfiguration[] configs = getMappedConfigurations(resource); 2315 try { 2316 for(int i = 0; i < launches.length; i++) { 2317 for(int j = 0; j < configs.length; j++) { 2318 if(configs[j].equals(launches[i].getLaunchConfiguration()) & launches[i].canTerminate()) { 2319 launches[i].terminate(); 2320 } 2321 } 2322 } 2323 } 2324 catch(CoreException e) {DebugPlugin.log(e);} 2325 } 2326 2327 2331 private void throwException(ILaunchConfiguration config, Throwable e) throws DebugException { 2332 IPath path = config.getLocation(); 2333 throw createDebugException(MessageFormat.format(DebugCoreMessages.LaunchManager__0__occurred_while_reading_launch_configuration_file__1___1, new String []{e.toString(), path.toOSString()}), e); 2334 } 2335 2336 2344 protected void verifyConfigurations(List verify, List valid) { 2345 Iterator configs = verify.iterator(); 2346 ILaunchConfiguration config = null; 2347 while (configs.hasNext()) { 2348 config = (ILaunchConfiguration)configs.next(); 2349 if (isValid(config)) { 2350 valid.add(config); 2351 } 2352 } 2353 } 2354 2355 2362 public String getLaunchModeName(String id) { 2363 ILaunchMode launchMode = getLaunchMode(id); 2364 if (launchMode != null) { 2365 return removeAccelerators(launchMode.getLabel()); 2366 } 2367 return null; 2368 } 2369 2375 public static String removeAccelerators(String label) { 2376 String title = label; 2377 if (title != null) { 2378 int index = title.indexOf('&'); 2380 if (index == 0) { 2381 title = title.substring(1); 2382 } else if (index > 0) { 2383 if (title.charAt(index - 1) == '(' && title.length() >= index + 3 && title.charAt(index + 2) == ')') { 2385 String first = title.substring(0, index - 1); 2386 String last = title.substring(index + 3); 2387 title = first + last; 2388 } else if (index < (title.length() - 1)) { 2389 String first = title.substring(0, index); 2390 String last = title.substring(index + 1); 2391 title = first + last; 2392 } 2393 } 2394 } 2395 return title; 2396 } 2397 2398 2403 public synchronized StepFilterManager getStepFilterManager() { 2404 if (fStepFilterManager == null) { 2405 fStepFilterManager = new StepFilterManager(); 2406 } 2407 return fStepFilterManager; 2408 } 2409 2410} 2411 | Popular Tags |