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