Avoid NPE when deleting a project
[phpeclipse.git] / net.sourceforge.phpeclipse.launching / src / net / sourceforge / phpdt / internal / launching / InterpreterRunner.java
1 package net.sourceforge.phpdt.internal.launching;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Iterator;
6 import java.util.Map;
7
8 import net.sourceforge.phpdt.internal.core.JavaProject;
9
10 import org.eclipse.core.resources.IProject;
11 import org.eclipse.core.runtime.IPath;
12 import org.eclipse.core.runtime.Path;
13 import org.eclipse.core.runtime.Platform;
14 import org.eclipse.debug.core.DebugPlugin;
15 import org.eclipse.debug.core.ILaunch;
16 import org.eclipse.debug.core.model.IProcess;
17
18 public class InterpreterRunner {
19
20         public InterpreterRunner() {
21         }
22
23         public IProcess run(InterpreterRunnerConfiguration configuration, ILaunch launch) {             
24                 String commandLine = renderCommandLine(configuration);          
25                 File workingDirectory = configuration.getAbsoluteWorkingDirectory();
26                 
27                 setEnvironmentVariables(configuration);
28                 String[] env = configuration.getEnvironment();
29                 Process nativePHPProcess = null;
30                 try {
31                         nativePHPProcess = configuration.getInterpreter().exec(commandLine, workingDirectory, env);
32                 } catch (IOException e) {
33                         throw new RuntimeException("Unable to execute interpreter: " + commandLine + workingDirectory);
34                 }
35
36                 IProcess process = DebugPlugin.newProcess(launch, nativePHPProcess, renderLabel(configuration));
37                 process.setAttribute(PHPLaunchingPlugin.PLUGIN_ID + ".launcher.cmdline", commandLine);
38
39                 return process ;
40         }
41
42         protected String renderLabel(InterpreterRunnerConfiguration configuration) {
43                 StringBuffer buffer = new StringBuffer();
44
45                 PHPInterpreter interpreter = configuration.getInterpreter();
46                 buffer.append("PHP ");
47                 buffer.append(interpreter.getCommand());
48                 buffer.append(" : ");
49                 buffer.append(configuration.getFileName());
50
51                 return buffer.toString();
52         }
53
54         protected String renderCommandLine(InterpreterRunnerConfiguration configuration) {
55                 PHPInterpreter interpreter = configuration.getInterpreter();
56
57                 StringBuffer buffer = new StringBuffer();
58                 buffer.append(this.getDebugCommandLineArgument());
59         //      buffer.append(renderLoadPath(configuration));
60                 buffer.append(" " + configuration.getInterpreterArguments());
61         //      buffer.append(interpreter.endOfOptionsDelimeter);
62                 buffer.append(" " + osDependentPath(configuration.getAbsoluteFileName()));
63                 buffer.append(" " + configuration.getProgramArguments());
64
65                 return buffer.toString();
66         }
67         
68         protected void setEnvironmentVariables(InterpreterRunnerConfiguration configuration) {
69                 IPath FilePath= new Path(configuration.getAbsoluteFileName());
70                 String OSFilePath= FilePath.toOSString();
71                 configuration.addEnvironmentValue("REDIRECT_URL",OSFilePath,true);
72                 configuration.addEnvironmentValue("REQUEST_URI",OSFilePath,true);
73                 configuration.addEnvironmentValue("PATH_INFO",OSFilePath,true);
74                 configuration.addEnvironmentValue("PATH_TRANSLATED",OSFilePath,true);
75                 configuration.addEnvironmentValue("SCRIPT_FILENAME",configuration.getInterpreter().getCommand(),true);
76                 configuration.addEnvironmentValue("SERVER_PROTOCOL","HTTP / 1.1",true);
77
78                 configuration.addEnvironmentValue("REDIRECT_QUERY_STRING","",true);
79                 configuration.addEnvironmentValue("REDIRECT_STATUS","200",true);
80                 configuration.addEnvironmentValue("SERVER_SOFTWARE","DBG / 2.1",true);
81                 configuration.addEnvironmentValue("SERVER_NAME","localhost",true);
82                 configuration.addEnvironmentValue("SERVER_ADDR","127.0.0.1",true);
83                 configuration.addEnvironmentValue("SERVER_PORT","80",true);
84                 configuration.addEnvironmentValue("REMOTE_ADDR","127.0.0.1",true);
85
86                 configuration.addEnvironmentValue("GATEWAY_INTERFACE","CGI / 1.1",true);
87                 configuration.addEnvironmentValue("REQUEST_METHOD","GET",true);
88                 
89                 Map stringVars = DebugPlugin.getDefault().getLaunchManager().getNativeEnvironment();
90                 if (stringVars.containsKey("SYSTEMROOT"))
91                         configuration.addEnvironmentValue("SYSTEMROOT",(String) stringVars.get("SYSTEMROOT"),true);
92                 
93         }
94
95         protected String renderLoadPath(InterpreterRunnerConfiguration configuration) {
96                 StringBuffer loadPath = new StringBuffer();
97
98                 JavaProject project = configuration.getProject();
99                 addToLoadPath(loadPath, project.getProject());
100
101                 Iterator referencedProjects = project.getReferencedProjects().iterator();
102                 while (referencedProjects.hasNext())
103                         addToLoadPath(loadPath, (IProject) referencedProjects.next());
104
105                 return loadPath.toString();
106         }
107
108         protected void addToLoadPath(StringBuffer loadPath, IProject project) {
109                 loadPath.append(" -I " + osDependentPath(project.getLocation().toOSString()));
110         }
111
112         protected String osDependentPath(String aPath) {
113                 if (Platform.getOS().equals(Platform.OS_WIN32))
114                         aPath = "\"" + aPath + "\"";
115
116                 return aPath;
117         }
118         
119         protected String getDebugCommandLineArgument() {
120                 return "" ;     
121         }       
122 }