X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.gnuplot%2Fsrc%2Forg%2Fsimantics%2Fgnuplot%2FGnuplot.java;h=be2eda1dcb0db0ce76e6fe3459542b294172b1e0;hb=f1b5a0bf9e546ff51e4460140ae529b909a3e188;hp=f1321796ab6fdd6b5f3407e3415f9d1dfa2e08ee;hpb=68e5274fbda4c51b920db69b2e87d5646c446ce4;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.gnuplot/src/org/simantics/gnuplot/Gnuplot.java b/bundles/org.simantics.gnuplot/src/org/simantics/gnuplot/Gnuplot.java index f1321796a..be2eda1dc 100644 --- a/bundles/org.simantics.gnuplot/src/org/simantics/gnuplot/Gnuplot.java +++ b/bundles/org.simantics.gnuplot/src/org/simantics/gnuplot/Gnuplot.java @@ -19,10 +19,19 @@ public class Gnuplot { private Path executable; - public Gnuplot(Path executable) { + public Gnuplot(Path executable) throws IOException { + if (!Files.exists(executable)) + throw new IOException("Provided gnuplot executable does not exist: " + executable); + if (!Files.isExecutable(executable)) + throw new IOException("Provided gnuplot executable is not marked executable: " + executable); this.executable = executable; } + @Override + public String toString() { + return executable.toString(); + } + public static Gnuplot detect() throws IOException { try { return new Gnuplot(findGnuplotPath()); @@ -104,23 +113,40 @@ public class Gnuplot { } private void execute0(Path workingDirectory, Path stdout, Path stderr, String... cmdline) throws IOException { - Process process = new ProcessBuilder(cmdline) - .directory(workingDirectory.toFile()) - .start(); + ProcessBuilder builder = new ProcessBuilder(cmdline) + .directory(workingDirectory.toFile()); + + if (stdout != null) + builder.redirectOutput(stdout.toFile()); + if (stderr != null) + builder.redirectError(stderr.toFile()); + + Process process = builder.start(); + Thread stdoutDumper = stdout == null ? new Thread(new InputStreamToFileCopier(process.getInputStream(), null)) : null; + Thread stderrDumper = stderr == null ? new Thread(new InputStreamToFileCopier(process.getErrorStream(), null)) : null; + if (stdoutDumper != null) + stdoutDumper.start(); + if (stderrDumper != null) + stderrDumper.start(); + process.getOutputStream().close(); - Thread stdoutDumper = new Thread(new InputStreamToFileCopier(process.getInputStream(), stdout)); - Thread stderrDumper = new Thread(new InputStreamToFileCopier(process.getErrorStream(), stderr)); - stdoutDumper.start(); - stderrDumper.start(); try { - stdoutDumper.join(); - stderrDumper.join(); + if (stdoutDumper != null) + stdoutDumper.join(); + if (stderrDumper != null) + stderrDumper.join(); process.waitFor(); } catch (InterruptedException e) { throw new IOException(e); } } + public void execute(Path workingDirectory, Path stdout, Path stderr, boolean redirectStderr, InputStream input, String charset) throws IOException { + try (GnuplotSession session = newSession(workingDirectory, stdout, stderr, redirectStderr)) { + session.evaluateStream(input, charset); + } + } + public void execute(Path workingDirectory, Path stdout, Path stderr, Path script) throws IOException { execute0(workingDirectory, stdout, stderr, executable.toString(), "-c", script.toString()); } @@ -138,11 +164,24 @@ public class Gnuplot { execute0(workingDirectory, stdout, stderr, executable.toString(), "-e", e.toString()); } - public GnuplotSession newSession(Path workingDirectory, Path stdout, Path stderr) throws IOException { - Process process = new ProcessBuilder(executable.toString()) - .directory(workingDirectory.toFile()) - .start(); - return new GnuplotSession(workingDirectory, stdout, stderr, process); + private ProcessBuilder buildSessionProcess(Path workingDirectory, Path stdout, Path stderr, boolean redirectErrorStream) { + ProcessBuilder builder = new ProcessBuilder(executable.toString()) + .directory(workingDirectory != null ? workingDirectory.toFile() : null) + .redirectErrorStream(redirectErrorStream); + if (stdout != null) + builder.redirectOutput(stdout.toFile()); + if (stderr != null) + builder.redirectError(stderr.toFile()); + return builder; + } + + public GnuplotSession newSession(Path workingDirectory, Path stdout) throws IOException { + return newSession(workingDirectory, stdout, null, true); + } + + public GnuplotSession newSession(Path workingDirectory, Path stdout, Path stderr, boolean redirectStderr) throws IOException { + return new GnuplotSession(workingDirectory, stdout == null, stderr == null, + buildSessionProcess(workingDirectory, stdout, stderr, redirectStderr).start()); } static enum OSType {