d464f646f897462f05eb3212b42016e0dc720ce4
[phpeclipse.git] /
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.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;
25
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;
41
42
43 public class PHPLaunchConfigurationDelegate extends LaunchConfigurationDelegate {
44         
45         /**
46          * @see ILaunchConfigurationDelegate#launch(ILaunchConfiguration, String, ILaunch, IProgressMonitor)
47          */
48         public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
49                 List commandList = new ArrayList();
50                 
51                 String phpInterpreter = configuration.getAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER, (String)null);
52                 boolean useDefaultInterpreter= configuration.getAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, true);
53
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);
58                         }
59                 }
60                 File exe = new File(phpInterpreter);
61                 // Just to get sure that the interpreter exists
62                 if (!exe.exists()) {
63                         abort(MessageFormat.format("Specified PHP executable {0} does not exist. Check value of PHP-Interpreter.", new String[]{phpInterpreter}), null);
64                 }
65                 commandList.add(phpInterpreter);
66                 
67                 // Project name
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);
73                 }
74                 String fileName = configuration.getAttribute(IXDebugConstants.ATTR_PHP_FILE, (String)null);
75
76                 IFile file = project.getFile(fileName);
77                 // Just to get sure that the script exists
78                 if (!file.exists()) {
79                         abort(MessageFormat.format("PHP-Script {0} does not exist.", new String[] {file.getFullPath().toString()}), null);
80                 }
81                 
82                 commandList.add(file.getLocation().toOSString());
83
84                 // Get the Debugport  from the preferences
85                 int debugPort=XDebugCorePlugin.getDefault().getPreferenceStore().getInt(IXDebugPreferenceConstants.DEBUGPORT_PREFERENCE);
86                 
87                 // check for default port
88                 if (debugPort==0)
89                         debugPort=IXDebugPreferenceConstants.DEFAULT_DEBUGPORT;
90
91                 String[] envp=DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration);
92                 // appends the environment to the native environment
93                 if (envp==null) {
94                         Map stringVars = DebugPlugin.getDefault().getLaunchManager().getNativeEnvironment();
95                         int idx=0;
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;
101                         }
102                 }
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++)
107                                         env[i+1]=envp[i];
108                         env[0]="XDEBUG_CONFIG=idekey="+idekey+" remote_enable=1 remote_port="+debugPort;
109                         envp=env;
110                 }
111                 System.out.println("ideKey= "+idekey);
112                 
113                 String[] commandLine = (String[]) commandList.toArray(new String[commandList.size()]);
114
115                 XDebugTarget target=null;
116                 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
117                         target = new XDebugTarget(launch, null, idekey);
118                 }
119                 Process process = DebugPlugin.exec(commandLine, null,envp);
120                 IProcess p = DebugPlugin.newProcess(launch, process, phpInterpreter);
121         
122                 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
123                         target.addProcess(p);
124                         launch.addDebugTarget(target);
125                 }
126
127         }
128         
129         /**
130          * Throws an exception with a new status containing the given
131          * message and optional exception.
132          * 
133          * @param message error message
134          * @param e underlying exception
135          * @throws CoreException
136          */
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));
140         }
141
142 }