|
| 1 | +package net.openhft.affinity.common; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | + |
| 6 | +import java.io.ByteArrayOutputStream; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.lang.management.ManagementFactory; |
| 10 | +import java.lang.management.RuntimeMXBean; |
| 11 | +import java.nio.charset.Charset; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.nio.file.Paths; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.List; |
| 17 | +import java.util.stream.Collectors; |
| 18 | + |
| 19 | +public class ProcessRunner { |
| 20 | + |
| 21 | + private static final Logger LOGGER = LoggerFactory.getLogger(ProcessRunner.class); |
| 22 | + |
| 23 | + /** |
| 24 | + * Spawn a process running the main method of a specified class |
| 25 | + * |
| 26 | + * @param clazz The class to execute |
| 27 | + * @param args Any arguments to pass to the process |
| 28 | + * @return the Process spawned |
| 29 | + * @throws IOException if there is an error starting the process |
| 30 | + */ |
| 31 | + public static Process runClass(Class<?> clazz, String... args) throws IOException { |
| 32 | + // Because Java17 must be run using various module flags, these must be propagated |
| 33 | + // to the child processes |
| 34 | + // https://stackoverflow.com/questions/1490869/how-to-get-vm-arguments-from-inside-of-java-application |
| 35 | + final RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean(); |
| 36 | + |
| 37 | + // filter out javaagent params, or this confuses the IntelliJ debugger |
| 38 | + final List<String> jvmArgsWithoutJavaAgents = runtimeMxBean.getInputArguments().stream() |
| 39 | + .filter(arg -> !arg.startsWith("-javaagent:")) |
| 40 | + .filter(arg -> !arg.startsWith("-agentlib:")) |
| 41 | + .collect(Collectors.toList()); |
| 42 | + |
| 43 | + String classPath = System.getProperty("java.class.path"); |
| 44 | + String className = clazz.getName(); |
| 45 | + String javaBin = findJavaBinPath().toString(); |
| 46 | + List<String> allArgs = new ArrayList<>(); |
| 47 | + allArgs.add(javaBin); |
| 48 | + allArgs.addAll(jvmArgsWithoutJavaAgents); |
| 49 | + allArgs.add("-cp"); |
| 50 | + allArgs.add(classPath); |
| 51 | + allArgs.add(className); |
| 52 | + allArgs.addAll(Arrays.asList(args)); |
| 53 | + ProcessBuilder processBuilder = new ProcessBuilder(allArgs.toArray(new String[]{})); |
| 54 | +// processBuilder.inheritIO(); // this doesn't place nice with surefire |
| 55 | + return processBuilder.start(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Log stdout and stderr for a process |
| 60 | + * <p> |
| 61 | + * ProcessBuilder.inheritIO() didn't play nicely with Maven failsafe plugin |
| 62 | + * <p> |
| 63 | + * https://maven.apache.org/surefire/maven-failsafe-plugin/faq.html#corruptedstream |
| 64 | + */ |
| 65 | + public static void printProcessOutput(String processName, Process process) { |
| 66 | + LOGGER.info("\n" |
| 67 | + + "Output for " + processName + "\n" |
| 68 | + + "stdout:\n" |
| 69 | + + copyStreamToString(process.getInputStream()) + "\n" |
| 70 | + + "stderr:\n" |
| 71 | + + copyStreamToString(process.getErrorStream())); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Copies a stream to a string, up to the point where reading more would block |
| 76 | + * |
| 77 | + * @param inputStream The stream to read from |
| 78 | + * @return The output as a string |
| 79 | + */ |
| 80 | + private static String copyStreamToString(InputStream inputStream) { |
| 81 | + ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 82 | + byte[] buffer = new byte[1024]; |
| 83 | + int read; |
| 84 | + try { |
| 85 | + while (inputStream.available() > 0 && (read = inputStream.read(buffer)) >= 0) { |
| 86 | + os.write(buffer, 0, read); |
| 87 | + } |
| 88 | + } catch (IOException e) { |
| 89 | + // Ignore |
| 90 | + } |
| 91 | + return new String(os.toByteArray(), Charset.defaultCharset()); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Try and work out what the java executable is cross platform |
| 96 | + * |
| 97 | + * @return the Path to the java executable |
| 98 | + * @throws IllegalStateException if the executable couldn't be located |
| 99 | + */ |
| 100 | + private static Path findJavaBinPath() { |
| 101 | + final Path javaBinPath = Paths.get(System.getProperty("java.home")).resolve("bin"); |
| 102 | + final Path linuxJavaExecutable = javaBinPath.resolve("java"); |
| 103 | + if (linuxJavaExecutable.toFile().exists()) { |
| 104 | + return linuxJavaExecutable; |
| 105 | + } else { |
| 106 | + Path windowsJavaExecutable = javaBinPath.resolve("java.exe"); |
| 107 | + if (windowsJavaExecutable.toFile().exists()) { |
| 108 | + return windowsJavaExecutable; |
| 109 | + } |
| 110 | + } |
| 111 | + throw new IllegalStateException("Couldn't locate java executable!"); |
| 112 | + } |
| 113 | +} |
0 commit comments