Quick fix for the Environment Vars.
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / php / launching / PHPLaunchConfigurationDelegate.java
1 /**********************************************************************
2  Copyright (c) 2000, 2002 IBM Corp. and others.
3  All rights reserved. This program and the accompanying materials
4  are made available under the terms of the Common Public License v1.0
5  which accompanies this distribution, and is available at
6  http://www.eclipse.org/legal/cpl-v10.html
7  
8  Contributors:
9  IBM Corporation - Initial implementation
10  Vicente Fernando - www.alfersoft.com.ar
11  **********************************************************************/
12 package net.sourceforge.phpeclipse.xdebug.php.launching;
13
14 import java.io.File;
15 import java.text.MessageFormat;
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Map;
20
21 import net.sourceforge.phpeclipse.xdebug.core.IXDebugPreferenceConstants;
22 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
23 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget;
24
25 import org.eclipse.core.resources.IFile;
26 import org.eclipse.core.resources.IProject;
27 import org.eclipse.core.resources.ResourcesPlugin;
28 import org.eclipse.core.runtime.CoreException;
29 import org.eclipse.core.runtime.IProgressMonitor;
30 import org.eclipse.core.runtime.IStatus;
31 import org.eclipse.core.runtime.Status;
32 import org.eclipse.debug.core.DebugPlugin;
33 import org.eclipse.debug.core.ILaunch;
34 import org.eclipse.debug.core.ILaunchConfiguration;
35 import org.eclipse.debug.core.ILaunchManager;
36 import org.eclipse.debug.core.model.IDebugTarget;
37 import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
38 import org.eclipse.debug.core.model.IProcess;
39 import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
40
41
42 public class PHPLaunchConfigurationDelegate extends LaunchConfigurationDelegate {
43         
44         /**
45          * @see ILaunchConfigurationDelegate#launch(ILaunchConfiguration, String, ILaunch, IProgressMonitor)
46          */
47         public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
48                 List commandList = new ArrayList();
49                 
50                 String phpInterpreter = configuration.getAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER, (String)null);
51                 boolean useDefaultInterpreter= configuration.getAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, true);
52
53                 if (useDefaultInterpreter)              
54                         phpInterpreter=XDebugCorePlugin.getDefault().getPreferenceStore().getString(IXDebugPreferenceConstants.PHP_INTERPRETER_PREFERENCE);
55
56                 File exe = new File(phpInterpreter);
57                 // Just to get sure that the interpreter exists
58                 if (!exe.exists()) {
59                         abort(MessageFormat.format("Specified PHP executable {0} does not exist. Check value of PHP-Interpreter.", new String[]{phpInterpreter}), null);
60                 }
61                 commandList.add(phpInterpreter);
62                 
63                 // Project name
64                 String projectName = configuration.getAttribute(IXDebugConstants.ATTR_PHP_PROJECT, (String)null);
65                 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
66 //               Just to get sure that the project exists
67                 if (project == null) {
68                         abort("Project does not exist.", null);
69                 }
70                 String fileName = configuration.getAttribute(IXDebugConstants.ATTR_PHP_FILE, (String)null);
71
72                 IFile file = project.getFile(fileName);
73                 // Just to get sure that the script exists
74                 if (!file.exists()) {
75                         abort(MessageFormat.format("PHP-Script {0} does not exist.", new String[] {file.getFullPath().toString()}), null);
76                 }
77                 
78                 commandList.add(file.getLocation().toOSString());
79
80                 // Get de Debugport form the Launchconfiguration or from the preferences
81                 int debugPort  = configuration.getAttribute(IXDebugConstants.ATTR_PHP_DEBUGPORT,-1);
82                 boolean useDefaultPort= configuration.getAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_DEBUGPORT, true);
83                 if (useDefaultPort)
84                         debugPort=XDebugCorePlugin.getDefault().getPreferenceStore().getInt(IXDebugPreferenceConstants.DEBUGPORT_PREFERENCE);
85                 if (debugPort<1024)
86                         debugPort=IXDebugPreferenceConstants.DEFAULT_DEBUGPORT;
87
88                 String[] envp=DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration);
89                 // appends the environment to the native environment
90                 if (envp==null) {
91                         Map stringVars = DebugPlugin.getDefault().getLaunchManager().getNativeEnvironment();
92                         int idx=0;
93                         envp= new String[stringVars.size()];
94                         for (Iterator i = stringVars.keySet().iterator(); i.hasNext();) {
95                                 String key = (String) i.next();
96                                 String value = (String) stringVars.get(key);
97                                 envp[idx++]=key+"="+value;
98                         }
99                 }
100                 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
101                         String[] env = new String[envp.length+1];
102                         for(int i=0;i<envp.length;i++)
103                                         env[i+1]=envp[i];
104                         env[0]="XDEBUG_CONFIG=idekey=xdebug_test remote_enable=1";
105                         envp=env;
106                 }
107
108                 String[] commandLine = (String[]) commandList.toArray(new String[commandList.size()]);
109                 Process process = DebugPlugin.exec(commandLine, null,envp);
110                 IProcess p = DebugPlugin.newProcess(launch, process, phpInterpreter);
111                 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
112                         IDebugTarget target = new XDebugTarget(launch, p, debugPort);
113                         launch.addDebugTarget(target);
114                 }
115         }
116         
117         /**
118          * Throws an exception with a new status containing the given
119          * message and optional exception.
120          * 
121          * @param message error message
122          * @param e underlying exception
123          * @throws CoreException
124          */
125         private void abort(String message, Throwable e) throws CoreException {
126                 // TODO: the plug-in code should be the example plug-in, not Perl debug model id
127                 throw new CoreException(new Status(IStatus.ERROR, IXDebugConstants.ID_PHP_DEBUG_MODEL, 0, message, e));
128         }
129
130 }