001: package com.salmonllc.ideTools.eclipse;
002:
003: import org.eclipse.core.resources.*;
004: import org.eclipse.core.runtime.CoreException;
005: import org.eclipse.debug.core.DebugPlugin;
006: import org.eclipse.debug.core.ILaunchConfigurationType;
007: import org.eclipse.debug.core.ILaunchManager;
008: import org.eclipse.debug.core.IStreamListener;
009: import org.eclipse.debug.core.Launch;
010: import org.eclipse.debug.core.model.IProcess;
011: import org.eclipse.debug.core.model.ISourceLocator;
012: import org.eclipse.jdt.core.IJavaProject;
013: import org.eclipse.jdt.launching.IVMConnector;
014: import org.eclipse.jdt.launching.IVMInstall;
015: import org.eclipse.jdt.launching.IVMInstallType;
016: import org.eclipse.jdt.launching.IVMRunner;
017: import org.eclipse.jdt.launching.JavaRuntime;
018: import org.eclipse.jdt.launching.VMRunnerConfiguration;
019: import org.eclipse.jdt.launching.sourcelookup.JavaProjectSourceLocation;
020: import org.eclipse.jdt.launching.sourcelookup.JavaSourceLocator;
021: import org.eclipse.jface.preference.IPreferenceStore;
022:
023: /**
024: * @author Administrator
025: *
026: * To change this generated comment edit the template variable "typecomment":
027: * Window>Preferences>Java>Templates.
028: * To enable and disable the creation of type comments go to
029: * Window>Preferences>Java>Code Generation.
030: */
031: public class ProgramLauncher {
032:
033: public static Launch runProgram(String classToLaunch,
034: String classPath[], String vmArgs[], String programArgs[],
035: String workingDir, boolean debug) throws CoreException {
036: return runProgram(classToLaunch, classPath, vmArgs,
037: programArgs, workingDir, debug, null, null);
038: }
039:
040: public static Launch runProgram(String classToLaunch,
041: String classPath[], String vmArgs[], String programArgs[],
042: String workingDir, boolean debug, IStreamListener listener,
043: String vmName) throws CoreException {
044: IVMInstall vmInstall = null;
045: if (vmName != null) {
046: IVMInstallType[] test = JavaRuntime.getVMInstallTypes();
047: outer: for (int i = 0; i < test.length; i++) {
048: IVMInstall installs[] = test[i].getVMInstalls();
049: for (int j = 0; j < installs.length; j++) {
050: if (installs[j].getName().equals(vmName)) {
051: vmInstall = installs[j];
052: break outer;
053: }
054: }
055: }
056: }
057: if (vmInstall == null)
058: vmInstall = JavaRuntime.getDefaultVMInstall();
059:
060: IPreferenceStore pref = SalmonPlugin.getDefault()
061: .getPreferenceStore();
062: boolean debugLaunch = pref
063: .getBoolean(SalmonPlugin.PREF_DEBUG_LAUNCH);
064: if (debugLaunch) {
065: StringBuffer sb = new StringBuffer();
066: sb.append("Class=" + classToLaunch + "\r\n");
067: sb.append("Working Dir=" + workingDir + "\r\n");
068: if (classPath != null) {
069: sb.append("ClassPath=\r\n");
070: for (int i = 0; i < classPath.length; i++)
071: sb.append(" " + classPath[i] + "\r\n");
072: }
073: if (vmArgs != null) {
074: sb.append("VM Args=\r\n");
075: for (int i = 0; i < vmArgs.length; i++)
076: sb.append(" " + vmArgs[i] + "\r\n");
077: }
078:
079: if (programArgs != null) {
080: sb.append("Program args=\r\n");
081: for (int i = 0; i < programArgs.length; i++)
082: sb.append(programArgs[i] + "\r\n");
083: }
084: SalmonPlugin.displayError("VM Launch", sb.toString());
085: debug = true;
086: }
087:
088: ILaunchConfigurationType launchTypes[] = DebugPlugin
089: .getDefault().getLaunchManager()
090: .getLaunchConfigurationTypes();
091: org.eclipse.debug.core.ILaunchConfiguration config = launchTypes[0]
092: .newInstance(null, "Salmon Launcher");
093: Launch launch = null;
094: String mode = debug ? ILaunchManager.DEBUG_MODE
095: : ILaunchManager.RUN_MODE;
096: if (vmInstall != null) {
097: IVMRunner vmRunner = vmInstall.getVMRunner(mode);
098: if (vmRunner != null) {
099: VMRunnerConfiguration vmConfig = new VMRunnerConfiguration(
100: classToLaunch, classPath);
101: if (vmArgs != null)
102: vmConfig.setVMArguments(vmArgs);
103: if (programArgs != null)
104: vmConfig.setProgramArguments(programArgs);
105: if (workingDir != null)
106: vmConfig.setWorkingDirectory(workingDir);
107: launch = new Launch(config, mode, getSourceLocator());
108: vmRunner.run(vmConfig, launch, null);
109: if (listener != null) {
110: IProcess processes[] = launch.getProcesses();
111: if (processes != null && processes.length > 0)
112: processes[0].getStreamsProxy()
113: .getOutputStreamMonitor().addListener(
114: listener);
115: }
116: }
117: }
118: if (debug)
119: DebugPlugin.getDefault().getLaunchManager().addLaunch(
120: launch);
121: return launch;
122: }
123:
124: private static ISourceLocator getSourceLocator()
125: throws CoreException {
126: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
127: IProject allProjects[] = root.getProjects();
128: int skippedProjects = 0;
129: IJavaProject javaProjects[] = new IJavaProject[allProjects.length];
130: int i = 0;
131: int javaIndex = 0;
132: for (int max = allProjects.length; i < max; i++)
133: if (allProjects[i].isOpen()
134: && allProjects[i]
135: .hasNature("org.eclipse.jdt.core.javanature"))
136: javaProjects[javaIndex++] = (IJavaProject) allProjects[i]
137: .getNature("org.eclipse.jdt.core.javanature");
138: else
139: skippedProjects++;
140:
141: JavaProjectSourceLocation sourceLocations[] = new JavaProjectSourceLocation[allProjects.length
142: - skippedProjects];
143: i = 0;
144: for (int max = sourceLocations.length; i < max; i++)
145: sourceLocations[i] = new JavaProjectSourceLocation(
146: javaProjects[i]);
147:
148: return new JavaSourceLocator(sourceLocations);
149: }
150: }
|