From 15eab391311e45ef17da2fd9e9ce2aefa4bcd128 Mon Sep 17 00:00:00 2001 From: Lorenzo Tilve Date: Tue, 23 Mar 2010 02:03:49 +0100 Subject: [PATCH] ItEr52S04ValidacionEProbasFuncionaisItEr51S04: Implemented timeout thread to terminate unfinished CutyCapt processes --- .../web/print/CutyCaptTimeout.java | 47 +++++++++++++++++++ .../org/navalplanner/web/print/CutyPrint.java | 30 +++++++----- 2 files changed, 64 insertions(+), 13 deletions(-) create mode 100644 navalplanner-webapp/src/main/java/org/navalplanner/web/print/CutyCaptTimeout.java diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/print/CutyCaptTimeout.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/print/CutyCaptTimeout.java new file mode 100644 index 000000000..e2ca4167c --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/print/CutyCaptTimeout.java @@ -0,0 +1,47 @@ +/* + * This file is part of NavalPlan + * + * Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e + * Desenvolvemento Tecnolóxico de Galicia + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package org.navalplanner.web.print; + +import static org.zkoss.ganttz.i18n.I18nHelper._; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + + +public class CutyCaptTimeout extends Thread { + + long timeout; + + private static final Log LOG = LogFactory.getLog(CutyPrint.class); + + CutyCaptTimeout(int timeout) { + this.timeout = timeout; + } + + public void run() { + try { + sleep(timeout); + Runtime.getRuntime().exec("killall CutyCapt"); + } catch (Exception e) { + LOG.error(_("CutycaptTimeout thread exception"), e); + } + } +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/print/CutyPrint.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/print/CutyPrint.java index d3928c448..911635432 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/print/CutyPrint.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/print/CutyPrint.java @@ -53,6 +53,8 @@ public class CutyPrint { private static final Log LOG = LogFactory.getLog(CutyPrint.class); private static final String CUTYCAPT_COMMAND = "/usr/bin/CutyCapt "; + // Estimated maximum execution time (ms) + private static final int CUTYCAPT_TIMEOUT = 20000; // Taskdetails left padding private static int TASKDETAILS_WIDTH = 310; @@ -183,34 +185,36 @@ public class CutyPrint { // Destination complete absolute path captureString += " --out=" + absolutePath + filename; - try { // CutyCapt command execution LOG.warn(captureString); - Process print; - Process server = null; - - // Ensure cleanup of unfinished CutyCapt processes and CSS - Runtime.getRuntime().exec("killall CutyCapt"); + Process printProcess; + Process serverProcess = null; // If there is a not real X server environment then use Xvfb if ((System.getenv("DISPLAY") == null) || (System.getenv("DISPLAY").equals(""))) { String[] serverEnvironment = { "PATH=$PATH" }; - server = Runtime.getRuntime().exec("env - Xvfb :99", + serverProcess = Runtime.getRuntime().exec("env - Xvfb :99", serverEnvironment); String[] environment = { "DISPLAY=:99.0" }; - print = Runtime.getRuntime().exec(captureString, environment); + printProcess = Runtime.getRuntime().exec(captureString, + environment); } else { - print = Runtime.getRuntime().exec(captureString); + printProcess = Runtime.getRuntime().exec(captureString); } try { - print.waitFor(); - print.destroy(); + // Ensure CutyCapt process finalization + CutyCaptTimeout timeoutThread = new CutyCaptTimeout( CUTYCAPT_TIMEOUT ); + new Thread(timeoutThread).start(); + + printProcess.waitFor(); + printProcess.destroy(); + if ((System.getenv("DISPLAY") == null) || (System.getenv("DISPLAY").equals(""))) { - server.destroy(); + serverProcess.destroy(); } Executions.getCurrent().sendRedirect(filename, "_blank"); } catch (Exception e) { @@ -278,4 +282,4 @@ public class CutyPrint { } else return srFile; } -} +} \ No newline at end of file