KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > VMHelper


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.pde.internal.ui.launcher;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.debug.core.ILaunchConfiguration;
17 import org.eclipse.jdt.launching.IVMInstall;
18 import org.eclipse.jdt.launching.IVMInstallType;
19 import org.eclipse.jdt.launching.JavaRuntime;
20 import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
21 import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.pde.internal.ui.PDEUIMessages;
24 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
25
26 public class VMHelper {
27
28     public static IVMInstall[] getAllVMInstances() {
29         ArrayList JavaDoc res = new ArrayList JavaDoc();
30         IVMInstallType[] types = JavaRuntime.getVMInstallTypes();
31         for (int i = 0; i < types.length; i++) {
32             IVMInstall[] installs = types[i].getVMInstalls();
33             for (int k = 0; k < installs.length; k++) {
34                 res.add(installs[k]);
35             }
36         }
37         return (IVMInstall[]) res.toArray(new IVMInstall[res.size()]);
38     }
39
40     public static String JavaDoc[] getVMInstallNames() {
41         IVMInstall[] installs = getAllVMInstances();
42         String JavaDoc[] names = new String JavaDoc[installs.length];
43         for (int i = 0; i < installs.length; i++) {
44             names[i] = installs[i].getName();
45         }
46         return names;
47     }
48
49     public static String JavaDoc getDefaultVMInstallName() {
50         IVMInstall install = JavaRuntime.getDefaultVMInstall();
51         if (install != null)
52             return install.getName();
53         return null;
54     }
55
56     public static String JavaDoc getDefaultVMInstallLocation() {
57         IVMInstall install = JavaRuntime.getDefaultVMInstall();
58         if (install != null)
59             return install.getInstallLocation().getAbsolutePath();
60         return null;
61     }
62     
63     public static IVMInstall getVMInstall(ILaunchConfiguration configuration) throws CoreException {
64         String JavaDoc vm = configuration.getAttribute(IPDELauncherConstants.VMINSTALL, (String JavaDoc) null);
65         return getVMInstall(vm);
66     }
67
68
69     public static IVMInstall getVMInstall(String JavaDoc name) {
70         if (name != null) {
71             IVMInstall[] installs = getAllVMInstances();
72             for (int i = 0; i < installs.length; i++) {
73                 if (installs[i].getName().equals(name))
74                     return installs[i];
75             }
76         }
77         return JavaRuntime.getDefaultVMInstall();
78     }
79
80     public static IVMInstall createLauncher(
81             ILaunchConfiguration configuration)
82     throws CoreException {
83         String JavaDoc vm = configuration.getAttribute(IPDELauncherConstants.VMINSTALL, (String JavaDoc) null);
84         IVMInstall launcher = getVMInstall(vm);
85         if (launcher == null)
86             throw new CoreException(
87                     LauncherUtils.createErrorStatus(NLS.bind(PDEUIMessages.WorkbenchLauncherConfigurationDelegate_noJRE, vm)));
88
89         if (!launcher.getInstallLocation().exists())
90             throw new CoreException(
91                     LauncherUtils.createErrorStatus(PDEUIMessages.WorkbenchLauncherConfigurationDelegate_jrePathNotFound));
92
93         return launcher;
94     }
95
96     public static IExecutionEnvironment[] getExecutionEnvironments() {
97         IExecutionEnvironmentsManager manager =
98             JavaRuntime.getExecutionEnvironmentsManager();
99         return manager.getExecutionEnvironments();
100     }
101
102 }
103
Popular Tags