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;
41 public class PHPLaunchConfigurationDelegate extends LaunchConfigurationDelegate {
44 * @see ILaunchConfigurationDelegate#launch(ILaunchConfiguration, String,
45 * ILaunch, IProgressMonitor)
47 public void launch(ILaunchConfiguration configuration, String mode,
48 ILaunch launch, IProgressMonitor monitor) throws CoreException {
49 List commandList = new ArrayList();
51 String phpInterpreter = configuration.getAttribute(
52 IXDebugConstants.ATTR_PHP_INTERPRETER, (String) null);
53 boolean useDefaultInterpreter = configuration.getAttribute(
54 IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, true);
56 if (useDefaultInterpreter)
57 phpInterpreter = XDebugCorePlugin
61 IXDebugPreferenceConstants.PHP_INTERPRETER_PREFERENCE);
63 File exe = new File(phpInterpreter);
64 // Just to get sure that the interpreter exists
69 "Specified PHP executable {0} does not exist. Check value of PHP-Interpreter.",
70 new String[] { phpInterpreter }), null);
72 commandList.add(phpInterpreter);
75 String projectName = configuration.getAttribute(
76 IXDebugConstants.ATTR_PHP_PROJECT, (String) null);
77 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(
79 // Just to get sure that the project exists
80 if (project == null) {
81 abort("Project does not exist.", null);
83 String fileName = configuration.getAttribute(
84 IXDebugConstants.ATTR_PHP_FILE, (String) null);
86 IFile file = project.getFile(fileName);
87 // Just to get sure that the script exists
89 abort(MessageFormat.format("PHP-Script {0} does not exist.",
90 new String[] { file.getFullPath().toString() }), null);
93 commandList.add(file.getLocation().toOSString());
95 // Get de Debugport form the Launchconfiguration or from the preferences
96 int debugPort = configuration.getAttribute(
97 IXDebugConstants.ATTR_PHP_DEBUGPORT, -1);
98 boolean useDefaultPort = configuration.getAttribute(
99 IXDebugConstants.ATTR_PHP_DEFAULT_DEBUGPORT, true);
101 debugPort = XDebugCorePlugin.getDefault().getPreferenceStore()
102 .getInt(IXDebugPreferenceConstants.DEBUGPORT_PREFERENCE);
103 if (debugPort < 1024)
104 debugPort = IXDebugPreferenceConstants.DEFAULT_DEBUGPORT;
106 String[] envp = DebugPlugin.getDefault().getLaunchManager()
107 .getEnvironment(configuration);
108 // appends the environment to the native environment
110 Map stringVars = DebugPlugin.getDefault().getLaunchManager()
111 .getNativeEnvironment();
113 envp = new String[stringVars.size()];
114 for (Iterator i = stringVars.keySet().iterator(); i.hasNext();) {
115 String key = (String) i.next();
116 String value = (String) stringVars.get(key);
117 envp[idx++] = key + "=" + value;
120 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
121 String[] env = new String[envp.length + 1];
122 for (int i = 0; i < envp.length; i++)
123 env[i + 1] = envp[i];
124 env[0] = "XDEBUG_CONFIG=idekey=xdebug_test remote_enable=1";
128 String[] commandLine = (String[]) commandList
129 .toArray(new String[commandList.size()]);
130 Process process = DebugPlugin.exec(commandLine, null, envp);
131 IProcess p = DebugPlugin.newProcess(launch, process, phpInterpreter);
132 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
133 IDebugTarget target = new XDebugTarget(launch, p, debugPort);
134 launch.addDebugTarget(target);
139 * Throws an exception with a new status containing the given message and
140 * optional exception.
145 * underlying exception
146 * @throws CoreException
148 private void abort(String message, Throwable e) throws CoreException {
149 // TODO: the plug-in code should be the example plug-in, not Perl debug
151 throw new CoreException(new Status(IStatus.ERROR,
152 IXDebugConstants.ID_PHP_DEBUG_MODEL, 0, message, e));