KickJava   Java API By Example, From Geeks To Geeks.

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


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.runtime.CoreException;
10 import org.eclipse.core.runtime.IPath;
11 import org.eclipse.core.runtime.IProgressMonitor;
12 import org.eclipse.core.runtime.IStatus;
13 import org.eclipse.core.runtime.Status;
14 import org.eclipse.debug.core.ILaunch;
15 import org.eclipse.debug.core.ILaunchConfiguration;
16 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
17 import org.eclipse.jdt.core.IJavaProject;
18 import org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfiguration;
19 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
20 import org.eclipse.jdt.launching.IVMInstall;
21 import org.eclipse.jdt.launching.JavaRuntime;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.ui.IWorkbench;
24 import org.eclipse.ui.PlatformUI;
25 import org.terracotta.dso.BootJarHelper;
26 import org.terracotta.dso.ClasspathProvider;
27 import org.terracotta.dso.ConfigurationHelper;
28 import org.terracotta.dso.ServerTracker;
29 import org.terracotta.dso.TcPlugin;
30 import org.terracotta.dso.actions.BuildBootJarAction;
31
32 public class DSOJUnitLaunchConfiguration extends JUnitLaunchConfiguration
33   implements IJavaLaunchConfigurationConstants
34 {
35   public void launch(ILaunchConfiguration config, String JavaDoc mode, ILaunch launch, IProgressMonitor monitor)
36     throws CoreException
37   {
38     try {
39       Display.getDefault().syncExec(new Runnable JavaDoc() {
40         public void run() {
41           IWorkbench workbench = PlatformUI.getWorkbench();
42           if(workbench != null) {
43             workbench.saveAllEditors(false);
44           }
45         }
46       });
47
48       ILaunchConfigurationWorkingCopy wc = config.getWorkingCopy();
49       final IJavaProject javaProject = getJavaProject(wc);
50       final IProject project = javaProject.getProject();
51
52       final TcPlugin plugin = TcPlugin.getDefault();
53       String JavaDoc vmArgs = wc.getAttribute(ATTR_VM_ARGUMENTS, "");
54       IPath libDirPath = plugin.getLibDirPath();
55       IFile configFile = plugin.getConfigurationFile(project);
56
57       if (!plugin.continueWithConfigProblems(project)) { return; }
58
59       final ServerTracker tracker = ServerTracker.getDefault();
60       if (!tracker.anyRunning(javaProject)) {
61         tracker.startServer(javaProject, plugin.getAnyServerName(project));
62       }
63
64       IPath configPath = configFile.getLocation();
65       String JavaDoc configProp = " -Dtc.config=\"" + toOSString(configPath) + "\"";
66       
67       String JavaDoc portablePath = null;
68       IPath jrePath = JavaRuntime.computeJREEntry(javaProject).getPath();
69       if(jrePath != null) {
70         portablePath = jrePath.toPortableString();
71       }
72
73       String JavaDoc jreContainerPath = wc.getAttribute(ATTR_JRE_CONTAINER_PATH, portablePath);
74       String JavaDoc bootJarName = BootJarHelper.getHelper().getBootJarName(jreContainerPath);
75
76       if (bootJarName == null || bootJarName.length() == 0) {
77         IVMInstall vmInstall = getVMInstall(wc);
78         String JavaDoc vmName;
79
80         if (vmInstall != null) {
81           vmName = vmInstall.getName();
82         } else {
83           vmName = jreContainerPath.substring(jreContainerPath.lastIndexOf('/') + 1);
84         }
85
86         throw new RuntimeException JavaDoc("Can't determine BootJar name for runtime '" + vmName + "'");
87       }
88
89       IFile localBootJar = project.getFile(bootJarName);
90       IPath bootPath;
91
92       testEnsureBootJar(plugin, javaProject, localBootJar, jreContainerPath);
93
94       if (localBootJar.exists()) {
95         bootPath = localBootJar.getLocation();
96       } else {
97         bootPath = BootJarHelper.getHelper().getBootJarPath(bootJarName);
98       }
99
100       String JavaDoc bootProp = " -Xbootclasspath/p:\"" + toOSString(bootPath) + "\"";
101
102       if (!configPath.toFile().exists()) {
103         String JavaDoc path = configPath.toOSString();
104         plugin.openError("Project config file '" + path + "' not found", new RuntimeException JavaDoc("tc.config not found: "
105                                                                                               + path));
106       }
107
108       if (!bootPath.toFile().exists()) {
109         String JavaDoc path = bootPath.toOSString();
110         plugin.openError("System bootjar '" + path + "' not found", new RuntimeException JavaDoc("bootjar not found: " + path));
111       }
112
113       String JavaDoc cpProp;
114       if (libDirPath.append("tc.jar").toFile().exists()) {
115         cpProp = " -Dtc.install-root=\"" + toOSString(plugin.getLocation()) + "\"";
116       } else {
117         cpProp = " -Dtc.classpath=\"" + ClasspathProvider.makeDevClasspath() + "\"";
118       }
119
120       wc.setAttribute(ATTR_VM_ARGUMENTS, cpProp + configProp + bootProp + " " + vmArgs);
121
122       super.launch(wc, mode, launch, monitor);
123     } catch (Throwable JavaDoc t) {
124       String JavaDoc msg = "Unable to launch '" + config.getName() + "'\n\n" + t.getLocalizedMessage();
125       Status status = new Status(IStatus.ERROR, TcPlugin.getPluginId(), 1, msg, t);
126       throw new CoreException(status);
127     }
128   }
129
130   private static String JavaDoc toOSString(IPath path) {
131     return path.makeAbsolute().toOSString();
132   }
133
134   private void testEnsureBootJar(final TcPlugin plugin,
135                                  final IJavaProject javaProject,
136                                  final IFile bootJar,
137                                  final String JavaDoc jreContainerPath)
138    {
139      IProject project = javaProject.getProject();
140      ConfigurationHelper configHelper = plugin.getConfigurationHelper(project);
141      IFile configFile = plugin.getConfigurationFile(project);
142      boolean stdBootJarExists = false;
143      boolean configHasBootJarClasses = configHelper.hasBootJarClasses();
144      
145      try {
146        stdBootJarExists = BootJarHelper.getHelper().getBootJarFile().exists();
147      } catch(CoreException ce) {/**/}
148                      
149      if(!stdBootJarExists || (configFile != null && configHasBootJarClasses)) {
150        long bootStamp = bootJar.getLocalTimeStamp();
151        long confStamp = configFile.getLocalTimeStamp();
152        
153        if(!bootJar.exists() || (configHasBootJarClasses && bootStamp < confStamp)) {
154          Display.getDefault().syncExec(new Runnable JavaDoc() {
155            public void run() {
156              BuildBootJarAction bbja = new BuildBootJarAction(javaProject);
157              bbja.setJREContainerPath(jreContainerPath);
158              bbja.run(null);
159            }
160          });
161        }
162      }
163    }
164 }
165
Popular Tags