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 org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.debug.core.DebugPlugin;
18 import org.eclipse.debug.core.ILaunch;
19 import org.eclipse.debug.core.ILaunchConfiguration;
20 import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
21 import org.eclipse.debug.core.model.IProcess;
22 import net.sourceforge.phpdt.externaltools.launchConfigurations.ExternalToolsUtil;
23 import net.sourceforge.phpdt.externaltools.variable.ExpandVariableContext;
26 * Launch delegate for a program.
28 public class ProgramLaunchDelegate implements ILaunchConfigurationDelegate {
31 * Constructor for ProgramLaunchDelegate.
33 public ProgramLaunchDelegate() {
38 * @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)
40 public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
42 if (monitor.isCanceled()) {
46 // get variable context
47 ExpandVariableContext resourceContext = ExternalToolsUtil.getVariableContext();
49 if (monitor.isCanceled()) {
54 IPath location = ExternalToolsUtil.getLocation(configuration, resourceContext);
56 if (monitor.isCanceled()) {
60 // resolve working directory
61 IPath workingDirectory = ExternalToolsUtil.getWorkingDirectory(configuration, resourceContext);
63 if (monitor.isCanceled()) {
68 String[] arguments = ExternalToolsUtil.getArguments(configuration, resourceContext);
70 if (monitor.isCanceled()) {
74 int cmdLineLength = 1;
75 if (arguments != null) {
76 cmdLineLength += arguments.length;
78 String[] cmdLine = new String[cmdLineLength];
79 cmdLine[0] = location.toOSString();
80 if (arguments != null) {
81 System.arraycopy(arguments, 0, cmdLine, 1, arguments.length);
84 File workingDir = null;
85 if (workingDirectory != null) {
86 workingDir = workingDirectory.toFile();
89 if (monitor.isCanceled()) {
93 Process p = DebugPlugin.exec(cmdLine, workingDir);
94 IProcess process = null;
96 process = DebugPlugin.newProcess(launch, p, location.toOSString());
98 process.setAttribute(IProcess.ATTR_CMDLINE, renderCommandLine(cmdLine));
100 if (ExternalToolsUtil.isBackground(configuration)) {
101 // refresh resources after process finishes
102 if (ExternalToolsUtil.getRefreshScope(configuration) != null) {
103 BackgroundResourceRefresher refresher = new BackgroundResourceRefresher(configuration, process, resourceContext);
104 refresher.startBackgroundRefresh();
107 // wait for process to exit
108 while (!process.isTerminated()) {
110 if (monitor.isCanceled()) {
115 } catch (InterruptedException e) {
120 ExternalToolsUtil.refreshResources(configuration, resourceContext, monitor);
126 protected static String renderCommandLine(String[] commandLine) {
127 if (commandLine.length < 1)
128 return ""; //$NON-NLS-1$
129 StringBuffer buf= new StringBuffer(commandLine[0]);
130 for (int i= 1; i < commandLine.length; i++) {
132 buf.append(commandLine[i]);
134 return buf.toString();