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