be1286b6c6c6886f583e67138674f98c97347f9f
[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                 // Just to get sure that the interpreter exists
56                 if (!exe.exists()) {
57                         abort(MessageFormat.format("Specified PHP executable {0} does not exist. Check value of PHP-Interpreter.", new String[]{phpInterpreter}), null);
58                 }
59                 commandList.add(phpInterpreter);
60                 
61                 // Project name
62                 String projectName = configuration.getAttribute(IXDebugConstants.ATTR_PHP_PROJECT, (String)null);
63                 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
64 //               Just to get sure that the project exists
65                 if (project == null) {
66                         abort("Project does not exist.", null);
67                 }
68                 String fileName = configuration.getAttribute(IXDebugConstants.ATTR_PHP_FILE, (String)null);
69
70                 IFile file = project.getFile(fileName);
71                 // Just to get sure that the script exists
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                 String[] envp=DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration);
78
79                 // Get de Debugport form the Launchconfiguration or from the preferences
80                 int debugPort  = configuration.getAttribute(IXDebugConstants.ATTR_PHP_DEBUGPORT,-1);
81                 boolean useDefaultPort= configuration.getAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_DEBUGPORT, true);
82                 if (useDefaultPort)
83                         debugPort=XDebugCorePlugin.getDefault().getPreferenceStore().getInt(IXDebugPreferenceConstants.DEBUGPORT_PREFERENCE);
84                 if (debugPort<1024)
85                         debugPort=IXDebugPreferenceConstants.DEFAULT_DEBUGPORT;
86                 
87                 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
88                         String[] env;
89                         if (envp!=null) {
90                                 env = new String[envp.length+1];
91                                 for(int i=0;i<envp.length;i++)
92                                         env[i+1]=envp[i];
93                         } else
94                                 env = new String[1];
95                         env[0]="XDEBUG_CONFIG=idekey=xdebug_test remote_enable=1";
96                         envp=env;
97                 }
98                 
99                 
100                 String[] commandLine = (String[]) commandList.toArray(new String[commandList.size()]);
101                 Process process = DebugPlugin.exec(commandLine, null,envp);
102                 IProcess p = DebugPlugin.newProcess(launch, process, phpInterpreter);
103                 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
104                         IDebugTarget target = new XDebugTarget(launch, p, debugPort);
105                         launch.addDebugTarget(target);
106                 }
107         }
108         
109         /**
110          * Throws an exception with a new status containing the given
111          * message and optional exception.
112          * 
113          * @param message error message
114          * @param e underlying exception
115          * @throws CoreException
116          */
117         private void abort(String message, Throwable e) throws CoreException {
118                 // TODO: the plug-in code should be the example plug-in, not Perl debug model id
119                 throw new CoreException(new Status(IStatus.ERROR, IXDebugConstants.ID_PHP_DEBUG_MODEL, 0, message, e));
120         }
121
122 }