Fixed: Editor open a file in R/O (as external) when debugging a file under linked...
[phpeclipse.git] / net.sourceforge.phpeclipse.launching / src / net / sourceforge / phpdt / internal / launching / DebuggerRunner.java
1 /***********************************************************************************************************************************
2  * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This program and the accompanying materials are made
3  * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4  * http://www.eclipse.org/legal/cpl-v10.html
5  * 
6  * Contributors: IBM Corporation - Initial implementation Vicente Fernando - www.alfersoft.com.ar Christian Perkonig - remote Debug
7  **********************************************************************************************************************************/
8 package net.sourceforge.phpdt.internal.launching;
9
10 import java.util.Iterator;
11
12 import net.sourceforge.phpdt.internal.core.JavaProject;
13 import net.sourceforge.phpdt.internal.debug.core.PHPDBGProxy;
14 import net.sourceforge.phpdt.internal.debug.core.model.PHPDebugTarget;
15 import net.sourceforge.phpeclipse.ui.editor.BrowserUtil;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.debug.core.ILaunch;
20 import org.eclipse.debug.core.model.IProcess;
21 import org.eclipse.swt.widgets.Display;
22
23 // import net.sourceforge.phpeclipse.resourcesview.PHPProject;
24
25 public class DebuggerRunner extends InterpreterRunner {
26
27         public IProcess run(InterpreterRunnerConfiguration configuration,
28                         ILaunch launch) {
29                 String[] env;
30                 String name, value;
31                 PHPDBGProxy newPHPDBGProxy = new PHPDBGProxy(configuration
32                                 .useRemoteDebugger(), configuration.getRemoteSourcePath(),
33                                 configuration.usePathTranslation(), configuration.getPathMap());
34                 int pos;
35
36                 IProcess process = null;
37                 PHPDebugTarget debugTarget = new PHPDebugTarget(launch, process);
38                 newPHPDBGProxy.setDebugTarget(debugTarget);
39                 newPHPDBGProxy.start();
40                 if (configuration.useRemoteDebugger()) {
41                         // listener for remote debuger is started
42                         if (configuration.useDBGSessionInBrowser()) {
43                                 activateDBGSESSIDPreview(configuration, newPHPDBGProxy
44                                                 .getPort());
45                         }
46                 } else {
47                         setEnvironmentVariables(configuration, newPHPDBGProxy.getPort());
48                         // env=configuration.getEnvironment();
49                         process = super.run(configuration, launch);
50                         debugTarget.setProcess(process);
51                 }
52                 launch.addDebugTarget(debugTarget);
53
54                 return process;
55         }
56
57         /**
58          * Open the browser in the UI thread with the current debugger URL
59          * 
60          * @param configuration
61          * @param port
62          */
63         protected static void activateDBGSESSIDPreview(
64                         final InterpreterRunnerConfiguration configuration, final int port) {
65                 Display.getDefault().asyncExec(new Runnable() {
66                         public void run() {
67                                 String fileName = configuration.getFileName();
68                                 JavaProject jproject = configuration.getProject();
69                                 IProject project = jproject.getProject();
70                                 IFile file = project.getFile(fileName);
71                                 BrowserUtil.showPreview(file, true, "?DBGSESSID=1@clienthost:"
72                                                 + port);
73                         }
74                 });
75         }
76
77         protected void setEnvironmentVariables(
78                         InterpreterRunnerConfiguration configuration, int listenPort) {
79                 String DBGSessID;
80                 String env[] = new String[18];
81                 long id = Math.round(Math.random() * 100000);
82
83                 DBGSessID = "DBGSESSID=" + id + "@clienthost:" + listenPort;
84                 configuration.addEnvironmentValue("HTTP_COOKIE", DBGSessID, false);
85                 /*
86                  * configuration.addEnvironmentValue("REDIRECT_URL",OSFilePath,true);
87                  * configuration.addEnvironmentValue("REQUEST_URI",OSFilePath,true);
88                  * configuration.addEnvironmentValue("PATH_INFO",OSFilePath,true);
89                  * configuration.addEnvironmentValue("PATH_TRANSLATED",OSFilePath,true);
90                  * configuration.addEnvironmentValue("SCRIPT_FILENAME",interpreter,true);
91                  * configuration.addEnvironmentValue("SERVER_PROTOCOL","HTTP /
92                  * 1.1",true);
93                  */
94                 /*
95                  * env[0]= "HTTP_COOKIE=" + DBGSessID; env[1]= "REDIRECT_QUERY_STRING=";
96                  * env[2]= "REDIRECT_STATUS=200"; env[3]= "REDIRECT_URL=" + OSFilePath;
97                  * env[4]= "SERVER_SOFTWARE=DBG / 2.1"; env[5]= "SERVER_NAME=localhost";
98                  * env[6]= "SERVER_ADDR=127.0.0.1"; env[7]= "SERVER_PORT=80"; env[8]=
99                  * "REMOTE_ADDR=127.0.0.1"; env[9]= "SCRIPT_FILENAME=" + interpreter;
100                  * env[10]= "GATEWAY_INTERFACE=CGI / 1.1"; env[11]=
101                  * "SERVER_PROTOCOL=HTTP / 1.1"; env[12]= "REQUEST_METHOD=GET"; env[13]=
102                  * "QUERY_STRING=test=1"; env[14]= "REQUEST_URI=" + OSFilePath; env[15]=
103                  * "PATH_INFO=" + OSFilePath; env[16]= "PATH_TRANSLATED=" + OSFilePath;
104                  * env[17]= "SystemRoot=" + Environment.getenv("SystemRoot");
105                  */
106                 // return env;
107         }
108
109         protected String getDebugCommandLineArgument() {
110                 return "";
111         }
112
113         protected String renderLoadPath(InterpreterRunnerConfiguration configuration) {
114                 StringBuffer loadPath = new StringBuffer();
115
116                 JavaProject project = configuration.getProject();
117                 addToLoadPath(loadPath, project.getProject());
118
119                 Iterator referencedProjects = project.getReferencedProjects()
120                                 .iterator();
121                 while (referencedProjects.hasNext())
122                         addToLoadPath(loadPath, (IProject) referencedProjects.next());
123
124                 return loadPath.toString();
125         }
126 }