1 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 mode, ILaunch launch, IProgressMonitor monitor) 36 throws CoreException 37 { 38 try { 39 Display.getDefault().syncExec(new Runnable () { 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 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 configProp = " -Dtc.config=\"" + toOSString(configPath) + "\""; 66 67 String portablePath = null; 68 IPath jrePath = JavaRuntime.computeJREEntry(javaProject).getPath(); 69 if(jrePath != null) { 70 portablePath = jrePath.toPortableString(); 71 } 72 73 String jreContainerPath = wc.getAttribute(ATTR_JRE_CONTAINER_PATH, portablePath); 74 String bootJarName = BootJarHelper.getHelper().getBootJarName(jreContainerPath); 75 76 if (bootJarName == null || bootJarName.length() == 0) { 77 IVMInstall vmInstall = getVMInstall(wc); 78 String 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 ("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 bootProp = " -Xbootclasspath/p:\"" + toOSString(bootPath) + "\""; 101 102 if (!configPath.toFile().exists()) { 103 String path = configPath.toOSString(); 104 plugin.openError("Project config file '" + path + "' not found", new RuntimeException ("tc.config not found: " 105 + path)); 106 } 107 108 if (!bootPath.toFile().exists()) { 109 String path = bootPath.toOSString(); 110 plugin.openError("System bootjar '" + path + "' not found", new RuntimeException ("bootjar not found: " + path)); 111 } 112 113 String 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 t) { 124 String 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 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 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 () { 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 |