KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > launching > AbstractVMRunner


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.launching;
12
13
14 import java.io.File JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.debug.core.DebugPlugin;
22 import org.eclipse.debug.core.ILaunch;
23 import org.eclipse.debug.core.model.IProcess;
24 import org.eclipse.jdt.internal.launching.LaunchingMessages;
25
26 /**
27  * Abstract implementation of a VM runner.
28  * <p>
29  * Clients implementing VM runners should subclass this class.
30  * </p>
31  * @see IVMRunner
32  * @since 2.0
33  */

34 public abstract class AbstractVMRunner implements IVMRunner {
35
36     /**
37      * Throws a core exception with an error status object built from
38      * the given message, lower level exception, and error code.
39      *
40      * @param message the status message
41      * @param exception lower level exception associated with the
42      * error, or <code>null</code> if none
43      * @param code error code
44      * @throws CoreException The exception encapsulating the reason for the abort
45      */

46     protected void abort(String JavaDoc message, Throwable JavaDoc exception, int code) throws CoreException {
47         throw new CoreException(new Status(IStatus.ERROR, getPluginIdentifier(), code, message, exception));
48     }
49     
50     /**
51      * Returns the identifier of the plug-in this VM runner
52      * originated from.
53      *
54      * @return plug-in identifier
55      */

56     protected abstract String JavaDoc getPluginIdentifier();
57     
58     /**
59      * @see DebugPlugin#exec(String[], File)
60      */

61     protected Process JavaDoc exec(String JavaDoc[] cmdLine, File JavaDoc workingDirectory) throws CoreException {
62         return DebugPlugin.exec(cmdLine, workingDirectory);
63     }
64     
65     /**
66      * @since 3.0
67      * @see DebugPlugin#exec(String[], File, String[])
68      */

69     protected Process JavaDoc exec(String JavaDoc[] cmdLine, File JavaDoc workingDirectory, String JavaDoc[] envp) throws CoreException {
70         return DebugPlugin.exec(cmdLine, workingDirectory, envp);
71     }
72     
73     /**
74      * Returns the given array of strings as a single space-delimited string.
75      *
76      * @param cmdLine array of strings
77      * @return a single space-delimited string
78      */

79     protected String JavaDoc getCmdLineAsString(String JavaDoc[] cmdLine) {
80         StringBuffer JavaDoc buff= new StringBuffer JavaDoc();
81         for (int i = 0, numStrings= cmdLine.length; i < numStrings; i++) {
82             buff.append(cmdLine[i]);
83             buff.append(' ');
84         }
85         return buff.toString().trim();
86     }
87     
88     /**
89      * Returns the default process attribute map for Java processes.
90      *
91      * @return default process attribute map for Java processes
92      */

93     protected Map JavaDoc getDefaultProcessMap() {
94         Map JavaDoc map = new HashMap JavaDoc();
95         map.put(IProcess.ATTR_PROCESS_TYPE, IJavaLaunchConfigurationConstants.ID_JAVA_PROCESS_TYPE);
96         return map;
97     }
98     
99     /**
100      * Returns a new process aborting if the process could not be created.
101      * @param launch the launch the process is contained in
102      * @param p the system process to wrap
103      * @param label the label assigned to the process
104      * @param attributes values for the attribute map
105      * @return the new process
106      * @throws CoreException problems occurred creating the process
107      * @since 3.0
108      */

109     protected IProcess newProcess(ILaunch launch, Process JavaDoc p, String JavaDoc label, Map JavaDoc attributes) throws CoreException {
110         IProcess process= DebugPlugin.newProcess(launch, p, label, attributes);
111         if (process == null) {
112             p.destroy();
113             abort(LaunchingMessages.AbstractVMRunner_0, null, IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
114         }
115         return process;
116     }
117     
118     /**
119      * Combines and returns VM arguments specified by the runner configuration,
120      * with those specified by the VM install, if any.
121      *
122      * @param configuration runner configuration
123      * @param vmInstall vm install
124      * @return combined VM arguments specified by the runner configuration
125      * and VM install
126      * @since 3.0
127      */

128     protected String JavaDoc[] combineVmArgs(VMRunnerConfiguration configuration, IVMInstall vmInstall) {
129         String JavaDoc[] launchVMArgs= configuration.getVMArguments();
130         String JavaDoc[] vmVMArgs = vmInstall.getVMArguments();
131         if (vmVMArgs == null || vmVMArgs.length == 0) {
132             return launchVMArgs;
133         }
134         String JavaDoc[] allVMArgs = new String JavaDoc[launchVMArgs.length + vmVMArgs.length];
135         System.arraycopy(launchVMArgs, 0, allVMArgs, 0, launchVMArgs.length);
136         System.arraycopy(vmVMArgs, 0, allVMArgs, launchVMArgs.length, vmVMArgs.length);
137         return allVMArgs;
138     }
139 }
140
Popular Tags