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

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-site-plugin.git

commit e825af0590e3c2220ef8e9e78f6f3f6015e68bfa
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Wed Nov 4 11:28:56 2026 +0000

    [MSITE-849] Render only documents on the Mojo thread
    
    Requests, static resources included, are served by the server's thread pool 
again; only document rendering, which can instantiate components that inject 
MavenProject, waits for the Mojo thread.
---
 .../maven/plugins/site/run/DoxiaHandler.java       | 46 +++++++++++++++++++++-
 .../maven/plugins/site/run/MojoThreadExecutor.java |  8 ++--
 .../apache/maven/plugins/site/run/SiteRunMojo.java | 22 ++++++++---
 3 files changed, 65 insertions(+), 11 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/site/run/DoxiaHandler.java 
b/src/main/java/org/apache/maven/plugins/site/run/DoxiaHandler.java
index bb84c5a9..9957776a 100644
--- a/src/main/java/org/apache/maven/plugins/site/run/DoxiaHandler.java
+++ b/src/main/java/org/apache/maven/plugins/site/run/DoxiaHandler.java
@@ -30,6 +30,9 @@ import java.nio.file.Path;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executor;
 
 import com.sun.net.httpserver.HttpExchange;
 import com.sun.net.httpserver.HttpHandler;
@@ -59,6 +62,8 @@ public class DoxiaHandler implements HttpHandler {
 
     private final List<Locale> localesList;
 
+    private final Executor renderExecutor;
+
     private final Log log;
 
     public DoxiaHandler(
@@ -66,11 +71,13 @@ public class DoxiaHandler implements HttpHandler {
             SiteRenderer siteRenderer,
             Map<String, DoxiaBean> i18nDoxiaContexts,
             List<Locale> localesList,
+            Executor renderExecutor,
             Log log) {
         this.outputDirectory = 
outputDirectory.toPath().toAbsolutePath().normalize();
         this.siteRenderer = siteRenderer;
         this.i18nDoxiaContexts = i18nDoxiaContexts;
         this.localesList = localesList;
+        this.renderExecutor = renderExecutor;
         this.log = log;
     }
 
@@ -149,13 +156,50 @@ public class DoxiaHandler implements HttpHandler {
         }
 
         Writer writer = new StringWriter();
-        docRenderer.renderDocument(writer, siteRenderer, context);
+        render(writer, docRenderer, context);
 
         String encoding = context.getOutputEncoding();
         byte[] content = writer.toString().getBytes(Charset.forName(encoding));
         send(exchange, 200, getMimeType(outputName, encoding), content);
     }
 
+    /**
+     * Renders the document with the render executor and waits for it. Doxia 
components contributed by site
+     * dependencies, such as parsers, are instantiated while a document is 
rendered, and some of them inject
+     * objects of Maven's mojo execution scope, which is bound to the Mojo 
thread (MSITE-849).
+     */
+    private void render(Writer writer, DocumentRenderer docRenderer, 
SiteRenderingContext context)
+            throws IOException, RendererException {
+        CompletableFuture<Void> rendered = new CompletableFuture<>();
+        renderExecutor.execute(() -> {
+            try {
+                docRenderer.renderDocument(writer, siteRenderer, context);
+                rendered.complete(null);
+            } catch (Throwable e) {
+                rendered.completeExceptionally(e);
+            }
+        });
+
+        try {
+            rendered.get();
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new IOException("Interrupted while rendering " + 
docRenderer.getOutputName(), e);
+        } catch (ExecutionException e) {
+            Throwable cause = e.getCause();
+            if (cause instanceof IOException) {
+                throw (IOException) cause;
+            } else if (cause instanceof RendererException) {
+                throw (RendererException) cause;
+            } else if (cause instanceof RuntimeException) {
+                throw (RuntimeException) cause;
+            } else if (cause instanceof Error) {
+                throw (Error) cause;
+            }
+            throw new IOException(cause);
+        }
+    }
+
     private void serveStaticContent(HttpExchange exchange, String path) throws 
IOException {
         Path file = outputDirectory.resolve(path).normalize();
         if (!file.startsWith(outputDirectory) || !Files.isRegularFile(file)) {
diff --git 
a/src/main/java/org/apache/maven/plugins/site/run/MojoThreadExecutor.java 
b/src/main/java/org/apache/maven/plugins/site/run/MojoThreadExecutor.java
index 40a1e127..52061cb2 100644
--- a/src/main/java/org/apache/maven/plugins/site/run/MojoThreadExecutor.java
+++ b/src/main/java/org/apache/maven/plugins/site/run/MojoThreadExecutor.java
@@ -25,10 +25,10 @@ import java.util.concurrent.LinkedBlockingQueue;
 /**
  * An {@link Executor} that runs every submitted task on the thread calling 
{@link #runUntilStopped()}.
  * <p>
- * {@code site:run} hands HTTP requests to this executor and serves them from 
the Mojo thread. Maven's
- * thread-bound scopes ({@code MavenProject}, {@code MojoExecution}, {@code 
MavenSession}) are only
- * available on that thread, and Doxia components contributed by site 
dependencies, such as parsers, are
- * instantiated lazily while a page is rendered.
+ * {@code site:run} serves HTTP requests on its own threads and hands the 
rendering of documents to this
+ * executor, which runs it on the Mojo thread. Maven's mojo execution scope 
({@code MavenProject},
+ * {@code MojoExecution}) is only available on that thread, and Doxia 
components contributed by site
+ * dependencies, such as parsers, are instantiated lazily while a document is 
rendered.
  */
 class MojoThreadExecutor implements Executor {
     private static final Runnable STOP = () -> {};
diff --git a/src/main/java/org/apache/maven/plugins/site/run/SiteRunMojo.java 
b/src/main/java/org/apache/maven/plugins/site/run/SiteRunMojo.java
index 76e83de2..13689d5c 100644
--- a/src/main/java/org/apache/maven/plugins/site/run/SiteRunMojo.java
+++ b/src/main/java/org/apache/maven/plugins/site/run/SiteRunMojo.java
@@ -27,6 +27,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 import com.sun.net.httpserver.HttpServer;
 import org.apache.maven.doxia.site.inheritance.SiteModelInheritanceAssembler;
@@ -98,11 +100,18 @@ public class SiteRunMojo extends AbstractSiteRenderingMojo 
{
             throw new MojoExecutionException("Error creating HTTP server on " 
+ host + ":" + port, e);
         }
 
-        // Pages are rendered on this thread: Maven's execution scopes are 
bound to it (MSITE-849)
-        MojoThreadExecutor executor = new MojoThreadExecutor();
-        server.setExecutor(executor);
+        ExecutorService requestExecutor = 
Executors.newCachedThreadPool(runnable -> {
+            Thread thread = new Thread(runnable, "site:run");
+            thread.setDaemon(true);
+            return thread;
+        });
+        // Documents are rendered on this thread: Maven's mojo execution scope 
is bound to it (MSITE-849)
+        MojoThreadExecutor renderExecutor = new MojoThreadExecutor();
+        server.setExecutor(requestExecutor);
         server.createContext(
-                "/", new DoxiaHandler(tempWebappDirectory, siteRenderer, 
i18nDoxiaContexts, getLocales(), getLog()));
+                "/",
+                new DoxiaHandler(
+                        tempWebappDirectory, siteRenderer, i18nDoxiaContexts, 
getLocales(), renderExecutor, getLog()));
 
         server.start();
 
@@ -116,12 +125,13 @@ public class SiteRunMojo extends 
AbstractSiteRenderingMojo {
                 .addShutdownHook(new Thread(
                         () -> {
                             server.stop(0);
-                            executor.stop();
+                            requestExecutor.shutdown();
+                            renderExecutor.stop();
                         },
                         "site:run-shutdown"));
 
         try {
-            executor.runUntilStopped();
+            renderExecutor.runUntilStopped();
         } catch (InterruptedException e) {
             getLog().warn("site:run server was interrupted", e);
         }

Reply via email to