Compatibility fragment commit
[phpeclipse.git] / net.sourceforge.phpeclipse.32.compatibility / 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.Iterator;
18 import java.util.List;
19 import java.util.Map;
20
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;
24
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;
40
41 public class PHPLaunchConfigurationDelegate extends LaunchConfigurationDelegate {
42
43         /**
44          * @see ILaunchConfigurationDelegate#launch(ILaunchConfiguration, String,
45          *      ILaunch, IProgressMonitor)
46          */
47         public void launch(ILaunchConfiguration configuration, String mode,
48                         ILaunch launch, IProgressMonitor monitor) throws CoreException {
49                 List commandList = new ArrayList();
50
51                 String phpInterpreter = configuration.getAttribute(
52                                 IXDebugConstants.ATTR_PHP_INTERPRETER, (String) null);
53                 boolean useDefaultInterpreter = configuration.getAttribute(
54                                 IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, true);
55
56                 if (useDefaultInterpreter)
57                         phpInterpreter = XDebugCorePlugin
58                                         .getDefault()
59                                         .getPreferenceStore()
60                                         .getString(
61                                                         IXDebugPreferenceConstants.PHP_INTERPRETER_PREFERENCE);
62
63                 File exe = new File(phpInterpreter);
64                 // Just to get sure that the interpreter exists
65                 if (!exe.exists()) {
66                         abort(
67                                         MessageFormat
68                                                         .format(
69                                                                         "Specified PHP executable {0} does not exist. Check value of PHP-Interpreter.",
70                                                                         new String[] { phpInterpreter }), null);
71                 }
72                 commandList.add(phpInterpreter);
73
74                 // Project name
75                 String projectName = configuration.getAttribute(
76                                 IXDebugConstants.ATTR_PHP_PROJECT, (String) null);
77                 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(
78                                 projectName);
79                 // Just to get sure that the project exists
80                 if (project == null) {
81                         abort("Project does not exist.", null);
82                 }
83                 String fileName = configuration.getAttribute(
84                                 IXDebugConstants.ATTR_PHP_FILE, (String) null);
85
86                 IFile file = project.getFile(fileName);
87                 // Just to get sure that the script exists
88                 if (!file.exists()) {
89                         abort(MessageFormat.format("PHP-Script {0} does not exist.",
90                                         new String[] { file.getFullPath().toString() }), null);
91                 }
92
93                 commandList.add(file.getLocation().toOSString());
94
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);
100                 if (useDefaultPort)
101                         debugPort = XDebugCorePlugin.getDefault().getPreferenceStore()
102                                         .getInt(IXDebugPreferenceConstants.DEBUGPORT_PREFERENCE);
103                 if (debugPort < 1024)
104                         debugPort = IXDebugPreferenceConstants.DEFAULT_DEBUGPORT;
105
106                 String[] envp = DebugPlugin.getDefault().getLaunchManager()
107                                 .getEnvironment(configuration);
108                 // appends the environment to the native environment
109                 if (envp == null) {
110                         Map stringVars = DebugPlugin.getDefault().getLaunchManager()
111                                         .getNativeEnvironment();
112                         int idx = 0;
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;
118                         }
119                 }
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";
125                         envp = env;
126                 }
127
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);
135                 }
136         }
137
138         /**
139          * Throws an exception with a new status containing the given message and
140          * optional exception.
141          * 
142          * @param message
143          *            error message
144          * @param e
145          *            underlying exception
146          * @throws CoreException
147          */
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
150                 // model id
151                 throw new CoreException(new Status(IStatus.ERROR,
152                                 IXDebugConstants.ID_PHP_DEBUG_MODEL, 0, message, e));
153         }
154
155 }