001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.launching;
011:
012: import java.io.File;
013: import java.util.HashMap;
014: import java.util.Map;
015:
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IStatus;
018: import org.eclipse.core.runtime.Status;
019: import org.eclipse.debug.core.DebugPlugin;
020: import org.eclipse.debug.core.ILaunch;
021: import org.eclipse.debug.core.model.IProcess;
022: import org.eclipse.jdt.internal.launching.LaunchingMessages;
023:
024: /**
025: * Abstract implementation of a VM runner.
026: * <p>
027: * Clients implementing VM runners should subclass this class.
028: * </p>
029: * @see IVMRunner
030: * @since 2.0
031: */
032: public abstract class AbstractVMRunner implements IVMRunner {
033:
034: /**
035: * Throws a core exception with an error status object built from
036: * the given message, lower level exception, and error code.
037: *
038: * @param message the status message
039: * @param exception lower level exception associated with the
040: * error, or <code>null</code> if none
041: * @param code error code
042: * @throws CoreException The exception encapsulating the reason for the abort
043: */
044: protected void abort(String message, Throwable exception, int code)
045: throws CoreException {
046: throw new CoreException(new Status(IStatus.ERROR,
047: getPluginIdentifier(), code, message, exception));
048: }
049:
050: /**
051: * Returns the identifier of the plug-in this VM runner
052: * originated from.
053: *
054: * @return plug-in identifier
055: */
056: protected abstract String getPluginIdentifier();
057:
058: /**
059: * @see DebugPlugin#exec(String[], File)
060: */
061: protected Process exec(String[] cmdLine, File workingDirectory)
062: throws CoreException {
063: return DebugPlugin.exec(cmdLine, workingDirectory);
064: }
065:
066: /**
067: * @since 3.0
068: * @see DebugPlugin#exec(String[], File, String[])
069: */
070: protected Process exec(String[] cmdLine, File workingDirectory,
071: String[] envp) throws CoreException {
072: return DebugPlugin.exec(cmdLine, workingDirectory, envp);
073: }
074:
075: /**
076: * Returns the given array of strings as a single space-delimited string.
077: *
078: * @param cmdLine array of strings
079: * @return a single space-delimited string
080: */
081: protected String getCmdLineAsString(String[] cmdLine) {
082: StringBuffer buff = new StringBuffer();
083: for (int i = 0, numStrings = cmdLine.length; i < numStrings; i++) {
084: buff.append(cmdLine[i]);
085: buff.append(' ');
086: }
087: return buff.toString().trim();
088: }
089:
090: /**
091: * Returns the default process attribute map for Java processes.
092: *
093: * @return default process attribute map for Java processes
094: */
095: protected Map getDefaultProcessMap() {
096: Map map = new HashMap();
097: map.put(IProcess.ATTR_PROCESS_TYPE,
098: IJavaLaunchConfigurationConstants.ID_JAVA_PROCESS_TYPE);
099: return map;
100: }
101:
102: /**
103: * Returns a new process aborting if the process could not be created.
104: * @param launch the launch the process is contained in
105: * @param p the system process to wrap
106: * @param label the label assigned to the process
107: * @param attributes values for the attribute map
108: * @return the new process
109: * @throws CoreException problems occurred creating the process
110: * @since 3.0
111: */
112: protected IProcess newProcess(ILaunch launch, Process p,
113: String label, Map attributes) throws CoreException {
114: IProcess process = DebugPlugin.newProcess(launch, p, label,
115: attributes);
116: if (process == null) {
117: p.destroy();
118: abort(
119: LaunchingMessages.AbstractVMRunner_0,
120: null,
121: IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
122: }
123: return process;
124: }
125:
126: /**
127: * Combines and returns VM arguments specified by the runner configuration,
128: * with those specified by the VM install, if any.
129: *
130: * @param configuration runner configuration
131: * @param vmInstall vm install
132: * @return combined VM arguments specified by the runner configuration
133: * and VM install
134: * @since 3.0
135: */
136: protected String[] combineVmArgs(
137: VMRunnerConfiguration configuration, IVMInstall vmInstall) {
138: String[] launchVMArgs = configuration.getVMArguments();
139: String[] vmVMArgs = vmInstall.getVMArguments();
140: if (vmVMArgs == null || vmVMArgs.length == 0) {
141: return launchVMArgs;
142: }
143: String[] allVMArgs = new String[launchVMArgs.length
144: + vmVMArgs.length];
145: System.arraycopy(launchVMArgs, 0, allVMArgs, 0,
146: launchVMArgs.length);
147: System.arraycopy(vmVMArgs, 0, allVMArgs, launchVMArgs.length,
148: vmVMArgs.length);
149: return allVMArgs;
150: }
151: }
|