001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 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.pde.internal.ui.launcher;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.debug.core.ILaunchConfiguration;
016: import org.eclipse.jdt.launching.IVMInstall;
017: import org.eclipse.jdt.launching.IVMInstallType;
018: import org.eclipse.jdt.launching.JavaRuntime;
019: import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
020: import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
021: import org.eclipse.osgi.util.NLS;
022: import org.eclipse.pde.internal.ui.PDEUIMessages;
023: import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
024:
025: public class VMHelper {
026:
027: public static IVMInstall[] getAllVMInstances() {
028: ArrayList res = new ArrayList();
029: IVMInstallType[] types = JavaRuntime.getVMInstallTypes();
030: for (int i = 0; i < types.length; i++) {
031: IVMInstall[] installs = types[i].getVMInstalls();
032: for (int k = 0; k < installs.length; k++) {
033: res.add(installs[k]);
034: }
035: }
036: return (IVMInstall[]) res.toArray(new IVMInstall[res.size()]);
037: }
038:
039: public static String[] getVMInstallNames() {
040: IVMInstall[] installs = getAllVMInstances();
041: String[] names = new String[installs.length];
042: for (int i = 0; i < installs.length; i++) {
043: names[i] = installs[i].getName();
044: }
045: return names;
046: }
047:
048: public static String getDefaultVMInstallName() {
049: IVMInstall install = JavaRuntime.getDefaultVMInstall();
050: if (install != null)
051: return install.getName();
052: return null;
053: }
054:
055: public static String getDefaultVMInstallLocation() {
056: IVMInstall install = JavaRuntime.getDefaultVMInstall();
057: if (install != null)
058: return install.getInstallLocation().getAbsolutePath();
059: return null;
060: }
061:
062: public static IVMInstall getVMInstall(
063: ILaunchConfiguration configuration) throws CoreException {
064: String vm = configuration.getAttribute(
065: IPDELauncherConstants.VMINSTALL, (String) null);
066: return getVMInstall(vm);
067: }
068:
069: public static IVMInstall getVMInstall(String name) {
070: if (name != null) {
071: IVMInstall[] installs = getAllVMInstances();
072: for (int i = 0; i < installs.length; i++) {
073: if (installs[i].getName().equals(name))
074: return installs[i];
075: }
076: }
077: return JavaRuntime.getDefaultVMInstall();
078: }
079:
080: public static IVMInstall createLauncher(
081: ILaunchConfiguration configuration) throws CoreException {
082: boolean vmSelected = configuration.getAttribute(
083: IPDELauncherConstants.USE_VMINSTALL, true); //$NON-NLS-1$ //$NON-NLS-2$
084: String vm;
085: if (vmSelected)
086: vm = configuration.getAttribute(
087: IPDELauncherConstants.VMINSTALL, (String) null);
088: else {
089: String id = configuration.getAttribute(
090: IPDELauncherConstants.EXECUTION_ENVIRONMENT,
091: (String) null);
092: if (id != null) {
093: IExecutionEnvironment ee = getExecutionEnvironment(id);
094: if (ee == null)
095: throw new CoreException(
096: LauncherUtils
097: .createErrorStatus(NLS
098: .bind(
099: PDEUIMessages.VMHelper_cannotFindExecEnv,
100: id)));
101: vm = getVMInstallName(ee);
102: } else
103: vm = getDefaultVMInstallName();
104: }
105: IVMInstall launcher = getVMInstall(vm);
106: if (launcher == null)
107: throw new CoreException(
108: LauncherUtils
109: .createErrorStatus(NLS
110: .bind(
111: PDEUIMessages.WorkbenchLauncherConfigurationDelegate_noJRE,
112: vm)));
113:
114: if (!launcher.getInstallLocation().exists())
115: throw new CoreException(
116: LauncherUtils
117: .createErrorStatus(PDEUIMessages.WorkbenchLauncherConfigurationDelegate_jrePathNotFound));
118:
119: return launcher;
120: }
121:
122: public static IExecutionEnvironment[] getExecutionEnvironments() {
123: IExecutionEnvironmentsManager manager = JavaRuntime
124: .getExecutionEnvironmentsManager();
125: return manager.getExecutionEnvironments();
126: }
127:
128: public static IExecutionEnvironment getExecutionEnvironment(
129: String id) {
130: IExecutionEnvironmentsManager manager = JavaRuntime
131: .getExecutionEnvironmentsManager();
132: return manager.getEnvironment(id);
133: }
134:
135: public static String getVMInstallName(IExecutionEnvironment ee)
136: throws CoreException {
137: IVMInstall vmi = ee.getDefaultVM();
138: if (vmi == null) {
139: IVMInstall[] vmis = ee.getCompatibleVMs();
140: for (int i = 0; i < vmis.length; i++) {
141: if (ee.isStrictlyCompatible(vmis[i])) {
142: vmi = vmis[i];
143: break;
144: }
145: if (vmi == null)
146: vmi = vmis[i];
147: }
148: if (vmi == null)
149: throw new CoreException(LauncherUtils
150: .createErrorStatus(NLS.bind(
151: PDEUIMessages.VMHelper_noJreForExecEnv,
152: ee.getId())));
153: }
154: return vmi.getName();
155: }
156: }
|