1 package net.sourceforge.phpdt.externaltools.launchConfigurations;
3 /**********************************************************************
4 Copyright (c) 2002 IBM Corp. and others. All rights reserved.
5 This file is made available under the terms of the Common Public License v1.0
6 which accompanies this distribution, and is available at
7 http://www.eclipse.org/legal/cpl-v10.html
10 **********************************************************************/
13 import java.text.MessageFormat;
16 import net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsPlugin;
17 import net.sourceforge.phpdt.externaltools.internal.model.ToolMessages;
18 import net.sourceforge.phpdt.externaltools.internal.model.VariableContextManager;
19 import net.sourceforge.phpdt.externaltools.internal.registry.ExternalToolMigration;
20 import net.sourceforge.phpdt.externaltools.internal.registry.RefreshScopeVariable;
21 import net.sourceforge.phpdt.externaltools.internal.registry.RefreshScopeVariableRegistry;
22 import net.sourceforge.phpdt.externaltools.model.IExternalToolConstants;
23 import net.sourceforge.phpdt.externaltools.model.ToolUtil;
24 import net.sourceforge.phpdt.externaltools.variable.ExpandVariableContext;
26 import org.eclipse.core.resources.IResource;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IPath;
29 import org.eclipse.core.runtime.IProgressMonitor;
30 import org.eclipse.core.runtime.IStatus;
31 import org.eclipse.core.runtime.MultiStatus;
32 import org.eclipse.core.runtime.Path;
33 import org.eclipse.core.runtime.Status;
34 import org.eclipse.debug.core.DebugPlugin;
35 import org.eclipse.debug.core.ILaunchConfiguration;
36 import org.eclipse.debug.core.ILaunchConfigurationType;
37 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
38 import org.eclipse.debug.core.ILaunchManager;
41 * Utilities for external tool launch configurations.
43 * This class it not intended to be instantiated.
46 public class ExternalToolsUtil {
48 private static final String LAUNCH_CONFIG_HANDLE = "LaunchConfigHandle"; //$NON-NLS-1$
51 * Not to be instantiated.
53 private ExternalToolsUtil() {
57 * Throws a core exception with an error status object built from
58 * the given message, lower level exception, and error code.
60 * @param message the status message
61 * @param exception lower level exception associated with the
62 * error, or <code>null</code> if none
63 * @param code error code
65 protected static void abort(String message, Throwable exception, int code) throws CoreException {
66 throw new CoreException(new Status(IStatus.ERROR, IExternalToolConstants.PLUGIN_ID, code, message, exception));
70 * Returns active variable context. The active variable context is used to
71 * expand variable expressions. If the workspace is currently being built,
72 * the context is associated with the project being built. Otherwise, the
73 * context is associated with the selected resource.
75 * @return active variable context
77 public static ExpandVariableContext getVariableContext() {
78 return VariableContextManager.getDefault().getVariableContext();
82 * Expands and returns the location attribute of the given launch
83 * configuration, based on the given variable context. The location is
84 * verified to point to an existing file, in the local file system.
86 * @param configuration launch configuration
87 * @param context context used to expand variables
88 * @return an absolute path to a file in the local file system
89 * @throws CoreException if unable to retrieve the associated launch
90 * configuration attribute, if unable to resolve any variables, or if the
91 * resolved location does not point to an existing file in the local file
94 public static IPath getLocation(ILaunchConfiguration configuration, ExpandVariableContext context) throws CoreException {
95 String location = configuration.getAttribute(IExternalToolConstants.ATTR_LOCATION, (String) null);
96 if (location == null) {
97 abort(MessageFormat.format(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsUtil.Location_not_specified_by_{0}_1"), new String[] { configuration.getName()}), null, 0); //$NON-NLS-1$
99 MultiStatus status = new MultiStatus(IExternalToolConstants.PLUGIN_ID, 0, ToolMessages.getString("RunExternalToolAction.runProblem"), null); //$NON-NLS-1$;
100 String expandedLocation = ToolUtil.expandFileLocation(location, context, status);
102 if (expandedLocation == null || expandedLocation.length() == 0) {
103 String msg = ToolMessages.format("DefaultRunnerContext.invalidLocation", new Object[] { configuration.getName()}); //$NON-NLS-1$
106 File file = new File(expandedLocation);
108 return new Path(expandedLocation);
110 String msg = ToolMessages.format("DefaultRunnerContext.invalidLocation", new Object[] { configuration.getName()}); //$NON-NLS-1$
115 throw new CoreException(status);
118 // execution will not reach here
123 * Expands and returns the working directory attribute of the given launch
124 * configuration, based on the given variable context. Returns
125 * <code>null</code> if a working directory is not specified. If specified,
126 * the working is verified to point to an existing directory in the local
129 * @param configuration launch configuration
130 * @param context context used to expand variables
131 * @return an absolute path to a direcoty in the local file system, or
132 * <code>null</code> if unspecified
133 * @throws CoreException if unable to retrieve the associated launch
134 * configuration attribute, if unable to resolve any variables, or if the
135 * resolved location does not point to an existing directory in the local
138 public static IPath getWorkingDirectory(ILaunchConfiguration configuration, ExpandVariableContext context) throws CoreException {
139 String location = configuration.getAttribute(IExternalToolConstants.ATTR_WORKING_DIRECTORY, (String) null);
140 if (location != null) {
141 MultiStatus status = new MultiStatus(IExternalToolConstants.PLUGIN_ID, 0, ToolMessages.getString("RunExternalToolAction.runProblem"), null); //$NON-NLS-1$;
142 String expandedLocation = ToolUtil.expandDirectoryLocation(location, context, status);
144 if (expandedLocation != null && expandedLocation.length() > 0) {
145 File path = new File(expandedLocation);
146 if (path.isDirectory()) {
147 return new Path(expandedLocation);
149 String msg = ToolMessages.format("DefaultRunnerContext.invalidDirectory", new Object[] { configuration.getName()}); //$NON-NLS-1$
154 throw new CoreException(status);
161 * Expands and returns the arguments attribute of the given launch
162 * configuration, based on the given variable context. Returns
163 * <code>null</code> if arguments are not specified.
165 * @param configuration launch configuration
166 * @param context context used to expand variables
167 * @return an array of resolved arguments, or <code>null</code> if
169 * @throws CoreException if unable to retrieve the associated launch
170 * configuration attribute, or if unable to resolve any variables
172 public static String[] getArguments(ILaunchConfiguration configuration, ExpandVariableContext context) throws CoreException {
173 String args = configuration.getAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, (String) null);
175 MultiStatus status = new MultiStatus(IExternalToolConstants.PLUGIN_ID, 0, ToolMessages.getString("RunExternalToolAction.runProblem"), null); //$NON-NLS-1$;
176 String[] expandedArgs = ToolUtil.expandArguments(args, context, status);
180 throw new CoreException(status);
187 * Returns the refresh scope specified by the given launch configuration or
188 * <code>null</code> if none.
190 * @param configuration
191 * @return refresh scope
192 * @throws CoreException if unable to access the associated attribute
194 public static String getRefreshScope(ILaunchConfiguration configuration) throws CoreException {
195 return configuration.getAttribute(IExternalToolConstants.ATTR_REFRESH_SCOPE, (String) null);
199 * Returns whether the refresh scope specified by the given launch
200 * configuration is recursive.
202 * @param configuration
203 * @return whether the refresh scope is recursive
204 * @throws CoreException if unable to access the associated attribute
206 public static boolean isRefreshRecursive(ILaunchConfiguration configuration) throws CoreException {
207 return configuration.getAttribute(IExternalToolConstants.ATTR_REFRESH_RECURSIVE, false);
211 * Refreshes the resources as specified by the given launch configuration.
213 * @param configuration launch configuration
214 * @param context context used to expand variables
215 * @param monitor progress monitor
216 * @throws CoreException if an exception occurrs while refreshing resources
218 public static void refreshResources(ILaunchConfiguration configuration, ExpandVariableContext context, IProgressMonitor monitor)
219 throws CoreException {
220 String scope = getRefreshScope(configuration);
224 ToolUtil.VariableDefinition varDef = ToolUtil.extractVariableTag(scope, 0);
225 if (varDef.start == -1 || varDef.end == -1 || varDef.name == null) {
226 String msg = ToolMessages.format("DefaultRunnerContext.invalidRefreshVarFormat", new Object[] { configuration.getName()}); //$NON-NLS-1$
230 RefreshScopeVariableRegistry registry = ExternalToolsPlugin.getDefault().getRefreshVariableRegistry();
231 RefreshScopeVariable variable = registry.getRefreshVariable(varDef.name);
232 if (variable == null) {
233 String msg = ToolMessages.format("DefaultRunnerContext.noRefreshVarNamed", new Object[] { configuration.getName(), varDef.name }); //$NON-NLS-1$
237 int depth = IResource.DEPTH_ZERO;
238 if (isRefreshRecursive(configuration))
239 depth = IResource.DEPTH_INFINITE;
241 if (monitor.isCanceled())
244 IResource[] resources = variable.getExpander().getResources(varDef.name, varDef.argument, context);
245 if (resources == null || resources.length == 0)
248 monitor.beginTask(ToolMessages.getString("DefaultRunnerContext.refreshResources"), //$NON-NLS-1$
251 MultiStatus status = new MultiStatus(IExternalToolConstants.PLUGIN_ID, 0, ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsUtil.Exception(s)_occurred_during_refresh._2"), null); //$NON-NLS-1$
252 for (int i = 0; i < resources.length; i++) {
253 if (monitor.isCanceled())
255 if (resources[i] != null && resources[i].isAccessible()) {
257 resources[i].refreshLocal(depth, null);
258 } catch (CoreException e) {
259 status.merge(e.getStatus());
266 if (!status.isOK()) {
267 throw new CoreException(status);
272 * Returns whether this tool is to be run in the background..
274 * @param configuration
275 * @return whether this tool is to be run in the background
276 * @throws CoreException if unable to access the associated attribute
278 public static boolean isBackground(ILaunchConfiguration configuration) throws CoreException {
279 return configuration.getAttribute(IExternalToolConstants.ATTR_RUN_IN_BACKGROUND, false);
283 * Returns a launch configuration from the given ICommand arguments. If the
284 * given arguments are from an old-style external tool, an unsaved working
285 * copy will be created from the arguments and returned.
287 * @param commandArgs the builder ICommand arguments
288 * @param newName a new name for the config if the one in the command is
290 * @return a launch configuration, a launch configuration working copy, or
291 * <code>null</code> if not possible.
293 public static ILaunchConfiguration configFromBuildCommandArgs(Map commandArgs) {
294 String configHandle = (String) commandArgs.get(LAUNCH_CONFIG_HANDLE);
295 if (configHandle == null) {
296 // Probably an old-style external tool. Try to migrate.
297 return ExternalToolMigration.configFromArgumentMap(commandArgs);
300 return DebugPlugin.getDefault().getLaunchManager().getLaunchConfiguration(configHandle);
301 } catch (CoreException e) {
306 * Executes an external progam and saves the LaunchConfiguration under external tools
307 * @param command external tools command name
308 * @param executable executable path i.e.c:\apache\apache.exe
309 * @param arguments arguments for this configuration
310 * @param background run this configuration in background mode
312 public static void execute(String command, String executable, String arguments, boolean background) {
313 ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
314 ILaunchConfigurationType type = manager.getLaunchConfigurationType(IExternalToolConstants.ID_PROGRAM_LAUNCH_CONFIGURATION_TYPE);
316 ILaunchConfigurationWorkingCopy wc = null;
318 wc = type.newInstance(null, command);
319 } catch (CoreException e) {
320 //some exception handling
322 wc.setAttribute(IExternalToolConstants.ATTR_LOCATION, executable);
323 wc.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, arguments);
324 wc.setAttribute(IExternalToolConstants.ATTR_RUN_IN_BACKGROUND, background);
326 // saving will add the configuration to the external tools configurations
327 ILaunchConfiguration config;
329 config = wc.doSave();
330 config.launch(ILaunchManager.RUN_MODE, null);
331 } catch (CoreException e) {