You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2023/01/30 19:59:10 UTC

[logging-log4j-tools] branch windows-fixes updated: One more attempt

This is an automated email from the ASF dual-hosted git repository.

vy pushed a commit to branch windows-fixes
in repository https://gitbox.apache.org/repos/asf/logging-log4j-tools.git


The following commit(s) were added to refs/heads/windows-fixes by this push:
     new 69bd9af  One more attempt
69bd9af is described below

commit 69bd9af34e91e707d586f1323519f3bbd974f317
Author: Volkan Yazıcı <vo...@yazi.ci>
AuthorDate: Mon Jan 30 21:00:27 2023 +0100

    One more attempt
---
 .../log4j/changelog/exporter/FreeMarkerUtils.java  | 55 ++++++++++++----------
 1 file changed, 31 insertions(+), 24 deletions(-)

diff --git a/log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/exporter/FreeMarkerUtils.java b/log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/exporter/FreeMarkerUtils.java
index 5311de4..0426818 100644
--- a/log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/exporter/FreeMarkerUtils.java
+++ b/log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/exporter/FreeMarkerUtils.java
@@ -23,37 +23,30 @@ import java.io.UncheckedIOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.StandardOpenOption;
+import java.util.Arrays;
+
+import javax.swing.filechooser.FileSystemView;
 
 import org.apache.logging.log4j.changelog.util.CharsetUtils;
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import freemarker.cache.FileTemplateLoader;
+import freemarker.cache.MultiTemplateLoader;
+import freemarker.cache.TemplateLoader;
 import freemarker.template.*;
 
 final class FreeMarkerUtils {
 
-    private FreeMarkerUtils() {
-    }
-
-    private static Configuration CONFIGURATION;
+    private FreeMarkerUtils() {}
 
-    private synchronized static Configuration getOrCreateConfiguration(final Path path) {
-        return CONFIGURATION == null
-                ? (CONFIGURATION = createConfiguration(path))
-                : CONFIGURATION;
-    }
+    private static final Configuration CONFIGURATION = createConfiguration();
 
-    @SuppressFBWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME")
-    private static Configuration createConfiguration(final Path path) {
+    private static Configuration createConfiguration() {
         final Configuration configuration = new Configuration(Configuration.VERSION_2_3_29);
         configuration.setDefaultEncoding(CharsetUtils.CHARSET_NAME);
         configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
-        Path rootPath = getRootPath(path);
-        try {
-            configuration.setTemplateLoader(new FileTemplateLoader(rootPath.toFile()));
-        } catch (final IOException error) {
-            throw new UncheckedIOException(error);
-        }
+        final TemplateLoader templateLoader = createTemplateLoader();
+        configuration.setTemplateLoader(templateLoader);
         final DefaultObjectWrapperBuilder objectWrapperBuilder =
                 new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_27);
         objectWrapperBuilder.setExposeFields(true);
@@ -65,17 +58,31 @@ final class FreeMarkerUtils {
         return configuration;
     }
 
-    private static Path getRootPath(final Path path) {
-        Path lastParent = path.toAbsolutePath();
-        for (Path parent; (parent = lastParent.getParent()) != null; ) {
-            lastParent = parent;
+    @SuppressFBWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME")
+    private static TemplateLoader createTemplateLoader() {
+        if (File.separatorChar == '/') {
+            return createFileTemplateLoader(new File("/"));
+        }
+        final File[] roots = FileSystemView.getFileSystemView().getRoots();
+        final TemplateLoader[] templateLoaders = Arrays
+                .stream(roots)
+                .map(FreeMarkerUtils::createFileTemplateLoader)
+                .toArray(TemplateLoader[]::new);
+        return new MultiTemplateLoader(templateLoaders);
+    }
+
+    private static FileTemplateLoader createFileTemplateLoader(final File baseDir) {
+        try {
+            return new FileTemplateLoader(baseDir);
+        } catch (final IOException error) {
+            final String message =
+                    String.format("failed creating file template loader for base directory `%s`", baseDir);
+            throw new UncheckedIOException(message, error);
         }
-        return lastParent;
     }
 
     @SuppressFBWarnings("TEMPLATE_INJECTION_FREEMARKER")
     static void render(final Path templateFile, final Object templateData, final Path outputFile) {
-        final Configuration configuration = getOrCreateConfiguration(templateFile);
         // Since we are using `FileTemplateLoader`, template names refer to actual file paths.
         // On Windows, this causes failures; FreeMarker doesn't allow backslashes in the template name.
         // As a workaround, we are replacing backslashes with slashes.
@@ -83,7 +90,7 @@ final class FreeMarkerUtils {
                 ? templateFile.toAbsolutePath().toString()
                 : templateFile.toAbsolutePath().toString().replace('\\', '/');
         try {
-            final Template template = configuration.getTemplate(templateName);
+            final Template template = CONFIGURATION.getTemplate(templateName);
             final Path outputFileParent = outputFile.getParent();
             if (outputFileParent != null) {
                 Files.createDirectories(outputFileParent);