1 12 package org.eclipse.ui.externaltools.internal.program.launchConfigurations; 13 14 15 import java.io.File ; 16 import java.util.HashMap ; 17 import java.util.Map ; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.core.runtime.Status; 24 import org.eclipse.debug.core.DebugPlugin; 25 import org.eclipse.debug.core.ILaunch; 26 import org.eclipse.debug.core.ILaunchConfiguration; 27 import org.eclipse.debug.core.ILaunchConfigurationType; 28 import org.eclipse.debug.core.ILaunchManager; 29 import org.eclipse.debug.core.model.IProcess; 30 import org.eclipse.debug.core.model.LaunchConfigurationDelegate; 31 import org.eclipse.debug.ui.CommonTab; 32 import org.eclipse.debug.ui.RefreshTab; 33 import org.eclipse.jface.dialogs.MessageDialog; 34 import org.eclipse.osgi.util.NLS; 35 import org.eclipse.ui.IWindowListener; 36 import org.eclipse.ui.IWorkbenchWindow; 37 import org.eclipse.ui.PlatformUI; 38 import org.eclipse.ui.externaltools.internal.launchConfigurations.ExternalToolsUtil; 39 import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants; 40 41 44 public class ProgramLaunchDelegate extends LaunchConfigurationDelegate { 45 46 private static IWindowListener fWindowListener; 47 48 52 private class ProgramLaunchWindowListener implements IWindowListener { 53 public void windowActivated(IWorkbenchWindow window) { 54 } 55 public void windowDeactivated(IWorkbenchWindow window) { 56 } 57 public void windowClosed(IWorkbenchWindow window) { 58 IWorkbenchWindow windows[]= PlatformUI.getWorkbench().getWorkbenchWindows(); 59 if (windows.length > 1) { 60 return; 62 } 63 ILaunchManager manager= DebugPlugin.getDefault().getLaunchManager(); 64 ILaunchConfigurationType programType= manager.getLaunchConfigurationType(IExternalToolConstants.ID_PROGRAM_LAUNCH_CONFIGURATION_TYPE); 65 if (programType == null) { 66 return; 67 } 68 ILaunch launches[]= manager.getLaunches(); 69 ILaunchConfigurationType configType; 70 ILaunchConfiguration config; 71 for (int i = 0; i < launches.length; i++) { 72 try { 73 config= launches[i].getLaunchConfiguration(); 74 if (config == null) { 75 continue; 76 } 77 configType= config.getType(); 78 } catch (CoreException e) { 79 continue; 80 } 81 if (configType.equals(programType)) { 82 if (!launches[i].isTerminated()) { 83 MessageDialog.openWarning(window.getShell(), ExternalToolsProgramMessages.ProgramLaunchDelegate_Workbench_Closing_1, ExternalToolsProgramMessages.ProgramLaunchDelegate_The_workbench_is_exiting); 84 break; 85 } 86 } 87 } 88 } 89 public void windowOpened(IWorkbenchWindow window) { 90 } 91 } 92 93 96 public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException { 97 98 if (monitor.isCanceled()) { 99 return; 100 } 101 102 IPath location = ExternalToolsUtil.getLocation(configuration); 104 105 if (monitor.isCanceled()) { 106 return; 107 } 108 109 IPath workingDirectory = ExternalToolsUtil.getWorkingDirectory(configuration); 111 112 if (monitor.isCanceled()) { 113 return; 114 } 115 116 String [] arguments = ExternalToolsUtil.getArguments(configuration); 118 119 if (monitor.isCanceled()) { 120 return; 121 } 122 123 int cmdLineLength = 1; 124 if (arguments != null) { 125 cmdLineLength += arguments.length; 126 } 127 String [] cmdLine = new String [cmdLineLength]; 128 cmdLine[0] = location.toOSString(); 129 if (arguments != null) { 130 System.arraycopy(arguments, 0, cmdLine, 1, arguments.length); 131 } 132 133 File workingDir = null; 134 if (workingDirectory != null) { 135 workingDir = workingDirectory.toFile(); 136 } 137 138 if (monitor.isCanceled()) { 139 return; 140 } 141 142 String [] envp = DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration); 143 144 if (monitor.isCanceled()) { 145 return; 146 } 147 148 if (fWindowListener == null) { 149 fWindowListener= new ProgramLaunchWindowListener(); 150 PlatformUI.getWorkbench().addWindowListener(fWindowListener); 151 } 152 Process p = DebugPlugin.exec(cmdLine, workingDir, envp); 153 IProcess process = null; 154 155 Map processAttributes = new HashMap (); 157 String programName = location.lastSegment(); 158 String extension = location.getFileExtension(); 159 if (extension != null) { 160 programName = programName.substring(0, programName.length() - (extension.length() + 1)); 161 } 162 programName = programName.toLowerCase(); 163 processAttributes.put(IProcess.ATTR_PROCESS_TYPE, programName); 164 165 if (p != null) { 166 monitor.beginTask(NLS.bind(ExternalToolsProgramMessages.ProgramLaunchDelegate_3, new String [] {configuration.getName()}), IProgressMonitor.UNKNOWN); 167 process = DebugPlugin.newProcess(launch, p, location.toOSString(), processAttributes); 168 if (process == null) { 169 p.destroy(); 170 throw new CoreException(new Status(IStatus.ERROR, IExternalToolConstants.PLUGIN_ID, IExternalToolConstants.ERR_INTERNAL_ERROR, ExternalToolsProgramMessages.ProgramLaunchDelegate_4, null)); 171 } 172 173 } 174 process.setAttribute(IProcess.ATTR_CMDLINE, generateCommandLine(cmdLine)); 175 176 if (CommonTab.isLaunchInBackground(configuration)) { 177 if (RefreshTab.getRefreshScope(configuration) != null) { 179 BackgroundResourceRefresher refresher = new BackgroundResourceRefresher(configuration, process); 180 refresher.startBackgroundRefresh(); 181 } 182 } else { 183 while (!process.isTerminated()) { 185 try { 186 if (monitor.isCanceled()) { 187 process.terminate(); 188 break; 189 } 190 Thread.sleep(50); 191 } catch (InterruptedException e) { 192 } 193 } 194 195 RefreshTab.refreshResources(configuration, monitor); 197 } 198 } 199 200 private String generateCommandLine(String [] commandLine) { 201 if (commandLine.length < 1) 202 return ""; StringBuffer buf= new StringBuffer (); 204 for (int i= 0; i < commandLine.length; i++) { 205 buf.append(' '); 206 char[] characters= commandLine[i].toCharArray(); 207 StringBuffer command= new StringBuffer (); 208 boolean containsSpace= false; 209 for (int j = 0; j < characters.length; j++) { 210 char character= characters[j]; 211 if (character == '\"') { 212 command.append('\\'); 213 } else if (character == ' ') { 214 containsSpace = true; 215 } 216 command.append(character); 217 } 218 if (containsSpace) { 219 buf.append('\"'); 220 buf.append(command); 221 buf.append('\"'); 222 } else { 223 buf.append(command); 224 } 225 } 226 return buf.toString(); 227 } 228 229 } 230 | Popular Tags |