From 55f68989d8d2cf6f6143b858cee3e2bfb9f0f537 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 21 Feb 2022 14:01:04 +0000 Subject: [PATCH] Remove redundant action to make plugins dat file reproducible Closes gh-29917 --- .../ReproducibleLog4j2PluginsDatAction.java | 111 ------------------ .../ReproduciblePluginsDatActionTests.java | 80 ------------- spring-boot-project/spring-boot/build.gradle | 4 - 3 files changed, 195 deletions(-) delete mode 100644 buildSrc/src/main/java/org/springframework/boot/build/log4j2/ReproducibleLog4j2PluginsDatAction.java delete mode 100644 buildSrc/src/test/java/org/springframework/boot/build/log4j2/ReproduciblePluginsDatActionTests.java diff --git a/buildSrc/src/main/java/org/springframework/boot/build/log4j2/ReproducibleLog4j2PluginsDatAction.java b/buildSrc/src/main/java/org/springframework/boot/build/log4j2/ReproducibleLog4j2PluginsDatAction.java deleted file mode 100644 index a22939d626d..00000000000 --- a/buildSrc/src/main/java/org/springframework/boot/build/log4j2/ReproducibleLog4j2PluginsDatAction.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright 2012-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.build.log4j2; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.Map; -import java.util.Map.Entry; -import java.util.TreeMap; - -import org.gradle.api.Action; -import org.gradle.api.InvalidUserDataException; -import org.gradle.api.tasks.TaskExecutionException; -import org.gradle.api.tasks.compile.JavaCompile; - -/** - * An {@link Action} to post-process a {@code Log4j2Plugins.dat} and re-order its content - * so that it is reproducible. - * - * @author Andy Wilkinson - */ -public class ReproducibleLog4j2PluginsDatAction implements Action { - - @Override - public void execute(JavaCompile javaCompile) { - File datFile = new File(javaCompile.getDestinationDir(), - "META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat"); - try { - postProcess(datFile); - } - catch (IOException ex) { - throw new TaskExecutionException(javaCompile, ex); - } - } - - void postProcess(File datFile) throws IOException { - if (!datFile.isFile()) { - throw new InvalidUserDataException( - "META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat not found"); - } - Map> categories = new TreeMap<>(); - try (DataInputStream input = new DataInputStream(new FileInputStream(datFile))) { - int categoryCount = input.readInt(); - for (int i = 0; i < categoryCount; i++) { - String categoryName = input.readUTF(); - int pluginCount = input.readInt(); - Map category = categories.computeIfAbsent(categoryName, (c) -> new TreeMap<>()); - for (int j = 0; j < pluginCount; j++) { - Plugin plugin = new Plugin(input.readUTF(), input.readUTF(), input.readUTF(), input.readBoolean(), - input.readBoolean()); - category.putIfAbsent(plugin.key, plugin); - } - } - } - try (DataOutputStream output = new DataOutputStream(new FileOutputStream(datFile))) { - output.writeInt(categories.size()); - for (Entry> category : categories.entrySet()) { - output.writeUTF(category.getKey()); - output.writeInt(category.getValue().size()); - for (Plugin plugin : category.getValue().values()) { - output.writeUTF(plugin.key); - output.writeUTF(plugin.className); - output.writeUTF(plugin.name); - output.writeBoolean(plugin.printable); - output.writeBoolean(plugin.defer); - } - } - } - } - - private static final class Plugin { - - private final String key; - - private final String className; - - private final String name; - - private final boolean printable; - - private final boolean defer; - - private Plugin(String key, String className, String name, boolean printable, boolean defer) { - this.key = key; - this.className = className; - this.name = name; - this.printable = printable; - this.defer = defer; - } - - } - -} diff --git a/buildSrc/src/test/java/org/springframework/boot/build/log4j2/ReproduciblePluginsDatActionTests.java b/buildSrc/src/test/java/org/springframework/boot/build/log4j2/ReproduciblePluginsDatActionTests.java deleted file mode 100644 index 1899dd7ab31..00000000000 --- a/buildSrc/src/test/java/org/springframework/boot/build/log4j2/ReproduciblePluginsDatActionTests.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2012-2021 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.build.log4j2; - -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.Vector; - -import org.apache.logging.log4j.core.config.plugins.processor.PluginCache; -import org.apache.logging.log4j.core.config.plugins.processor.PluginEntry; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * Tests for {@link ReproducibleLog4j2PluginsDatAction} - * - * @author Andy Wilkinson - */ -class ReproduciblePluginsDatActionTests { - - @Test - void postProcessingOrdersCategoriesAndPlugins() throws IOException { - Path datFile = Files.createTempFile("Log4j2Plugins", "dat"); - try { - write(datFile); - PluginCache cache = new PluginCache(); - cache.loadCacheFiles(new Vector<>(Arrays.asList(datFile.toUri().toURL())).elements()); - assertThat(cache.getAllCategories().keySet()).containsExactly("one", "two"); - assertThat(cache.getCategory("one").keySet()).containsExactly("alpha", "bravo", "charlie"); - assertThat(cache.getCategory("two").keySet()).containsExactly("delta", "echo", "foxtrot"); - } - finally { - Files.delete(datFile); - } - } - - private void write(Path datFile) throws IOException { - PluginCache cache = new PluginCache(); - createCategory(cache, "two", Arrays.asList("delta", "foxtrot", "echo")); - createCategory(cache, "one", Arrays.asList("bravo", "alpha", "charlie")); - try (OutputStream output = new FileOutputStream(datFile.toFile())) { - cache.writeCache(output); - new ReproducibleLog4j2PluginsDatAction().postProcess(datFile.toFile()); - } - } - - private void createCategory(PluginCache cache, String categoryName, List entryNames) { - Map category = cache.getCategory(categoryName); - for (String entryName : entryNames) { - PluginEntry entry = new PluginEntry(); - entry.setKey(entryName); - entry.setClassName("com.example.Plugin"); - entry.setName("name"); - entry.setCategory(categoryName); - category.put(entryName, entry); - } - } - -} diff --git a/spring-boot-project/spring-boot/build.gradle b/spring-boot-project/spring-boot/build.gradle index 246b24ae2f0..c5d8a655364 100644 --- a/spring-boot-project/spring-boot/build.gradle +++ b/spring-boot-project/spring-boot/build.gradle @@ -173,10 +173,6 @@ compileTestKotlin { } } -compileJava { - doLast new org.springframework.boot.build.log4j2.ReproducibleLog4j2PluginsDatAction() -} - toolchain { testJvmArgs.add("--add-opens=java.base/java.net=ALL-UNNAMED") }