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