|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.build.docs; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.stream.Collectors; |
| 26 | + |
| 27 | +import org.gradle.api.DefaultTask; |
| 28 | +import org.gradle.api.Task; |
| 29 | +import org.gradle.api.file.FileCollection; |
| 30 | +import org.gradle.api.file.RegularFileProperty; |
| 31 | +import org.gradle.api.provider.ListProperty; |
| 32 | +import org.gradle.api.provider.Property; |
| 33 | +import org.gradle.api.tasks.Classpath; |
| 34 | +import org.gradle.api.tasks.Input; |
| 35 | +import org.gradle.api.tasks.OutputFile; |
| 36 | +import org.gradle.api.tasks.TaskAction; |
| 37 | +import org.gradle.internal.jvm.Jvm; |
| 38 | + |
| 39 | +/** |
| 40 | + * {@link Task} to run an application for the purpose of capturing its output for |
| 41 | + * inclusion in the reference documentation. |
| 42 | + * |
| 43 | + * @author Andy Wilkinson |
| 44 | + */ |
| 45 | +public class ApplicationRunner extends DefaultTask { |
| 46 | + |
| 47 | + private final RegularFileProperty output = getProject().getObjects().fileProperty(); |
| 48 | + |
| 49 | + private final ListProperty<String> args = getProject().getObjects().listProperty(String.class); |
| 50 | + |
| 51 | + private final Property<String> mainClass = getProject().getObjects().property(String.class); |
| 52 | + |
| 53 | + private final Property<String> expectedLogging = getProject().getObjects().property(String.class); |
| 54 | + |
| 55 | + private FileCollection classpath; |
| 56 | + |
| 57 | + @OutputFile |
| 58 | + public RegularFileProperty getOutput() { |
| 59 | + return this.output; |
| 60 | + } |
| 61 | + |
| 62 | + @Classpath |
| 63 | + public FileCollection getClasspath() { |
| 64 | + return this.classpath; |
| 65 | + } |
| 66 | + |
| 67 | + public void setClasspath(FileCollection classpath) { |
| 68 | + this.classpath = classpath; |
| 69 | + } |
| 70 | + |
| 71 | + @Input |
| 72 | + public ListProperty<String> getArgs() { |
| 73 | + return this.args; |
| 74 | + } |
| 75 | + |
| 76 | + @Input |
| 77 | + public Property<String> getMainClass() { |
| 78 | + return this.mainClass; |
| 79 | + } |
| 80 | + |
| 81 | + @Input |
| 82 | + public Property<String> getExpectedLogging() { |
| 83 | + return this.expectedLogging; |
| 84 | + } |
| 85 | + |
| 86 | + @TaskAction |
| 87 | + void runApplication() throws IOException { |
| 88 | + List<String> command = new ArrayList<>(); |
| 89 | + File executable = Jvm.current().getExecutable("java"); |
| 90 | + command.add(executable.getAbsolutePath()); |
| 91 | + command.add("-cp"); |
| 92 | + command.add(this.classpath.getFiles().stream().map(File::getAbsolutePath) |
| 93 | + .collect(Collectors.joining(File.pathSeparator))); |
| 94 | + command.add(this.mainClass.get()); |
| 95 | + command.addAll(this.args.get()); |
| 96 | + File outputFile = this.output.getAsFile().get(); |
| 97 | + Process process = new ProcessBuilder().redirectOutput(outputFile).redirectError(outputFile).command(command) |
| 98 | + .start(); |
| 99 | + awaitLogging(process); |
| 100 | + process.destroy(); |
| 101 | + } |
| 102 | + |
| 103 | + private void awaitLogging(Process process) { |
| 104 | + long end = System.currentTimeMillis() + 30000; |
| 105 | + String expectedLogging = this.expectedLogging.get(); |
| 106 | + while (System.currentTimeMillis() < end) { |
| 107 | + for (String line : outputLines()) { |
| 108 | + if (line.contains(expectedLogging)) { |
| 109 | + return; |
| 110 | + } |
| 111 | + } |
| 112 | + if (!process.isAlive()) { |
| 113 | + throw new IllegalStateException("Process exited before '" + expectedLogging + "' was logged"); |
| 114 | + } |
| 115 | + } |
| 116 | + throw new IllegalStateException("'" + expectedLogging + "' was not logged within 30 seconds"); |
| 117 | + } |
| 118 | + |
| 119 | + private List<String> outputLines() { |
| 120 | + Path outputPath = this.output.get().getAsFile().toPath(); |
| 121 | + try { |
| 122 | + return Files.readAllLines(outputPath); |
| 123 | + } |
| 124 | + catch (IOException ex) { |
| 125 | + throw new RuntimeException("Failed to read lines of output from '" + outputPath + "'", ex); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments