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.List;
 
  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;
 
  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;
 
  40 public class PHPLaunchConfigurationDelegate extends LaunchConfigurationDelegate {
 
  43          * @see ILaunchConfigurationDelegate#launch(ILaunchConfiguration, String, ILaunch, IProgressMonitor)
 
  45         public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
 
  46                 List commandList = new ArrayList();
 
  48                 String phpInterpreter = configuration.getAttribute(IXDebugConstants.ATTR_PHP_INTERPRETER, (String)null);
 
  49                 boolean useDefaultInterpreter= configuration.getAttribute(IXDebugConstants.ATTR_PHP_DEFAULT_INTERPRETER, true);
 
  51                 if (useDefaultInterpreter)              
 
  52                         phpInterpreter=XDebugCorePlugin.getDefault().getPreferenceStore().getString(IXDebugPreferenceConstants.PHP_INTERPRETER_PREFERENCE);
 
  54                 File exe = new File(phpInterpreter);
 
  55                 // Just to get sure that the interpreter exists
 
  57                         abort(MessageFormat.format("Specified PHP executable {0} does not exist. Check value of PHP-Interpreter.", new String[]{phpInterpreter}), null);
 
  59                 commandList.add(phpInterpreter);
 
  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);
 
  68                 String fileName = configuration.getAttribute(IXDebugConstants.ATTR_PHP_FILE, (String)null);
 
  70                 IFile file = project.getFile(fileName);
 
  71                 // Just to get sure that the script exists
 
  73                         abort(MessageFormat.format("PHP-Script {0} does not exist.", new String[] {file.getFullPath().toString()}), null);
 
  76                 commandList.add(file.getLocation().toOSString());
 
  77                 String[] envp=DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration);
 
  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);
 
  83                         debugPort=XDebugCorePlugin.getDefault().getPreferenceStore().getInt(IXDebugPreferenceConstants.DEBUGPORT_PREFERENCE);
 
  85                         debugPort=IXDebugPreferenceConstants.DEFAULT_DEBUGPORT;
 
  87                 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
 
  90                                 env = new String[envp.length+1];
 
  91                                 for(int i=0;i<envp.length;i++)
 
  95                         env[0]="XDEBUG_CONFIG=idekey=xdebug_test remote_enable=1";
 
 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);
 
 110          * Throws an exception with a new status containing the given
 
 111          * message and optional exception.
 
 113          * @param message error message
 
 114          * @param e underlying exception
 
 115          * @throws CoreException
 
 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));