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.externaltools.ExternalToolsPlugin;
22 import net.sourceforge.phpeclipse.xdebug.core.IXDebugPreferenceConstants;
23 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
24 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget;
26 import org.eclipse.core.resources.IFile;
27 import org.eclipse.core.resources.IProject;
28 import org.eclipse.core.resources.ResourcesPlugin;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.core.runtime.IProgressMonitor;
31 import org.eclipse.core.runtime.IStatus;
32 import org.eclipse.core.runtime.Status;
33 import org.eclipse.debug.core.DebugPlugin;
34 import org.eclipse.debug.core.ILaunch;
35 import org.eclipse.debug.core.ILaunchConfiguration;
36 import org.eclipse.debug.core.ILaunchManager;
37 import org.eclipse.debug.core.model.IDebugTarget;
38 import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
39 import org.eclipse.debug.core.model.IProcess;
40 import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
43 public class PHPLaunchConfigurationDelegate extends LaunchConfigurationDelegate implements ILaunchConfigurationDelegate{
46 * @see ILaunchConfigurationDelegate#launch(ILaunchConfiguration, String, ILaunch, IProgressMonitor)
48 public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
49 List commandList = new ArrayList();
51 String phpInterpreter = configuration.getAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER, (String)null);
52 boolean useDefaultInterpreter= configuration.getAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, true);
54 if (useDefaultInterpreter) {
55 phpInterpreter=XDebugCorePlugin.getDefault().getPreferenceStore().getString(IXDebugPreferenceConstants.PHP_INTERPRETER_PREFERENCE);
56 if (phpInterpreter=="") {
57 phpInterpreter=ExternalToolsPlugin.getDefault().getPreferenceStore().getString(ExternalToolsPlugin.PHP_RUN_PREF);
60 File exe = new File(phpInterpreter);
61 // Just to get sure that the interpreter exists
63 abort(MessageFormat.format("Specified PHP executable {0} does not exist. Check value of PHP-Interpreter.", new String[]{phpInterpreter}), null);
65 commandList.add(phpInterpreter);
68 String projectName = configuration.getAttribute(IXDebugConstants.ATTR_PHP_PROJECT, (String)null);
69 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
70 // Just to get sure that the project exists
71 if (project == null) {
72 abort("Project does not exist.", null);
74 String fileName = configuration.getAttribute(IXDebugConstants.ATTR_PHP_FILE, (String)null);
76 IFile file = project.getFile(fileName);
77 // Just to get sure that the script exists
79 abort(MessageFormat.format("PHP-Script {0} does not exist.", new String[] {file.getFullPath().toString()}), null);
82 commandList.add(file.getFullPath().toOSString());
84 // Get the Debugport from the preferences
85 int debugPort=XDebugCorePlugin.getDefault().getPreferenceStore().getInt(IXDebugPreferenceConstants.DEBUGPORT_PREFERENCE);
87 // check for default port
89 debugPort=IXDebugPreferenceConstants.DEFAULT_DEBUGPORT;
91 String[] envp=DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration);
92 // appends the environment to the native environment
94 Map stringVars = DebugPlugin.getDefault().getLaunchManager().getNativeEnvironment();
96 envp= new String[stringVars.size()];
97 for (Iterator i = stringVars.keySet().iterator(); i.hasNext();) {
98 String key = (String) i.next();
99 String value = (String) stringVars.get(key);
100 envp[idx++]=key+"="+value;
103 String idekey=fileName+"-"+(int)(Math.random()*100000);
104 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
105 String[] env = new String[envp.length+1];
106 for(int i=0;i<envp.length;i++)
108 env[0]="XDEBUG_CONFIG=idekey="+idekey+" remote_enable=1 remote_port="+debugPort;
111 System.out.println("ideKey= "+idekey);
113 String[] commandLine = (String[]) commandList.toArray(new String[commandList.size()]);
115 XDebugTarget target=null;
116 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
117 target = new XDebugTarget(launch, null, idekey);
119 Process process = DebugPlugin.exec(commandLine, null,envp);
120 IProcess p = DebugPlugin.newProcess(launch, process, phpInterpreter);
122 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
123 target.addProcess(p);
124 launch.addDebugTarget(target);
130 * Throws an exception with a new status containing the given
131 * message and optional exception.
133 * @param message error message
134 * @param e underlying exception
135 * @throws CoreException
137 private void abort(String message, Throwable e) throws CoreException {
138 // TODO: the plug-in code should be the example plug-in, not Perl debug model id
139 throw new CoreException(new Status(IStatus.ERROR, IXDebugConstants.ID_PHP_DEBUG_MODEL, 0, message, e));