Remove `stop()` FreeMarker method
diff --git a/log4j-tools-internal-freemarker-util/src/main/java/org/apache/logging/log4j/tools/internal/freemarker/util/FreeMarkerUtils.java b/log4j-tools-internal-freemarker-util/src/main/java/org/apache/logging/log4j/tools/internal/freemarker/util/FreeMarkerUtils.java
index 82c2bbc..19295b0 100644
--- a/log4j-tools-internal-freemarker-util/src/main/java/org/apache/logging/log4j/tools/internal/freemarker/util/FreeMarkerUtils.java
+++ b/log4j-tools-internal-freemarker-util/src/main/java/org/apache/logging/log4j/tools/internal/freemarker/util/FreeMarkerUtils.java
@@ -23,8 +23,6 @@
import freemarker.template.DefaultObjectWrapperBuilder;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
-import freemarker.template.TemplateMethodModelEx;
-import freemarker.template.TemplateModelException;
import freemarker.template.Version;
import java.io.BufferedWriter;
import java.io.IOException;
@@ -37,7 +35,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
-import java.util.List;
public final class FreeMarkerUtils {
@@ -49,52 +46,6 @@
private FreeMarkerUtils() {}
- private static final class StopMethod implements TemplateMethodModelEx {
-
- private static final StopMethod INSTANCE = new StopMethod();
-
- private static final TemplateModelException SIGNAL = new TemplateModelException();
-
- @Override
- public Object exec(final List arguments) throws TemplateModelException {
- throw SIGNAL;
- }
-
- private static boolean invoked(final Throwable throwable) {
-
- // Keep a second pointer that slowly walks the causal chain.
- // If the fast pointer ever catches the slower pointer, then there's a loop.
- Throwable slowPointer = throwable;
- boolean advanceSlowPointer = false;
-
- Throwable parent = throwable;
- while (true) {
-
- // Check the exception
- if (parent == StopMethod.SIGNAL) {
- return true;
- }
-
- // Advance the cause
- final Throwable cause = parent.getCause();
- if (cause == null) {
- break;
- }
-
- // Advance pointers
- parent = cause;
- if (parent == slowPointer) {
- throw new IllegalArgumentException("loop in causal chain");
- }
- if (advanceSlowPointer) {
- slowPointer = slowPointer.getCause();
- }
- advanceSlowPointer = !advanceSlowPointer; // only advance every other iteration
- }
- return false;
- }
- }
-
@SuppressFBWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME")
private static Configuration createConfiguration(final Path templateDirectory) {
final Configuration configuration = new Configuration(CONFIGURATION_VERSION);
@@ -112,7 +63,6 @@
configuration.setLogTemplateExceptions(false);
configuration.setWrapUncheckedExceptions(true);
configuration.setFallbackOnNullLoopVariable(false);
- configuration.setSharedVariable("stop", StopMethod.INSTANCE);
return configuration;
}
@@ -138,13 +88,10 @@
outputFileWriter.write(templateOutput);
}
} catch (final Exception error) {
- final boolean stopMethodInvoked = StopMethod.invoked(error);
- if (!stopMethodInvoked) {
- final String message = String.format(
- "failed rendering template `%s` in directory `%s` to file `%s`",
- templateName, templateDirectory, outputFile);
- throw new RuntimeException(message, error);
- }
+ final String message = String.format(
+ "failed rendering template `%s` in directory `%s` to file `%s`",
+ templateName, templateDirectory, outputFile);
+ throw new RuntimeException(message, error);
}
}
diff --git a/log4j-tools-internal-freemarker-util/src/test/java/org/apache/logging/log4j/tools/internal/freemarker/util/FreeMarkerUtilsTest.java b/log4j-tools-internal-freemarker-util/src/test/java/org/apache/logging/log4j/tools/internal/freemarker/util/FreeMarkerUtilsTest.java
index 5aeb65d..612892d 100644
--- a/log4j-tools-internal-freemarker-util/src/test/java/org/apache/logging/log4j/tools/internal/freemarker/util/FreeMarkerUtilsTest.java
+++ b/log4j-tools-internal-freemarker-util/src/test/java/org/apache/logging/log4j/tools/internal/freemarker/util/FreeMarkerUtilsTest.java
@@ -51,25 +51,6 @@
}
@Test
- void render_should_stop(@TempDir(cleanup = CleanupMode.ON_SUCCESS) final Path tempDir) throws Exception {
-
- // Create the template file
- final String templateName = "test.txt.ftl";
- final Path templateFile = tempDir.resolve(templateName);
- final String excessiveContentToEnsureSuccessIsNotDueToBuffering = String.format("%010000d\n", 1);
- final String templateFileContent = excessiveContentToEnsureSuccessIsNotDueToBuffering
- + "You might expect awesome things to get rendered.\nBut I tell you: they won't.\n${stop()}";
- write(templateFile, templateFileContent.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE_NEW);
-
- // Render the template
- final Path outputFile = tempDir.resolve("test.txt");
- render(tempDir, templateName, Collections.singletonMap("name", "volkan"), outputFile);
-
- // Verify the generated content
- assertThat(outputFile).doesNotExist();
- }
-
- @Test
void renderString_should_work() {
final String output = renderString("Hello, ${name?capitalize}!", Collections.singletonMap("name", "volkan"));
assertThat(output).isEqualTo("Hello, Volkan!");
diff --git a/src/changelog/.0.x.x/add-freemarker-stop-method.xml b/src/changelog/.0.x.x/add-freemarker-stop-method.xml
deleted file mode 100644
index a7b4093..0000000
--- a/src/changelog/.0.x.x/add-freemarker-stop-method.xml
+++ /dev/null
@@ -1,9 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="https://logging.apache.org/xml/ns"
- xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
- type="added">
- <issue id="122" link="https://github.com/apache/logging-log4j-tools/pull/122"/>
- <description format="asciidoc">Add `stop()` FreeMarker method to skip template file generation.
-A use case is to have multiple `typeTemplates` in the `log4j-docgen:generate-documentation` configuration and skip generating certain files if desired.</description>
-</entry>