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