]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.gnuplot/src/org/simantics/gnuplot/GnuplotSession.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.gnuplot / src / org / simantics / gnuplot / GnuplotSession.java
1 package org.simantics.gnuplot;
2
3 import java.io.BufferedReader;
4 import java.io.Closeable;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
8 import java.io.PrintStream;
9 import java.nio.file.Files;
10 import java.nio.file.Path;
11
12 /**
13  * @author Tuukka Lehtonen
14  * @since 1.24
15  */
16 public class GnuplotSession implements Closeable {
17
18     private Process process;
19     private PrintStream out;
20
21     private Thread stdoutDumper;
22     private Thread stderrDumper;
23
24     GnuplotSession(Path workingDirectory, boolean drainStdout, boolean drainStderr, Process process) {
25         this.process = process;
26         this.out = new PrintStream(process.getOutputStream());
27         if (drainStdout) {
28             stdoutDumper = new Thread(new InputStreamToFileCopier(process.getInputStream(), null));
29             stdoutDumper.start();
30         }
31         if (drainStderr) {
32             stderrDumper = new Thread(new InputStreamToFileCopier(process.getErrorStream(), null));
33             stderrDumper.start();
34         }
35     }
36
37     private void assertAlive() {
38         if (process == null)
39             throw new IllegalStateException("session has been closed already");
40     }
41
42     public void evaluate(String commands) throws IOException {
43         assertAlive();
44         out.println(commands);
45     }
46
47     public void evaluateStream(InputStream input, String charset) throws IOException {
48         try (BufferedReader br = new BufferedReader(new InputStreamReader(input, charset))) {
49             for (String line = br.readLine(); null != line; line = br.readLine()) {
50                 out.println(line);
51                 out.println();
52             }
53         }
54     }
55
56     public void evaluateFile(Path file) throws IOException {
57         try {
58             Files.lines(file).forEachOrdered(l -> {
59                 try {
60                     evaluate(l);
61                 } catch (IOException e) {
62                     throw new RuntimeException(e);
63                 }
64             });
65         } catch (RuntimeException e) {
66             Throwable t = e.getCause();
67             if (t instanceof IOException)
68                 throw (IOException) t;
69             throw e;
70         }
71     }
72
73     @Override
74     public void close() throws IOException {
75         Process p = process;
76         synchronized (this) {
77             if (process == null)
78                 return;
79             process = null;
80         }
81
82         // Inform the process that its stdin has closed, i.e. is done.
83         out.close();
84         try {
85             // Wait for the outputs to be closed by the process itself.
86             if (stdoutDumper != null)
87                 stdoutDumper.join();
88             if (stderrDumper != null)
89                 stderrDumper.join();
90             p.waitFor();
91             //System.out.println("gnuplot process ended");
92         } catch (InterruptedException e) {
93             throw new IOException(e);
94         }
95     }
96
97 }