From 98bcf3decdc80eb809f8000312cc9c15439a1358 Mon Sep 17 00:00:00 2001 From: Oscar Gonzalez Fernandez Date: Tue, 11 Jun 2013 13:18:07 +0200 Subject: [PATCH] Don't use the same display number always Otherwise the Xvfb server could fail if another printing request overlaps. They get different display numbers now. --- .../org/libreplan/web/print/CutyPrint.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/libreplan-webapp/src/main/java/org/libreplan/web/print/CutyPrint.java b/libreplan-webapp/src/main/java/org/libreplan/web/print/CutyPrint.java index 25074773f..354a005de 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/web/print/CutyPrint.java +++ b/libreplan-webapp/src/main/java/org/libreplan/web/print/CutyPrint.java @@ -34,6 +34,7 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; +import java.util.concurrent.atomic.AtomicLong; import javax.servlet.ServletContext; import javax.servlet.ServletException; @@ -148,6 +149,8 @@ public class CutyPrint { + extension; } + private static final AtomicLong counter = new AtomicLong(); + private final HttpServletRequest request = (HttpServletRequest) Executions .getCurrent().getNativeRequest(); private final ServletContext context = request.getSession() @@ -163,6 +166,8 @@ public class CutyPrint { private final String generatedSnapshotServerPath; + private final int recentUniqueToken = (int) (counter.getAndIncrement() % 1000); + public CutyCaptParameters(final String forwardURL, final Map entryPointsMap, Map printParameters, Planner planner) { @@ -185,6 +190,17 @@ public class CutyPrint { return generatedSnapshotServerPath; } + /** + * An unique recent display number for Xvfb. It's not truly unique + * across all the life of a libreplan application, but it's in the last + * period of time. + * + * @return the display number to use by Xvfb + */ + public int getXvfbDisplayNumber() { + return recentUniqueToken + 1; // avoid display 0 + } + void fillParameters(ProcessBuilder c) { Map parameters = buildParameters(); for (Entry each : parameters.entrySet()) { @@ -386,11 +402,13 @@ public class CutyPrint { // If there is a not real X server environment then use Xvfb if (System.getenv("DISPLAY") == null || System.getenv("DISPLAY").equals("")) { - ProcessBuilder s = new ProcessBuilder("Xvfb", ":99"); + ProcessBuilder s = new ProcessBuilder("Xvfb", ":" + + params.getXvfbDisplayNumber()); s.redirectOutput(Redirect.INHERIT).redirectError( Redirect.INHERIT); serverProcess = s.start(); - capture.environment().put("DISPLAY", ":99.0"); + capture.environment().put("DISPLAY", + ":" + params.getXvfbDisplayNumber() + ".0"); } printProcess = capture.start(); printProcess.waitFor();