--- /dev/null
+package jalview.util;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
+
+import io.github.classgraph.ClassGraph;
+import io.github.classgraph.ScanResult;
+
+/***
+ * from
+ * http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when
+ * -using-javas-runtime-exec
+ *
+ * @author jimp
+ *
+ */
+public class Worker extends Thread
+{
+ final Process process;
+
+ private BufferedReader outputReader;
+
+ private BufferedReader errorReader;
+
+ Integer exit;
+
+ public Worker(Process process)
+ {
+ this.process = process;
+ }
+
+ @Override
+ public void run()
+ {
+ try
+ {
+ exit = process.waitFor();
+ } catch (InterruptedException ignore)
+ {
+ return;
+ }
+ }
+
+ public BufferedReader getOutputReader()
+ {
+ return outputReader;
+ }
+
+ public void setOutputReader(BufferedReader outputReader)
+ {
+ this.outputReader = outputReader;
+ }
+
+ public BufferedReader getErrorReader()
+ {
+ return errorReader;
+ }
+
+ public void setErrorReader(BufferedReader errorReader)
+ {
+ this.errorReader = errorReader;
+ }
+
+ /**
+ *
+ * @return null or process exit code
+ */
+ public Integer getExitValue()
+ {
+ return exit;
+ }
+
+ /**
+ * terminate with prejudice
+ */
+ public void destroyProcess()
+ {
+ interrupt();
+ currentThread().interrupt();
+ process.destroy();
+ }
+
+ private static ClassGraph scanner = null;
+
+ private static String classpath = null;
+
+ public synchronized static String getClassPath()
+ {
+ if (scanner == null)
+ {
+ scanner = new ClassGraph();
+ ScanResult scan = scanner.scan();
+ classpath = scan.getClasspath();
+ }
+ while (classpath == null)
+ {
+ try
+ {
+ Thread.sleep(10);
+ } catch (InterruptedException x)
+ {
+
+ }
+ }
+ return classpath;
+ }
+
+ public static Worker jalviewDesktopRunner(boolean withAwt, String cmd,
+ int timeout)
+ {
+ return jalviewDesktopRunner(withAwt, cmd, timeout, "");
+ }
+
+ public static Worker jalviewDesktopRunner(boolean withAwt, String cmd,
+ int timeout, String javaArgs)
+ {
+ // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
+ // not expanded by the shell
+ String classpath = getClassPath();
+ String _cmd = "java " + (withAwt ? "-Djava.awt.headless=true" : "")
+ + " -classpath " + classpath + " " + javaArgs
+ + " jalview.bin.Jalview ";
+ Process ls2_proc = null;
+ Worker worker = null;
+ try
+ {
+ ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
+ } catch (IOException e1)
+ {
+ e1.printStackTrace();
+ }
+ if (ls2_proc != null)
+ {
+ BufferedReader outputReader = new BufferedReader(
+ new InputStreamReader(ls2_proc.getInputStream()));
+ BufferedReader errorReader = new BufferedReader(
+ new InputStreamReader(ls2_proc.getErrorStream()));
+ worker = new Worker(ls2_proc);
+ worker.start();
+ try
+ {
+ worker.join(timeout);
+ } catch (InterruptedException e)
+ {
+ // e.printStackTrace();
+ }
+ worker.setOutputReader(outputReader);
+ worker.setErrorReader(errorReader);
+ }
+ return worker;
+ }
+
+ class Echo
+ {
+ public Echo(final PrintStream out, final BufferedReader source)
+ {
+ try
+ {
+ new Thread(new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ String line;
+ try
+ {
+ while ((line = source.readLine()) != null)
+ {
+ out.print(line);
+ }
+ } catch (IOException q)
+ {
+ }
+ ;
+ };
+ }).start();
+ } catch (Exception ex)
+ {
+ }
+ ;
+
+ }
+
+ }
+
+ public void doEcho()
+ {
+ new Echo(System.out, getOutputReader());
+ new Echo(System.err, getErrorReader());
+ }
+}
\ No newline at end of file