KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > terracotta > dso > launch > DSOEclipseApplicationLaunchConfiguration


1 /*
2  * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package org.terracotta.dso.launch;
6
7 import org.eclipse.core.resources.IFile;
8 import org.eclipse.core.resources.IProject;
9 import org.eclipse.core.resources.ResourcesPlugin;
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.core.runtime.IPath;
12 import org.eclipse.core.runtime.IProgressMonitor;
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.Status;
15 import org.eclipse.debug.core.ILaunch;
16 import org.eclipse.debug.core.ILaunchConfiguration;
17 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
18 import org.eclipse.jdt.core.IJavaProject;
19 import org.eclipse.jdt.core.JavaCore;
20 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
21 import org.eclipse.jdt.launching.IVMInstall;
22 import org.eclipse.jdt.launching.JavaRuntime;
23 import org.eclipse.pde.core.plugin.IPluginModelBase;
24 import org.eclipse.pde.internal.core.PDECore;
25 import org.eclipse.pde.internal.core.PluginModelManager;
26 import org.eclipse.pde.internal.core.WorkspaceModelManager;
27 import org.eclipse.pde.ui.launcher.EclipseApplicationLaunchConfiguration;
28 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
29 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.ui.IWorkbench;
31 import org.eclipse.ui.PlatformUI;
32 import org.terracotta.dso.BootJarHelper;
33 import org.terracotta.dso.ClasspathProvider;
34 import org.terracotta.dso.ConfigurationHelper;
35 import org.terracotta.dso.ServerTracker;
36 import org.terracotta.dso.TcPlugin;
37 import org.terracotta.dso.actions.BuildBootJarAction;
38
39 public class DSOEclipseApplicationLaunchConfiguration extends EclipseApplicationLaunchConfiguration implements
40     IJavaLaunchConfigurationConstants {
41   public void launch(ILaunchConfiguration config, String JavaDoc mode, ILaunch launch, IProgressMonitor monitor)
42       throws CoreException {
43     try {
44       Display.getDefault().syncExec(new Runnable JavaDoc() {
45         public void run() {
46           IWorkbench workbench = PlatformUI.getWorkbench();
47           if(workbench != null) {
48             workbench.saveAllEditors(false);
49           }
50         }
51       });
52
53       ILaunchConfigurationWorkingCopy wc = config.getWorkingCopy();
54       final IJavaProject javaProject = getJavaProject(wc);
55       final IProject project = javaProject.getProject();
56
57       final TcPlugin plugin = TcPlugin.getDefault();
58       String JavaDoc vmArgs = wc.getAttribute(ATTR_VM_ARGUMENTS, "");
59       IPath libDirPath = plugin.getLibDirPath();
60       IFile configFile = plugin.getConfigurationFile(project);
61
62       if (!plugin.continueWithConfigProblems(project)) { return; }
63
64       final ServerTracker tracker = ServerTracker.getDefault();
65       if (!tracker.anyRunning(javaProject)) {
66         tracker.startServer(javaProject, plugin.getAnyServerName(project));
67       }
68
69       IPath configPath = configFile.getLocation();
70       String JavaDoc configProp = " -Dtc.config=\"" + toOSString(configPath) + "\"";
71       
72       String JavaDoc portablePath = null;
73       IPath jrePath = JavaRuntime.computeJREEntry(javaProject).getPath();
74       if(jrePath != null) {
75         portablePath = jrePath.toPortableString();
76       }
77
78       String JavaDoc jreContainerPath = wc.getAttribute(ATTR_JRE_CONTAINER_PATH, portablePath);
79       String JavaDoc bootJarName = BootJarHelper.getHelper().getBootJarName(jreContainerPath);
80
81       if (bootJarName == null || bootJarName.length() == 0) {
82         IVMInstall vmInstall = getVMInstall(wc);
83         String JavaDoc vmName;
84
85         if (vmInstall != null) {
86           vmName = vmInstall.getName();
87         } else {
88           vmName = jreContainerPath.substring(jreContainerPath.lastIndexOf('/') + 1);
89         }
90
91         throw new RuntimeException JavaDoc("Can't determine BootJar name for runtime '" + vmName + "'");
92       }
93
94       IFile localBootJar = project.getFile(bootJarName);
95       IPath bootPath;
96
97       testEnsureBootJar(plugin, javaProject, localBootJar, jreContainerPath);
98
99       if (localBootJar.exists()) {
100         bootPath = localBootJar.getLocation();
101       } else {
102         bootPath = BootJarHelper.getHelper().getBootJarPath(bootJarName);
103       }
104
105       String JavaDoc bootProp = " -Xbootclasspath/p:\"" + toOSString(bootPath) + "\"";
106
107       if (!configPath.toFile().exists()) {
108         String JavaDoc path = configPath.toOSString();
109         plugin.openError("Project config file '" + path + "' not found", new RuntimeException JavaDoc("tc.config not found: "
110                                                                                               + path));
111       }
112
113       if (!bootPath.toFile().exists()) {
114         String JavaDoc path = bootPath.toOSString();
115         plugin.openError("System bootjar '" + path + "' not found", new RuntimeException JavaDoc("bootjar not found: " + path));
116       }
117
118       String JavaDoc cpProp;
119       if (libDirPath.append("tc.jar").toFile().exists()) {
120         cpProp = " -Dtc.install-root=\"" + toOSString(plugin.getLocation()) + "\"";
121       } else {
122         cpProp = " -Dtc.classpath=\"" + ClasspathProvider.makeDevClasspath() + "\"";
123       }
124
125       wc.setAttribute(ATTR_VM_ARGUMENTS, cpProp + configProp + bootProp + " " + vmArgs);
126
127       super.launch(wc, mode, launch, monitor);
128     } catch (Throwable JavaDoc t) {
129       String JavaDoc msg = "Unable to launch '" + config.getName() + "'\n\n" + t.getLocalizedMessage();
130       Status status = new Status(IStatus.ERROR, TcPlugin.getPluginId(), 1, msg, t);
131       throw new CoreException(status);
132     }
133   }
134
135   private static String JavaDoc toOSString(IPath path) {
136     return path.makeAbsolute().toOSString();
137   }
138
139   private void testEnsureBootJar(final TcPlugin plugin,
140                                  final IJavaProject javaProject,
141                                  final IFile bootJar,
142                                  final String JavaDoc jreContainerPath)
143   {
144     IProject project = javaProject.getProject();
145     ConfigurationHelper configHelper = plugin.getConfigurationHelper(project);
146     IFile configFile = plugin.getConfigurationFile(project);
147     boolean stdBootJarExists = false;
148     boolean configHasBootJarClasses = configHelper.hasBootJarClasses();
149     
150     try {
151       stdBootJarExists = BootJarHelper.getHelper().getBootJarFile().exists();
152     } catch (CoreException ce) {/**/
153     }
154
155     if (!stdBootJarExists || (configFile != null && configHasBootJarClasses)) {
156       long bootStamp = bootJar.getLocalTimeStamp();
157       long confStamp = configFile.getLocalTimeStamp();
158
159       if (!bootJar.exists() || (configHasBootJarClasses && bootStamp < confStamp)) {
160         Display.getDefault().syncExec(new Runnable JavaDoc() {
161           public void run() {
162             BuildBootJarAction bbja = new BuildBootJarAction(javaProject);
163             bbja.setJREContainerPath(jreContainerPath);
164             bbja.run(null);
165           }
166         });
167       }
168     }
169   }
170
171   public IJavaProject getJavaProject(ILaunchConfiguration configuration) throws CoreException {
172     String JavaDoc projectName = getJavaProjectName(configuration);
173     
174     if (projectName != null) {
175       projectName = projectName.trim();
176       
177       if (projectName.length() > 0) {
178         IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
179         IJavaProject javaProject = JavaCore.create(project);
180         
181         if (javaProject != null && javaProject.exists()) {
182           return javaProject;
183         }
184       }
185     }
186     
187     return null;
188   }
189
190   public String JavaDoc getJavaProjectName(ILaunchConfiguration configuration) throws CoreException {
191     String JavaDoc appNameRoot = configuration.getAttribute(IPDELauncherConstants.APPLICATION, (String JavaDoc)null);
192     if(appNameRoot != null) {
193       appNameRoot = appNameRoot.substring(0, appNameRoot.lastIndexOf('.'));
194     } else {
195       String JavaDoc msg = "No application specified for launch configuration '"+configuration.getName()+"'";
196       Status status = new Status(IStatus.ERROR, TcPlugin.getPluginId(), 1, msg, null);
197       throw new CoreException(status);
198     }
199     PluginModelManager manager = PDECore.getDefault().getModelManager();
200     IProject[] projs = ResourcesPlugin.getWorkspace().getRoot().getProjects();
201     for (int i = 0; i < projs.length; i++) {
202       if (!WorkspaceModelManager.isPluginProject(projs[i])) {
203         continue;
204       }
205       IPluginModelBase base = manager.findModel(projs[i]);
206       if(appNameRoot.equals(base.getPluginBase().getId())) {
207         return projs[i].getName();
208       }
209     }
210     String JavaDoc msg = "Unable to determine project for pluginId '"+appNameRoot+"'";
211     Status status = new Status(IStatus.ERROR, TcPlugin.getPluginId(), 1, msg, null);
212     throw new CoreException(status);
213   }
214
215   public IVMInstall getVMInstall(ILaunchConfiguration configuration) throws CoreException {
216     return JavaRuntime.computeVMInstall(configuration);
217   }
218 }
219
Popular Tags