Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / 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, java.lang.String, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
40          */
41         public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
42                 
43                 if (monitor.isCanceled()) {
44                         return;
45                 }
46                 
47                 // get variable context
48                 ExpandVariableContext resourceContext = ExternalToolsUtil.getVariableContext();
49
50                 if (monitor.isCanceled()) {
51                         return;
52                 }
53                 
54                 // resolve location
55                 IPath location = ExternalToolsUtil.getLocation(configuration, resourceContext);
56                 
57                 if (monitor.isCanceled()) {
58                         return;
59                 }               
60                 
61                 // resolve working directory
62                 IPath workingDirectory = ExternalToolsUtil.getWorkingDirectory(configuration, resourceContext);
63                 
64                 if (monitor.isCanceled()) {
65                         return;
66                 }
67                 
68                 // resolve arguments
69                 String[] arguments = ExternalToolsUtil.getArguments(configuration, resourceContext);
70                 
71                 if (monitor.isCanceled()) {
72                         return;
73                 }
74                 
75                 int cmdLineLength = 1;
76                 if (arguments != null) {
77                         cmdLineLength += arguments.length;
78                 }
79                 String[] cmdLine = new String[cmdLineLength];
80                 cmdLine[0] = location.toOSString();
81                 if (arguments != null) {
82                         System.arraycopy(arguments, 0, cmdLine, 1, arguments.length);
83                 }
84                 
85                 File workingDir = null;
86                 if (workingDirectory != null) {
87                         workingDir = workingDirectory.toFile();
88                 }
89                 
90                 if (monitor.isCanceled()) {
91                         return;
92                 }
93                                 
94                 Process p = DebugPlugin.exec(cmdLine, workingDir);
95                 IProcess process = null;
96                 if (p != null) {
97                         process = DebugPlugin.newProcess(launch, p, location.toOSString());
98                 }
99                 process.setAttribute(IProcess.ATTR_CMDLINE, renderCommandLine(cmdLine));
100                 
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();
106                         }                               
107                 } else {
108                         // wait for process to exit
109                         while (!process.isTerminated()) {
110                                 try {
111                                         if (monitor.isCanceled()) {
112                                                 process.terminate();
113                                                 break;
114                                         }
115                                         Thread.sleep(50);
116                                 } catch (InterruptedException e) {
117                                 }
118                         }
119                         
120                         // refresh resources
121                         ExternalToolsUtil.refreshResources(configuration, resourceContext, monitor);
122                 }
123                 
124         
125         }
126         
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++) {
132                         buf.append(' ');
133                         buf.append(commandLine[i]);
134                 }       
135                 return buf.toString();
136         }       
137         
138 }