Changing the version to 1.2.4.qualifier.
[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.useRemoteDebugger(),
32                                                                       configuration.getRemoteSourcePath(),
33                                                                       configuration.usePathTranslation(),
34                                                                       configuration.getPathMap(),
35                                                                       configuration.useRelaunchOnScriptTermination());
36                 //int pos;
37
38                 IProcess process = null;
39                 PHPDebugTarget debugTarget = new PHPDebugTarget(launch, process);
40                 newPHPDBGProxy.setDebugTarget(debugTarget);
41                 newPHPDBGProxy.start();
42                 if (configuration.useRemoteDebugger()) {
43                         // listener for remote debuger is started
44                         if (configuration.useDBGSessionInBrowser()) {
45                                 activateDBGSESSIDPreview(configuration, newPHPDBGProxy
46                                                 .getPort());
47                         }
48                 } else {
49                         setEnvironmentVariables(configuration, newPHPDBGProxy.getPort());
50                         // env=configuration.getEnvironment();
51                         process = super.run(configuration, launch);
52                         debugTarget.setProcess(process);
53                 }
54                 launch.addDebugTarget(debugTarget);
55
56                 return process;
57         }
58
59         /**
60          * Open the browser in the UI thread with the current debugger URL
61          * 
62          * @param configuration
63          * @param port
64          */
65         protected static void activateDBGSESSIDPreview(
66                         final InterpreterRunnerConfiguration configuration, final int port) {
67                 Display.getDefault().asyncExec(new Runnable() {
68                         public void run() {
69                                 String fileName = configuration.getFileName();
70                                 JavaProject jproject = configuration.getProject();
71                                 IProject project = jproject.getProject();
72                                 IFile file = project.getFile(fileName);
73                                 if (configuration.useDBGSessionInExternalBrowser()) {
74                                         BrowserUtil.showBrowserAsExternal(file,
75                                                         "?DBGSESSID=1@clienthost:" + port);
76                                 } else {
77                                         BrowserUtil.showPreview(file, true,
78                                                         "?DBGSESSID=1@clienthost:" + port);
79                                 }
80                         }
81                 });
82         }
83
84         protected void setEnvironmentVariables(
85                         InterpreterRunnerConfiguration configuration, int listenPort) {
86                 String DBGSessID;
87                 //String env[] = new String[18];
88                 long id = Math.round(Math.random() * 100000);
89
90                 DBGSessID = "DBGSESSID=" + id + "@clienthost:" + listenPort;
91                 configuration.addEnvironmentValue("HTTP_COOKIE", DBGSessID, false);
92                 /*
93                  * configuration.addEnvironmentValue("REDIRECT_URL",OSFilePath,true);
94                  * configuration.addEnvironmentValue("REQUEST_URI",OSFilePath,true);
95                  * configuration.addEnvironmentValue("PATH_INFO",OSFilePath,true);
96                  * configuration.addEnvironmentValue("PATH_TRANSLATED",OSFilePath,true);
97                  * configuration.addEnvironmentValue("SCRIPT_FILENAME",interpreter,true);
98                  * configuration.addEnvironmentValue("SERVER_PROTOCOL","HTTP /
99                  * 1.1",true);
100                  */
101                 /*
102                  * env[0]= "HTTP_COOKIE=" + DBGSessID; env[1]= "REDIRECT_QUERY_STRING=";
103                  * env[2]= "REDIRECT_STATUS=200"; env[3]= "REDIRECT_URL=" + OSFilePath;
104                  * env[4]= "SERVER_SOFTWARE=DBG / 2.1"; env[5]= "SERVER_NAME=localhost";
105                  * env[6]= "SERVER_ADDR=127.0.0.1"; env[7]= "SERVER_PORT=80"; env[8]=
106                  * "REMOTE_ADDR=127.0.0.1"; env[9]= "SCRIPT_FILENAME=" + interpreter;
107                  * env[10]= "GATEWAY_INTERFACE=CGI / 1.1"; env[11]=
108                  * "SERVER_PROTOCOL=HTTP / 1.1"; env[12]= "REQUEST_METHOD=GET"; env[13]=
109                  * "QUERY_STRING=test=1"; env[14]= "REQUEST_URI=" + OSFilePath; env[15]=
110                  * "PATH_INFO=" + OSFilePath; env[16]= "PATH_TRANSLATED=" + OSFilePath;
111                  * env[17]= "SystemRoot=" + Environment.getenv("SystemRoot");
112                  */
113                 // return env;
114         }
115
116         protected String getDebugCommandLineArgument() {
117                 return "";
118         }
119
120         protected String renderLoadPath(InterpreterRunnerConfiguration configuration) {
121                 StringBuffer loadPath = new StringBuffer();
122
123                 JavaProject project = configuration.getProject();
124                 addToLoadPath(loadPath, project.getProject());
125
126                 Iterator referencedProjects = project.getReferencedProjects()
127                                 .iterator();
128                 while (referencedProjects.hasNext())
129                         addToLoadPath(loadPath, (IProject) referencedProjects.next());
130
131                 return loadPath.toString();
132         }
133 }