001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.dso.launch;
006:
007: import org.eclipse.core.resources.IProject;
008: import org.eclipse.core.resources.ResourcesPlugin;
009: import org.eclipse.core.runtime.CoreException;
010: import org.eclipse.core.runtime.IProgressMonitor;
011: import org.eclipse.core.runtime.IStatus;
012: import org.eclipse.core.runtime.Status;
013: import org.eclipse.debug.core.ILaunch;
014: import org.eclipse.debug.core.ILaunchConfiguration;
015: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
016: import org.eclipse.jdt.core.IJavaProject;
017: import org.eclipse.jdt.core.JavaCore;
018: import org.eclipse.jdt.launching.IVMInstall;
019: import org.eclipse.jdt.launching.IVMInstallType;
020: import org.eclipse.jdt.launching.JavaRuntime;
021: import org.eclipse.pde.core.plugin.IPluginModelBase;
022: import org.eclipse.pde.internal.core.PDECore;
023: import org.eclipse.pde.internal.core.PluginModelManager;
024: import org.eclipse.pde.internal.core.WorkspaceModelManager;
025: import org.eclipse.pde.ui.launcher.EclipseApplicationLaunchConfiguration;
026: import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
027: import org.terracotta.dso.TcPlugin;
028:
029: import java.util.ArrayList;
030:
031: /**
032: * Launcher for DSO Eclipse applications.
033: */
034:
035: public class DSOEclipseApplicationLaunchConfiguration extends
036: EclipseApplicationLaunchConfiguration implements
037: IDSOLaunchDelegate {
038:
039: private LaunchHelper fLaunchHelper = new LaunchHelper(this );
040:
041: public void launch(ILaunchConfiguration config, String mode,
042: ILaunch launch, IProgressMonitor monitor)
043: throws CoreException {
044: ILaunchConfigurationWorkingCopy wc = fLaunchHelper.setup(
045: config, mode, launch, monitor);
046: if (wc != null) {
047: super .launch(wc, mode, launch, monitor);
048: }
049: }
050:
051: public IJavaProject getJavaProject(
052: ILaunchConfiguration configuration) throws CoreException {
053: String projectName = getJavaProjectName(configuration);
054:
055: if (projectName != null) {
056: projectName = projectName.trim();
057:
058: if (projectName.length() > 0) {
059: IProject project = ResourcesPlugin.getWorkspace()
060: .getRoot().getProject(projectName);
061: IJavaProject javaProject = JavaCore.create(project);
062:
063: if (javaProject != null && javaProject.exists()) {
064: return javaProject;
065: }
066: }
067: }
068:
069: return null;
070: }
071:
072: public String getJavaProjectName(ILaunchConfiguration configuration)
073: throws CoreException {
074: String appNameRoot = configuration.getAttribute(
075: IPDELauncherConstants.APPLICATION, (String) null);
076: if (appNameRoot != null) {
077: appNameRoot = appNameRoot.substring(0, appNameRoot
078: .lastIndexOf('.'));
079: } else {
080: String msg = "No application specified for launch configuration '"
081: + configuration.getName() + "'";
082: Status status = new Status(IStatus.ERROR, TcPlugin
083: .getPluginId(), 1, msg, null);
084: throw new CoreException(status);
085: }
086: PluginModelManager manager = PDECore.getDefault()
087: .getModelManager();
088: IProject[] projs = ResourcesPlugin.getWorkspace().getRoot()
089: .getProjects();
090: for (int i = 0; i < projs.length; i++) {
091: if (!WorkspaceModelManager.isPluginProject(projs[i])) {
092: continue;
093: }
094: IPluginModelBase base = manager.findModel(projs[i]);
095: if (appNameRoot.equals(base.getPluginBase().getId())) {
096: return projs[i].getName();
097: }
098: }
099: String msg = "Unable to determine project for pluginId '"
100: + appNameRoot + "'";
101: Status status = new Status(IStatus.ERROR, TcPlugin
102: .getPluginId(), 1, msg, null);
103: throw new CoreException(status);
104: }
105:
106: public IVMInstall getVMInstall(ILaunchConfiguration configuration)
107: throws CoreException {
108: String vm = configuration.getAttribute(
109: IPDELauncherConstants.VMINSTALL, (String) null);
110: IVMInstall launcher = getVMInstall(vm);
111: if (launcher == null) {
112: String msg = "Cannot locate VMInstall for '" + vm + "'";
113: Status status = new Status(IStatus.ERROR, TcPlugin
114: .getPluginId(), 1, msg, null);
115: throw new CoreException(status);
116: }
117: return launcher;
118: }
119:
120: public static IVMInstall getVMInstall(String name) {
121: if (name != null) {
122: IVMInstall[] installs = getAllVMInstances();
123: for (int i = 0; i < installs.length; i++) {
124: if (installs[i].getName().equals(name))
125: return installs[i];
126: }
127: }
128: return JavaRuntime.getDefaultVMInstall();
129: }
130:
131: public static IVMInstall[] getAllVMInstances() {
132: ArrayList res = new ArrayList();
133: IVMInstallType[] types = JavaRuntime.getVMInstallTypes();
134: for (int i = 0; i < types.length; i++) {
135: IVMInstall[] installs = types[i].getVMInstalls();
136: for (int k = 0; k < installs.length; k++) {
137: res.add(installs[k]);
138: }
139: }
140: return (IVMInstall[]) res.toArray(new IVMInstall[res.size()]);
141: }
142: }
|