|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Broadcom, Inc. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Broadcom, Inc. - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | +package org.springframework.ide.eclipse.boot.launch; |
| 12 | + |
| 13 | +import java.io.File; |
| 14 | +import java.io.IOException; |
| 15 | +import java.nio.file.Files; |
| 16 | +import java.nio.file.Path; |
| 17 | +import java.nio.file.Paths; |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Optional; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.UUID; |
| 26 | +import java.util.concurrent.CompletableFuture; |
| 27 | +import java.util.concurrent.ConcurrentHashMap; |
| 28 | +import java.util.concurrent.ExecutionException; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | +import java.util.concurrent.TimeoutException; |
| 31 | +import java.util.regex.Pattern; |
| 32 | + |
| 33 | +import org.eclipse.core.runtime.CoreException; |
| 34 | +import org.eclipse.debug.core.ILaunch; |
| 35 | +import org.eclipse.debug.core.ILaunchConfiguration; |
| 36 | +import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; |
| 37 | +import org.eclipse.debug.core.ILaunchDelegate; |
| 38 | +import org.eclipse.debug.core.ILaunchManager; |
| 39 | +import org.eclipse.debug.core.ILaunchesListener2; |
| 40 | +import org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationConstants; |
| 41 | +import org.eclipse.jdt.junit.launcher.JUnitLaunchConfigurationDelegate; |
| 42 | +import org.springsource.ide.eclipse.commons.livexp.util.Log; |
| 43 | + |
| 44 | +import com.google.gson.reflect.TypeToken; |
| 45 | + |
| 46 | +@SuppressWarnings("restriction") |
| 47 | +public class TestJarLaunchListener implements ILaunchesListener2 { |
| 48 | + |
| 49 | + private static TestJarLaunchListener instance; |
| 50 | + |
| 51 | + private static final Pattern TESTJAR_PATTERN = Pattern.compile("^spring-boot-testjars-\\d+\\.\\d+\\.\\d+(.*)?.jar$"); |
| 52 | + |
| 53 | + private static final String TESTJAR_ARTIFACTS = "spring.boot.test-jar-artifacts"; |
| 54 | + |
| 55 | + public record ExecutableProject(String name, String uri, String gav, String mainClass, Collection<String> classpath) {} |
| 56 | + |
| 57 | + public static TestJarLaunchListener getSingletonInstance() { |
| 58 | + if (instance == null) { |
| 59 | + instance = new TestJarLaunchListener(); |
| 60 | + } |
| 61 | + return instance; |
| 62 | + } |
| 63 | + |
| 64 | + public void launchRemoved(ILaunch launch) { |
| 65 | + clearTestJarWorkspaceProjectFiles(launch.getLaunchConfiguration()); |
| 66 | + } |
| 67 | + |
| 68 | + private void clearTestJarWorkspaceProjectFiles(ILaunchConfiguration configuration) { |
| 69 | + try { |
| 70 | + if (JUnitLaunchConfigurationConstants.ID_JUNIT_APPLICATION.equals(configuration.getType().getIdentifier())) { |
| 71 | + Map<String, String> oldTestJarArtifacts = configuration.getAttribute(TESTJAR_ARTIFACTS, Collections.emptyMap()); |
| 72 | + Map<String, String> env = new HashMap<>(configuration.getAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, Collections.emptyMap())); |
| 73 | + for (Map.Entry<String, String> testJarArtifactEnvEntry : oldTestJarArtifacts.entrySet()) { |
| 74 | + env.remove(testJarArtifactEnvEntry.getKey()); |
| 75 | + try { |
| 76 | + Files.deleteIfExists(Paths.get(testJarArtifactEnvEntry.getValue())); |
| 77 | + } catch (IOException e) { |
| 78 | + Log.log(e); |
| 79 | + } |
| 80 | + } |
| 81 | + ILaunchConfigurationWorkingCopy wc = configuration.getWorkingCopy(); |
| 82 | + wc.removeAttribute(TESTJAR_ARTIFACTS); |
| 83 | + if (env.isEmpty()) { |
| 84 | + wc.removeAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES); |
| 85 | + } else { |
| 86 | + wc.setAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, env); |
| 87 | + } |
| 88 | + wc.doSave(); |
| 89 | + } |
| 90 | + } catch (CoreException e) { |
| 91 | + Log.log(e); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private void setupTestJarWorkspaceProjectFiles(ILaunchConfiguration configuration, Map<String, String> testJarArtifactsEnvMap) { |
| 96 | + try { |
| 97 | + Map<String, String> originalEnv = new HashMap<>(configuration.getAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, Collections.emptyMap())); |
| 98 | + Map<String, String> oldTestJarArtifacts = configuration.getAttribute(TESTJAR_ARTIFACTS, Collections.emptyMap()); |
| 99 | + for (Map.Entry<String, String> envEntry : oldTestJarArtifacts.entrySet()) { |
| 100 | + try { |
| 101 | + Files.deleteIfExists(Paths.get(envEntry.getValue())); |
| 102 | + } catch (IOException e) { |
| 103 | + Log.log(e); |
| 104 | + } |
| 105 | + originalEnv.remove(envEntry.getKey()); |
| 106 | + } |
| 107 | + for (String envVar : originalEnv.keySet()) { |
| 108 | + testJarArtifactsEnvMap.remove(envVar); |
| 109 | + } |
| 110 | + originalEnv.putAll(testJarArtifactsEnvMap); |
| 111 | + ILaunchConfigurationWorkingCopy wc = configuration.getWorkingCopy(); |
| 112 | + wc.setAttribute(TESTJAR_ARTIFACTS, testJarArtifactsEnvMap); |
| 113 | + wc.setAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, originalEnv); |
| 114 | + wc.doSave(); |
| 115 | + } catch (CoreException e) { |
| 116 | + e.printStackTrace(); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public void launchAdded(ILaunch launch) { |
| 121 | + try { |
| 122 | + ILaunchConfiguration configuration = launch.getLaunchConfiguration(); |
| 123 | + if (JUnitLaunchConfigurationConstants.ID_JUNIT_APPLICATION.equals(configuration.getType().getIdentifier())) { |
| 124 | + ILaunchDelegate[] delegates = configuration.getType().getDelegates(Set.of(launch.getLaunchMode())); |
| 125 | + if (delegates.length > 0) { |
| 126 | + JUnitLaunchConfigurationDelegate delegate = (JUnitLaunchConfigurationDelegate) delegates[0].getDelegate(); |
| 127 | + String[] classpath = delegate.getClasspathAndModulepath(configuration)[0]; |
| 128 | + if (isTestJarsOnClasspath(classpath)) { |
| 129 | + try { |
| 130 | + getTestJarArtifactsMap() |
| 131 | + .thenAccept(testJarArtifactsEnvMap -> setupTestJarWorkspaceProjectFiles(configuration, testJarArtifactsEnvMap)) |
| 132 | + .get(3000000, TimeUnit.SECONDS); |
| 133 | + } catch (Exception e) { |
| 134 | + Log.log(e); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } catch (CoreException e) { |
| 140 | + e.printStackTrace(); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private static String createTestJarArtifactEnvKey(String gav) { |
| 145 | + return "TESTJARS_ARTIFACT_%s".formatted(gav.replace(":", "_")); |
| 146 | + } |
| 147 | + |
| 148 | + private static boolean isTestJarsOnClasspath(String[] classpath) { |
| 149 | + for (String cpe : classpath) { |
| 150 | + Path p = Paths.get(cpe); |
| 151 | + if (Files.isRegularFile(p) && TESTJAR_PATTERN.matcher(p.getFileName().toString()).matches()) { |
| 152 | + return true; |
| 153 | + } |
| 154 | + } |
| 155 | + return false; |
| 156 | + } |
| 157 | + |
| 158 | + private CompletableFuture<Map<String, String>> getTestJarArtifactsMap() { |
| 159 | + try { |
| 160 | + Optional<?> opt = BootLsCommandUtils.executeCommand(TypeToken.getParameterized(List.class, ExecutableProject.class), "sts/spring-boot/executableBootProjects").get(3, TimeUnit.SECONDS); |
| 161 | + if (opt.isPresent() && opt.get() instanceof List<?> projects) { |
| 162 | + Map<String, String> gavToFile = new ConcurrentHashMap<>(); |
| 163 | + CompletableFuture<?>[] futures = projects.stream().map(ExecutableProject.class::cast).map(p -> CompletableFuture.runAsync(() -> { |
| 164 | + try { |
| 165 | + Path file = Files.createTempFile("%s_".formatted(p.gav().replace(":", "_")), UUID.randomUUID().toString()); |
| 166 | + Files.write(file, List.of( |
| 167 | + "# the main class to invoke", |
| 168 | + "main=%s".formatted(p.mainClass()), |
| 169 | + "# the classpath to use delimited by the OS specific delimiters", |
| 170 | + "classpath=%s".formatted(String.join(File.pathSeparator, p.classpath()) |
| 171 | + ))); |
| 172 | + gavToFile.put(createTestJarArtifactEnvKey(p.gav()), file.toFile().toString()); |
| 173 | + } catch (IOException e) { |
| 174 | + Log.log(e); |
| 175 | + } |
| 176 | + })).toArray(CompletableFuture[]::new); |
| 177 | + return CompletableFuture.allOf(futures).thenApply(v -> gavToFile); |
| 178 | + } |
| 179 | + } catch (InterruptedException | ExecutionException | TimeoutException e) { |
| 180 | + Log.log(e); |
| 181 | + } |
| 182 | + |
| 183 | + return CompletableFuture.completedFuture(Collections.emptyMap()); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void launchesRemoved(ILaunch[] launches) { |
| 188 | + // nothing to do |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public void launchesAdded(ILaunch[] launches) { |
| 193 | + for (ILaunch l : launches) { |
| 194 | + launchAdded(l); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public void launchesChanged(ILaunch[] launches) { |
| 200 | + // nothing to do |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public void launchesTerminated(ILaunch[] launches) { |
| 205 | + for (ILaunch l : launches) { |
| 206 | + clearTestJarWorkspaceProjectFiles(l.getLaunchConfiguration()); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments