From cc71a32d278f0963fd2f86f1d78e35000efa0d3f Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Sun, 19 Mar 2017 20:04:07 +0100 Subject: [PATCH] make location of gnuplot configurable --- .../org/lucares/recommind/logs/Gnuplot.java | 56 +++++++++++++++++-- .../org/lucares/recommind/logs/Plotter.java | 2 +- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/pdb-plotting/src/main/java/org/lucares/recommind/logs/Gnuplot.java b/pdb-plotting/src/main/java/org/lucares/recommind/logs/Gnuplot.java index 6dc92bd..52d6dcb 100644 --- a/pdb-plotting/src/main/java/org/lucares/recommind/logs/Gnuplot.java +++ b/pdb-plotting/src/main/java/org/lucares/recommind/logs/Gnuplot.java @@ -6,10 +6,18 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.util.Collection; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import com.google.common.io.Files; public class Gnuplot { + private static final Logger LOGGER = LoggerFactory.getLogger(Gnuplot.class); + private static final Logger METRICS_LOGGER = LoggerFactory.getLogger("org.lucares.metrics.gnuplot"); + + private static final String ENV_GNUPLOT_HOME = "GNUPLOT_HOME"; + private static final String PROPERTY_GNUPLOT_HOME = "gnuplot.home"; private final Path tmpDirectory; public Gnuplot(final Path tmpDirectory) { @@ -22,18 +30,54 @@ public class Gnuplot { final GnuplotFileGenerator generator = new GnuplotFileGenerator(); final String gnuplotFileContent = generator.generate(settings, dataSeries); - System.out.println(gnuplotFileContent); + LOGGER.debug(gnuplotFileContent); final File gnuplotFile = File.createTempFile("gnuplot", ".dem", tmpDirectory.toFile()); Files.write(gnuplotFileContent, gnuplotFile, StandardCharsets.UTF_8); final long start = System.nanoTime(); - final ProcessBuilder processBuilder = new ProcessBuilder("gnuplot", gnuplotFile.getAbsolutePath()); - processBuilder.inheritIO(); - final Process process = processBuilder.start(); - process.waitFor(); + try { + final ProcessBuilder processBuilder = new ProcessBuilder(gnuplotBinary(), gnuplotFile.getAbsolutePath()); + processBuilder.inheritIO(); + final Process process = processBuilder.start(); + process.waitFor(); + } catch (final IOException e) { + if (e.getMessage().contains("No such file or directory")) { + throw new IOException("Did not find gnuplot. Add it to the 'PATH' or create an environment variable '" + + ENV_GNUPLOT_HOME + "' or add the java property '" + PROPERTY_GNUPLOT_HOME + "': " + + e.getMessage(), e); + } else { + throw e; + } + } - System.out.println("gnuplot: " + (System.nanoTime() - start) / 1_000_000.0 + "ms"); + METRICS_LOGGER.debug("gnuplot: {}ms", (System.nanoTime() - start) / 1_000_000.0); + } + + private String gnuplotBinary() { + + if (System.getProperty(PROPERTY_GNUPLOT_HOME) != null) { + + if (isWindows()) { + return System.getProperty(PROPERTY_GNUPLOT_HOME) + "\\bin\\+gnuplot.exe"; + } else { + return System.getProperty(PROPERTY_GNUPLOT_HOME) + "/bin/gnuplot"; + } + } + if (System.getenv(ENV_GNUPLOT_HOME) != null) { + + if (isWindows()) { + return System.getenv(ENV_GNUPLOT_HOME) + "\\bin\\+gnuplot.exe"; + } else { + return System.getenv(ENV_GNUPLOT_HOME) + "/bin/gnuplot"; + } + } + + return "gnuplot"; + } + + private boolean isWindows() { + return System.getProperty("os.name").toLowerCase().startsWith("windows"); } } diff --git a/pdb-plotting/src/main/java/org/lucares/recommind/logs/Plotter.java b/pdb-plotting/src/main/java/org/lucares/recommind/logs/Plotter.java index cd83361..aef514a 100644 --- a/pdb-plotting/src/main/java/org/lucares/recommind/logs/Plotter.java +++ b/pdb-plotting/src/main/java/org/lucares/recommind/logs/Plotter.java @@ -108,7 +108,7 @@ public class Plotter { Thread.currentThread().interrupt(); throw new IllegalStateException("Plotting was interrupted."); } catch (final IOException e) { - throw new InternalPlottingException("Plotting failed.", e); + throw new InternalPlottingException("Plotting failed: " + e.getMessage(), e); } finally { FileUtils.delete(tmpDir); }