make location of gnuplot configurable

This commit is contained in:
2017-03-19 20:04:07 +01:00
parent ea905c2315
commit cc71a32d27
2 changed files with 51 additions and 7 deletions

View File

@@ -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());
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");
}
}

View File

@@ -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);
}