KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > utils > VMLauncherUtility


1 package com.bull.eclipse.jonas.utils;
2
3 /*
4  * (c) Copyright Bull SA 2003.
5  * All Rights Reserved.
6  */

7
8
9 import org.eclipse.core.runtime.CoreException;
10 import org.eclipse.debug.core.DebugPlugin;
11 import org.eclipse.debug.core.ILaunch;
12 import org.eclipse.debug.core.ILaunchConfigurationType;
13 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
14 import org.eclipse.debug.core.ILaunchManager;
15 import org.eclipse.debug.core.Launch;
16 import org.eclipse.debug.core.model.ISourceLocator;
17 import org.eclipse.debug.ui.DebugUITools;
18 import org.eclipse.debug.ui.IDebugUIConstants;
19 import org.eclipse.jdt.launching.IVMInstall;
20 import org.eclipse.jdt.launching.IVMInstallType;
21 import org.eclipse.jdt.launching.IVMRunner;
22 import org.eclipse.jdt.launching.JavaRuntime;
23 import org.eclipse.jdt.launching.VMRunnerConfiguration;
24
25 import com.bull.eclipse.jonas.JonasLauncherPlugin;
26
27
28
29
30 /**
31  * Utility class for launching a JVM in Eclipse and registering it to debugger
32  *
33  * It might exist better way to implements those operations,
34  * or they might already exist in other form JDT
35  */

36
37 public class VMLauncherUtility {
38
39     static public IVMInstall getVMInstall() {
40         IVMInstallType[] vmTypes = JavaRuntime.getVMInstallTypes();
41         for (int i = 0; i < vmTypes.length; i++) {
42             IVMInstall[] vms = vmTypes[i].getVMInstalls();
43             for (int j = 0; j < vms.length; j++) {
44                 if(vms[j].getId().equals(JonasLauncherPlugin.getDefault().getJonasJRE())) {
45                     return vms[j];
46                 }
47             }
48         }
49         return JavaRuntime.getDefaultVMInstall();
50     }
51
52
53     static public void runVM(String JavaDoc label, String JavaDoc classToLaunch, String JavaDoc[] classpath, String JavaDoc[] bootClasspath, String JavaDoc[] vmArgs, String JavaDoc[] prgArgs, ISourceLocator sourceLocator, boolean debug, boolean showInDebugger) throws CoreException{
54         runVM( label, classToLaunch, classpath, bootClasspath, vmArgs, prgArgs, sourceLocator, debug, showInDebugger, null );
55     }
56
57
58     static public void runVM( String JavaDoc label,
59                                 String JavaDoc classToLaunch,
60                                 String JavaDoc[] classpath,
61                                 String JavaDoc[] bootClasspath,
62                                 String JavaDoc[] vmArgs,
63                                 String JavaDoc[] prgArgs,
64                                 ISourceLocator sourceLocator,
65                                 boolean debug,
66                                 boolean showInDebugger,
67                                 String JavaDoc workingDir
68                             ) throws CoreException
69     {
70         IVMInstall vmInstall = getVMInstall();
71         String JavaDoc mode = "";
72         if (debug)
73             mode = ILaunchManager.DEBUG_MODE;
74         else
75             mode = ILaunchManager.RUN_MODE;
76
77         IVMRunner vmRunner = vmInstall.getVMRunner(mode);
78         
79         ILaunchConfigurationType launchType = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType("org.eclipse.jdt.launching.localJavaApplication");
80         ILaunchConfigurationWorkingCopy config = launchType.newInstance(null, label);
81         config.setAttribute(IDebugUIConstants.ATTR_PRIVATE, true);
82         DebugUITools.setLaunchPerspective(launchType,"debug",IDebugUIConstants.PERSPECTIVE_DEFAULT);
83         DebugUITools.setLaunchPerspective(launchType,"run",IDebugUIConstants.PERSPECTIVE_DEFAULT);
84         config.setAttribute(IDebugUIConstants.ATTR_TARGET_DEBUG_PERSPECTIVE, IDebugUIConstants.PERSPECTIVE_DEFAULT);
85         config.setAttribute(IDebugUIConstants.ATTR_TARGET_RUN_PERSPECTIVE, IDebugUIConstants.PERSPECTIVE_DEFAULT);
86 // config.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID, JavaUISourceLocator.ID_PROMPTING_JAVA_SOURCE_LOCATOR);
87

88         Launch launch = new Launch(config, mode, sourceLocator);
89
90         config.doSave();
91
92         if (vmRunner != null) {
93             VMRunnerConfiguration vmConfig =
94                 new VMRunnerConfiguration(classToLaunch, classpath);
95             vmConfig.setVMArguments(vmArgs);
96             vmConfig.setProgramArguments(prgArgs);
97
98             if( workingDir!=null ) { vmConfig.setWorkingDirectory( workingDir ); }
99
100             if(bootClasspath.length == 0) {
101                 vmConfig.setBootClassPath(null); // use default bootclasspath
102
} else {
103                 vmConfig.setBootClassPath(bootClasspath);
104             }
105
106
107             vmRunner.run(vmConfig, launch, null);
108
109         }
110
111         // Show in debugger
112
if(showInDebugger) {
113             DebugPlugin.getDefault().getLaunchManager().addLaunch(launch);
114         }
115
116     }
117
118
119     /**
120      * Register process to eclipse debugger
121      */

122     static public void showInDebugger(ILaunch launch) throws CoreException {
123         DebugPlugin.getDefault().getLaunchManager().addLaunch(launch);
124     }
125
126
127 }
128
Popular Tags