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
9 IBM Corporation - Initial implementation
10 Vicente Fernando - www.alfersoft.com.ar
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.xdebug.php.launching;
15 import java.text.MessageFormat;
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
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;
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;
42 public class PHPLaunchConfigurationDelegate extends LaunchConfigurationDelegate {
45 * @see ILaunchConfigurationDelegate#launch(ILaunchConfiguration, String, ILaunch, IProgressMonitor)
47 public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
48 List commandList = new ArrayList();
50 String phpInterpreter = configuration.getAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER, (String)null);
51 boolean useDefaultInterpreter= configuration.getAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, true);
53 if (useDefaultInterpreter)
54 phpInterpreter=XDebugCorePlugin.getDefault().getPreferenceStore().getString(IXDebugPreferenceConstants.PHP_INTERPRETER_PREFERENCE);
56 File exe = new File(phpInterpreter);
57 // Just to get sure that the interpreter exists
59 abort(MessageFormat.format("Specified PHP executable {0} does not exist. Check value of PHP-Interpreter.", new String[]{phpInterpreter}), null);
61 commandList.add(phpInterpreter);
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);
70 String fileName = configuration.getAttribute(IXDebugConstants.ATTR_PHP_FILE, (String)null);
72 IFile file = project.getFile(fileName);
73 // Just to get sure that the script exists
75 abort(MessageFormat.format("PHP-Script {0} does not exist.", new String[] {file.getFullPath().toString()}), null);
78 commandList.add(file.getLocation().toOSString());
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);
84 debugPort=XDebugCorePlugin.getDefault().getPreferenceStore().getInt(IXDebugPreferenceConstants.DEBUGPORT_PREFERENCE);
86 debugPort=IXDebugPreferenceConstants.DEFAULT_DEBUGPORT;
88 String[] envp=DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration);
89 // appends the environment to the native environment
91 Map stringVars = DebugPlugin.getDefault().getLaunchManager().getNativeEnvironment();
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;
100 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
101 String[] env = new String[envp.length+1];
102 for(int i=0;i<envp.length;i++)
104 env[0]="XDEBUG_CONFIG=idekey=xdebug_test remote_enable=1";
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);
118 * Throws an exception with a new status containing the given
119 * message and optional exception.
121 * @param message error message
122 * @param e underlying exception
123 * @throws CoreException
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));