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());
}
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());
}
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 {
package org.simantics.gnuplot;
+import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
private Thread stdoutDumper;
private Thread stderrDumper;
- GnuplotSession(Path workingDirectory, Path stdout, Path stderr, Process process) {
+ GnuplotSession(Path workingDirectory, boolean drainStdout, boolean drainStderr, Process process) {
this.process = process;
this.out = new PrintStream(process.getOutputStream());
- stdoutDumper = new Thread(new InputStreamToFileCopier(process.getInputStream(), stdout));
- stderrDumper = new Thread(new InputStreamToFileCopier(process.getErrorStream(), stderr));
- stdoutDumper.start();
- stderrDumper.start();
+ if (drainStdout) {
+ stdoutDumper = new Thread(new InputStreamToFileCopier(process.getInputStream(), null));
+ stdoutDumper.start();
+ }
+ if (drainStderr) {
+ stderrDumper = new Thread(new InputStreamToFileCopier(process.getErrorStream(), null));
+ stderrDumper.start();
+ }
}
private void assertAlive() {
out.println(commands);
}
+ public void evaluateStream(InputStream input, String charset) throws IOException {
+ try (BufferedReader br = new BufferedReader(new InputStreamReader(input, charset))) {
+ for (String line = br.readLine(); null != line; line = br.readLine()) {
+ out.println(line);
+ out.println();
+ }
+ }
+ }
+
public void evaluateFile(Path file) throws IOException {
try {
Files.lines(file).forEachOrdered(l -> {
out.close();
try {
// Wait for the outputs to be closed by the process itself.
- stdoutDumper.join();
- stderrDumper.join();
+ if (stdoutDumper != null)
+ stdoutDumper.join();
+ if (stderrDumper != null)
+ stderrDumper.join();
p.waitFor();
//System.out.println("gnuplot process ended");
} catch (InterruptedException e) {
+++ /dev/null
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
- <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
- <classpathentry kind="src" path="src"/>
- <classpathentry kind="output" path="bin"/>
-</classpath>
+++ /dev/null
-<?xml version="1.0" encoding="UTF-8"?>
-<projectDescription>
- <name>org.simantics.msvc.runtime.x86</name>
- <comment></comment>
- <projects>
- </projects>
- <buildSpec>
- <buildCommand>
- <name>org.eclipse.jdt.core.javabuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- <buildCommand>
- <name>org.eclipse.pde.ManifestBuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- <buildCommand>
- <name>org.eclipse.pde.SchemaBuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- </buildSpec>
- <natures>
- <nature>org.eclipse.pde.PluginNature</nature>
- <nature>org.eclipse.jdt.core.javanature</nature>
- </natures>
-</projectDescription>
+++ /dev/null
-eclipse.preferences.version=1
-org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
-org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
-org.eclipse.jdt.core.compiler.compliance=1.6
-org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
-org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
-org.eclipse.jdt.core.compiler.source=1.6
+++ /dev/null
-Manifest-Version: 1.0
-Bundle-ManifestVersion: 2
-Bundle-Name: Microsoft Visual C++ Runtime x86 libraries
-Bundle-SymbolicName: org.simantics.msvc.runtime.x86
-Bundle-Version: 10.0.40219.qualifier
-Bundle-Vendor: Semantum Oy
-Fragment-Host: org.simantics.msvc.runtime
-Bundle-RequiredExecutionEnvironment: JavaSE-1.6
-Eclipse-PlatformFilter: (& (osgi.os=win32) (osgi.arch=x86))
-Bundle-NativeCode: msvcr100.dll; msvcp100.dll; processor=x86
+++ /dev/null
-source.. = src/
-output.. = bin/
-bin.includes = META-INF/,\
- .,\
- msvcp100.dll,\
- msvcr100.dll
+++ /dev/null
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
- <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
- <classpathentry kind="src" path="src"/>
- <classpathentry kind="output" path="bin"/>
-</classpath>
+++ /dev/null
-<?xml version="1.0" encoding="UTF-8"?>
-<projectDescription>
- <name>org.simantics.msvc.runtime</name>
- <comment></comment>
- <projects>
- </projects>
- <buildSpec>
- <buildCommand>
- <name>org.eclipse.jdt.core.javabuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- <buildCommand>
- <name>org.eclipse.pde.ManifestBuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- <buildCommand>
- <name>org.eclipse.pde.SchemaBuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
- </buildSpec>
- <natures>
- <nature>org.eclipse.pde.PluginNature</nature>
- <nature>org.eclipse.jdt.core.javanature</nature>
- </natures>
-</projectDescription>
+++ /dev/null
-eclipse.preferences.version=1
-org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
-org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
-org.eclipse.jdt.core.compiler.compliance=1.6
-org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
-org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
-org.eclipse.jdt.core.compiler.source=1.6
+++ /dev/null
-Manifest-Version: 1.0
-Bundle-ManifestVersion: 2
-Bundle-Name: Microsoft Visual C++ Runtimes
-Bundle-SymbolicName: org.simantics.msvc.runtime
-Bundle-Version: 10.0.40219.qualifier
-Bundle-Activator: org.simantics.msvc.runtime.Activator
-Bundle-Vendor: Semantum Oy
-Require-Bundle: org.eclipse.core.runtime
-Bundle-RequiredExecutionEnvironment: JavaSE-1.6
-Bundle-ActivationPolicy: lazy
-Export-Package: org.simantics.msvc.runtime
+++ /dev/null
-source.. = src/
-output.. = bin/
-bin.includes = META-INF/,\
- .
+++ /dev/null
-package org.simantics.msvc.runtime;
-
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-
-public class Activator implements BundleActivator {
-
- private static BundleContext context;
-
- static BundleContext getContext() {
- return context;
- }
-
- /*
- * (non-Javadoc)
- * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
- */
- public void start(BundleContext bundleContext) throws Exception {
- Activator.context = bundleContext;
- Runtimes.initialize(Runtimes.VC_2010);
- }
-
- /*
- * (non-Javadoc)
- * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
- */
- public void stop(BundleContext bundleContext) throws Exception {
- Activator.context = null;
- }
-
-}
+++ /dev/null
-package org.simantics.msvc.runtime;
-
-import java.io.File;
-import java.io.UnsupportedEncodingException;
-import java.net.URL;
-import java.net.URLDecoder;
-
-import org.eclipse.core.runtime.Path;
-
-/**
- * @author Tuukka Lehtonen
- */
-public final class Runtimes {
-
- public static final String VC_2010 = "vc2010";
-
- static boolean vc100Initialized = false;
-
- public static void initialize(String vc) {
- OSType os = OSType.calculate();
- if (os == OSType.WINDOWS) {
- if (VC_2010.equals(vc) && !vc100Initialized) {
- initializeVC100();
- }
- }
- }
-
- static synchronized void initializeVC100() {
- try {
- // Works in OSGi environment.
- System.loadLibrary("msvcr100");
- System.loadLibrary("msvcp100");
- vc100Initialized = true;
- } catch (UnsatisfiedLinkError e) {
- // Try to load from a workspace in POJO environment.
- try {
- String archStr = ARCHType.calculate().toString().toLowerCase();
- URL url = Runtimes.class.getResource(".");
- File dir = new File(URLDecoder.decode(url.getPath(), "UTF-8") + "../../../../../../org.simantics.msvc.runtime." + archStr);
- dir = new Path(dir.getAbsolutePath()).toFile();
- String vcr = new File(dir, "msvcr100.dll").getAbsolutePath();
- String vcp = new File(dir, "msvcp100.dll").getAbsolutePath();
- System.load(vcr);
- System.load(vcp);
- vc100Initialized = true;
- } catch (UnsupportedEncodingException e1) {
- throw new Error("UTF-8 not supported, impossible");
- }
- }
- }
-
- public enum ARCHType {
- PPC, PPC_64, SPARC, X86, X86_64, UNKNOWN;
-
- public static ARCHType calculate() {
- String osArch = System.getProperty("os.arch");
- assert osArch != null;
- osArch = osArch.toLowerCase();
- if (osArch.equals("i386") || osArch.equals("i586") || osArch.equals("i686") || osArch.equals("x86"))
- return X86;
- if (osArch.startsWith("amd64") || osArch.startsWith("x86_64"))
- return X86_64;
- if (osArch.equals("ppc"))
- return PPC;
- if (osArch.startsWith("ppc"))
- return PPC_64;
- if (osArch.startsWith("sparc"))
- return SPARC;
- return UNKNOWN;
- }
- }
-
- public enum OSType {
- APPLE, LINUX, SUN, WINDOWS, UNKNOWN;
-
- public static OSType calculate() {
- String osName = System.getProperty("os.name");
- assert osName != null;
- osName = osName.toLowerCase();
- if (osName.startsWith("mac os x"))
- return APPLE;
- if (osName.startsWith("windows"))
- return WINDOWS;
- if (osName.startsWith("linux"))
- return LINUX;
- if (osName.startsWith("sun"))
- return SUN;
- return UNKNOWN;
- }
- }
-
-}