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,
40 * java.lang.String, org.eclipse.debug.core.ILaunch,
41 * org.eclipse.core.runtime.IProgressMonitor)
43 public void launch(ILaunchConfiguration configuration, String mode,
44 ILaunch launch, IProgressMonitor monitor) throws CoreException {
46 if (monitor.isCanceled()) {
50 // get variable context
51 ExpandVariableContext resourceContext = ExternalToolsUtil
52 .getVariableContext();
54 if (monitor.isCanceled()) {
59 IPath location = ExternalToolsUtil.getLocation(configuration,
62 if (monitor.isCanceled()) {
66 // resolve working directory
67 IPath workingDirectory = ExternalToolsUtil.getWorkingDirectory(
68 configuration, resourceContext);
70 if (monitor.isCanceled()) {
75 String[] arguments = ExternalToolsUtil.getArguments(configuration,
78 if (monitor.isCanceled()) {
82 int cmdLineLength = 1;
83 if (arguments != null) {
84 cmdLineLength += arguments.length;
86 String[] cmdLine = new String[cmdLineLength];
87 cmdLine[0] = location.toOSString();
88 if (arguments != null) {
89 System.arraycopy(arguments, 0, cmdLine, 1, arguments.length);
92 File workingDir = null;
93 if (workingDirectory != null) {
94 workingDir = workingDirectory.toFile();
97 if (monitor.isCanceled()) {
101 Process p = DebugPlugin.exec(cmdLine, workingDir);
102 IProcess process = null;
104 process = DebugPlugin.newProcess(launch, p, location.toOSString());
106 process.setAttribute(IProcess.ATTR_CMDLINE, renderCommandLine(cmdLine));
108 if (ExternalToolsUtil.isBackground(configuration)) {
109 // refresh resources after process finishes
110 if (ExternalToolsUtil.getRefreshScope(configuration) != null) {
111 BackgroundResourceRefresher refresher = new BackgroundResourceRefresher(
112 configuration, process, resourceContext);
113 refresher.startBackgroundRefresh();
116 // wait for process to exit
117 while (!process.isTerminated()) {
119 if (monitor.isCanceled()) {
124 } catch (InterruptedException e) {
129 ExternalToolsUtil.refreshResources(configuration, resourceContext,
135 protected static String renderCommandLine(String[] commandLine) {
136 if (commandLine.length < 1)
137 return ""; //$NON-NLS-1$
138 StringBuffer buf = new StringBuffer(commandLine[0]);
139 for (int i = 1; i < commandLine.length; i++) {
141 buf.append(commandLine[i]);
143 return buf.toString();