A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse.externaltools / src / net / sourceforge / phpdt / externaltools / internal / program / launchConfigurations / ProgramLaunchDelegate.java
1 package net.sourceforge.phpdt.externaltools.internal.program.launchConfigurations;
2
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
8  �
9  Contributors:
10  **********************************************************************/
11
12 import java.io.File;
13
14 import net.sourceforge.phpdt.externaltools.launchConfigurations.ExternalToolsUtil;
15 import net.sourceforge.phpdt.externaltools.variable.ExpandVariableContext;
16
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;
25
26 /**
27  * Launch delegate for a program.
28  */
29 public class ProgramLaunchDelegate implements ILaunchConfigurationDelegate {
30
31         /**
32          * Constructor for ProgramLaunchDelegate.
33          */
34         public ProgramLaunchDelegate() {
35                 super();
36         }
37
38         /**
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)
42          */
43         public void launch(ILaunchConfiguration configuration, String mode,
44                         ILaunch launch, IProgressMonitor monitor) throws CoreException {
45
46                 if (monitor.isCanceled()) {
47                         return;
48                 }
49
50                 // get variable context
51                 ExpandVariableContext resourceContext = ExternalToolsUtil
52                                 .getVariableContext();
53
54                 if (monitor.isCanceled()) {
55                         return;
56                 }
57
58                 // resolve location
59                 IPath location = ExternalToolsUtil.getLocation(configuration,
60                                 resourceContext);
61
62                 if (monitor.isCanceled()) {
63                         return;
64                 }
65
66                 // resolve working directory
67                 IPath workingDirectory = ExternalToolsUtil.getWorkingDirectory(
68                                 configuration, resourceContext);
69
70                 if (monitor.isCanceled()) {
71                         return;
72                 }
73
74                 // resolve arguments
75                 String[] arguments = ExternalToolsUtil.getArguments(configuration,
76                                 resourceContext);
77
78                 if (monitor.isCanceled()) {
79                         return;
80                 }
81
82                 int cmdLineLength = 1;
83                 if (arguments != null) {
84                         cmdLineLength += arguments.length;
85                 }
86                 String[] cmdLine = new String[cmdLineLength];
87                 cmdLine[0] = location.toOSString();
88                 if (arguments != null) {
89                         System.arraycopy(arguments, 0, cmdLine, 1, arguments.length);
90                 }
91
92                 File workingDir = null;
93                 if (workingDirectory != null) {
94                         workingDir = workingDirectory.toFile();
95                 }
96
97                 if (monitor.isCanceled()) {
98                         return;
99                 }
100
101                 Process p = DebugPlugin.exec(cmdLine, workingDir);
102                 IProcess process = null;
103                 if (p != null) {
104                         process = DebugPlugin.newProcess(launch, p, location.toOSString());
105                 }
106                 process.setAttribute(IProcess.ATTR_CMDLINE, renderCommandLine(cmdLine));
107
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();
114                         }
115                 } else {
116                         // wait for process to exit
117                         while (!process.isTerminated()) {
118                                 try {
119                                         if (monitor.isCanceled()) {
120                                                 process.terminate();
121                                                 break;
122                                         }
123                                         Thread.sleep(50);
124                                 } catch (InterruptedException e) {
125                                 }
126                         }
127
128                         // refresh resources
129                         ExternalToolsUtil.refreshResources(configuration, resourceContext,
130                                         monitor);
131                 }
132
133         }
134
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++) {
140                         buf.append(' ');
141                         buf.append(commandLine[i]);
142                 }
143                 return buf.toString();
144         }
145
146 }