1 package net.sourceforge.phpdt.externaltools.internal.program.launchConfigurations;
3 /**********************************************************************
4 Copyright (c) 2002 IBM Corp. and others. All rights reserved.
5 This file is made available under the terms of the Common Public License v1.0
6 which accompanies this distribution, and is available at
7 http://www.eclipse.org/legal/cpl-v10.html
10 **********************************************************************/
14 import net.sourceforge.phpdt.externaltools.launchConfigurations.ExternalToolsUtil;
15 import net.sourceforge.phpdt.externaltools.variable.ExpandVariableContext;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.debug.core.DebugPlugin;
21 import org.eclipse.debug.core.ILaunch;
22 import org.eclipse.debug.core.ILaunchConfiguration;
23 import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
24 import org.eclipse.debug.core.model.IProcess;
27 * Launch delegate for a program.
29 public class ProgramLaunchDelegate implements ILaunchConfigurationDelegate {
32 * Constructor for ProgramLaunchDelegate.
34 public ProgramLaunchDelegate() {
39 * @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
41 public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
43 if (monitor.isCanceled()) {
47 // get variable context
48 ExpandVariableContext resourceContext = ExternalToolsUtil.getVariableContext();
50 if (monitor.isCanceled()) {
55 IPath location = ExternalToolsUtil.getLocation(configuration, resourceContext);
57 if (monitor.isCanceled()) {
61 // resolve working directory
62 IPath workingDirectory = ExternalToolsUtil.getWorkingDirectory(configuration, resourceContext);
64 if (monitor.isCanceled()) {
69 String[] arguments = ExternalToolsUtil.getArguments(configuration, resourceContext);
71 if (monitor.isCanceled()) {
75 int cmdLineLength = 1;
76 if (arguments != null) {
77 cmdLineLength += arguments.length;
79 String[] cmdLine = new String[cmdLineLength];
80 cmdLine[0] = location.toOSString();
81 if (arguments != null) {
82 System.arraycopy(arguments, 0, cmdLine, 1, arguments.length);
85 File workingDir = null;
86 if (workingDirectory != null) {
87 workingDir = workingDirectory.toFile();
90 if (monitor.isCanceled()) {
94 Process p = DebugPlugin.exec(cmdLine, workingDir);
95 IProcess process = null;
97 process = DebugPlugin.newProcess(launch, p, location.toOSString());
99 process.setAttribute(IProcess.ATTR_CMDLINE, renderCommandLine(cmdLine));
101 if (ExternalToolsUtil.isBackground(configuration)) {
102 // refresh resources after process finishes
103 if (ExternalToolsUtil.getRefreshScope(configuration) != null) {
104 BackgroundResourceRefresher refresher = new BackgroundResourceRefresher(configuration, process, resourceContext);
105 refresher.startBackgroundRefresh();
108 // wait for process to exit
109 while (!process.isTerminated()) {
111 if (monitor.isCanceled()) {
116 } catch (InterruptedException e) {
121 ExternalToolsUtil.refreshResources(configuration, resourceContext, monitor);
127 protected static String renderCommandLine(String[] commandLine) {
128 if (commandLine.length < 1)
129 return ""; //$NON-NLS-1$
130 StringBuffer buf= new StringBuffer(commandLine[0]);
131 for (int i= 1; i < commandLine.length; i++) {
133 buf.append(commandLine[i]);
135 return buf.toString();