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:39:05 UTC

[logging-log4j-tools] branch windows-fixes updated: Try to fix FreeMarker path handling on Windows

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 c9787da  Try to fix FreeMarker path handling on Windows
c9787da is described below

commit c9787da136551bfcab048d5b3bafc10816b6b85b
Author: Volkan Yazıcı <vo...@yazi.ci>
AuthorDate: Mon Jan 30 20:40:21 2023 +0100

    Try to fix FreeMarker path handling on Windows
---
 .../log4j/changelog/exporter/FreeMarkerUtils.java  | 28 +++++++++++++++++-----
 1 file changed, 22 insertions(+), 6 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 5ed4c2c..367c49d 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
@@ -24,25 +24,33 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.StandardOpenOption;
 
+import freemarker.cache.FileTemplateLoader;
+
 import org.apache.logging.log4j.changelog.util.CharsetUtils;
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-import freemarker.cache.FileTemplateLoader;
 import freemarker.template.*;
 
 final class FreeMarkerUtils {
 
     private FreeMarkerUtils() {}
 
-    private static final Configuration CONFIGURATION = createConfiguration();
+    private static Configuration CONFIGURATION;
+
+    private synchronized static Configuration getOrCreateConfiguration(final Path path) {
+        return CONFIGURATION == null
+                ? (CONFIGURATION = createConfiguration(path))
+                : CONFIGURATION;
+    }
 
     @SuppressFBWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME")
-    private static Configuration createConfiguration() {
+    private static Configuration createConfiguration(final Path path) {
         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(new File("/")));
+            configuration.setTemplateLoader(new FileTemplateLoader(rootPath.toFile()));
         } catch (final IOException error) {
             throw new UncheckedIOException(error);
         }
@@ -57,9 +65,17 @@ 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;
+        }
+        return lastParent;
+    }
+
     @SuppressFBWarnings("TEMPLATE_INJECTION_FREEMARKER")
     static void render(final Path templateFile, final Object templateData, final Path outputFile) {
-        System.out.format(">>> templateFile: `%s` (exists: %s)%n", templateFile, Files.exists(templateFile));
+        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.
@@ -67,7 +83,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);