484f75a907db79bd173efc99ec21b3e434844897
[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.List;
18
19 import net.sourceforge.phpeclipse.xdebug.core.IXDebugPreferenceConstants;
20 import net.sourceforge.phpeclipse.xdebug.core.XDebugCorePlugin;
21 import net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget;
22
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.core.resources.IProject;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.core.runtime.IStatus;
29 import org.eclipse.core.runtime.Status;
30 import org.eclipse.debug.core.DebugPlugin;
31 import org.eclipse.debug.core.ILaunch;
32 import org.eclipse.debug.core.ILaunchConfiguration;
33 import org.eclipse.debug.core.ILaunchManager;
34 import org.eclipse.debug.core.model.IDebugTarget;
35 import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
36 import org.eclipse.debug.core.model.IProcess;
37 import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
38
39
40 public class PHPLaunchConfigurationDelegate extends LaunchConfigurationDelegate {
41         
42         /**
43          * @see ILaunchConfigurationDelegate#launch(ILaunchConfiguration, String, ILaunch, IProgressMonitor)
44          */
45         public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
46                 List commandList = new ArrayList();
47                 
48                 String phpInterpreter = configuration.getAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER, (String)null);
49                 boolean useDefaultInterpreter= configuration.getAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, true);
50
51                 if (useDefaultInterpreter)              
52                         phpInterpreter=XDebugCorePlugin.getDefault().getPreferenceStore().getString(IXDebugPreferenceConstants.PHP_INTERPRETER_PREFERENCE);
53
54                 File exe = new File(phpInterpreter);
55                 if (!exe.exists()) {
56                         abort(MessageFormat.format("Specified PHP executable {0} does not exist. Check value of PHP-Interpreter.", new String[]{phpInterpreter}), null);
57                 }
58                 commandList.add(phpInterpreter);
59                 
60                 // program name
61                 String projectName = configuration.getAttribute(IXDebugConstants.ATTR_PHP_PROJECT, (String)null);
62                 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
63
64                 if (project == null) {
65                         abort("PHP-Script unspecified.", null);
66                 }
67                 String fileName = configuration.getAttribute(IXDebugConstants.ATTR_PHP_FILE, (String)null);
68
69 //              String program = project.getFile(fileName);
70                 IFile file = project.getFile(fileName);
71 //              IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(program));
72                 if (!file.exists()) {
73                         abort(MessageFormat.format("PHP-Script {0} does not exist.", new String[] {file.getFullPath().toString()}), null);
74                 }
75                 
76                 commandList.add(file.getLocation().toOSString());
77 //              Map nativeEnvVars = DebugPlugin.getDefault().getLaunchManager().getNativeEnvironment();
78                 // Environment variables
79                 String[] envp=DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration);
80                 //              Map envVars=configuration.getAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, nativeEnvVars);
81                 int debugPort=XDebugCorePlugin.getDefault().getPreferenceStore().getInt(IXDebugPreferenceConstants.DEBUGPORT_PREFERENCE);
82                 if (debugPort<1024)
83                         debugPort=IXDebugPreferenceConstants.DEFAULT_DEBUGPORT;
84 //              if (mode.equals(ILaunchManager.DEBUG_MODE)) {
85 //                      nativeEnvVars.put("XDEBUG_CONFIG", "idekey=xdebug_test remote_enable=1");
86 //              }
87                 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
88                         String[] env = new String[envp.length+1];
89                         for(int i=0;i<envp.length;i++)
90                                 env[i+1]=envp[i];
91                         env[0]="XDEBUG_CONFIG=idekey=xdebug_test remote_enable=1";
92                         envp=env;
93                 }
94                 
95                 
96                 String[] commandLine = (String[]) commandList.toArray(new String[commandList.size()]);
97                 Process process = DebugPlugin.exec(commandLine, null,envp);
98                 IProcess p = DebugPlugin.newProcess(launch, process, phpInterpreter);
99                 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
100                         IDebugTarget target = new XDebugTarget(launch, p, debugPort);
101                         launch.addDebugTarget(target);
102                 }
103         }
104         
105         /**
106          * Throws an exception with a new status containing the given
107          * message and optional exception.
108          * 
109          * @param message error message
110          * @param e underlying exception
111          * @throws CoreException
112          */
113         private void abort(String message, Throwable e) throws CoreException {
114                 // TODO: the plug-in code should be the example plug-in, not Perl debug model id
115                 throw new CoreException(new Status(IStatus.ERROR, IXDebugConstants.ID_PHP_DEBUG_MODEL, 0, message, e));
116         }
117
118 }