update eclipse classpath
[jalview.git] / src / jalview / util / Worker.java
1 package jalview.util;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.io.PrintStream;
7
8 import io.github.classgraph.ClassGraph;
9 import io.github.classgraph.ScanResult;
10
11 /***
12  * from
13  * http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when
14  * -using-javas-runtime-exec
15  * 
16  * @author jimp
17  * 
18  */
19 public class Worker extends Thread
20 {
21   final Process process;
22
23   private BufferedReader outputReader;
24
25   private BufferedReader errorReader;
26
27   Integer exit;
28
29   public Worker(Process process)
30   {
31     this.process = process;
32   }
33
34   @Override
35   public void run()
36   {
37     try
38     {
39       exit = process.waitFor();
40     } catch (InterruptedException ignore)
41     {
42       return;
43     }
44   }
45
46   public BufferedReader getOutputReader()
47   {
48     return outputReader;
49   }
50
51   public void setOutputReader(BufferedReader outputReader)
52   {
53     this.outputReader = outputReader;
54   }
55
56   public BufferedReader getErrorReader()
57   {
58     return errorReader;
59   }
60
61   public void setErrorReader(BufferedReader errorReader)
62   {
63     this.errorReader = errorReader;
64   }
65
66   /**
67    * 
68    * @return null or process exit code
69    */
70   public Integer getExitValue()
71   {
72     return exit;
73   }
74
75   /**
76    * terminate with prejudice
77    */
78   public void destroyProcess()
79   {
80     interrupt();
81     currentThread().interrupt();
82     process.destroy();
83   }
84
85   private static ClassGraph scanner = null;
86
87   private static String classpath = null;
88
89   public synchronized static String getClassPath()
90   {
91     if (scanner == null)
92     {
93       scanner = new ClassGraph();
94       ScanResult scan = scanner.scan();
95       classpath = scan.getClasspath();
96     }
97     while (classpath == null)
98     {
99       try
100       {
101         Thread.sleep(10);
102       } catch (InterruptedException x)
103       {
104
105       }
106     }
107     return classpath;
108   }
109
110   public static Worker jalviewDesktopRunner(boolean withAwt, String cmd,
111           int timeout)
112   {
113     return jalviewDesktopRunner(withAwt, cmd, timeout, "");
114   }
115
116   public static Worker jalviewDesktopRunner(boolean withAwt, String cmd,
117           int timeout, String javaArgs)
118   {
119     // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
120     // not expanded by the shell
121     String classpath = getClassPath();
122     String _cmd = "java " + (withAwt ? "-Djava.awt.headless=true" : "")
123             + " -classpath " + classpath + " " + javaArgs
124             + " jalview.bin.Jalview ";
125     Process ls2_proc = null;
126     Worker worker = null;
127     try
128     {
129       ls2_proc = Runtime.getRuntime().exec(_cmd + cmd);
130     } catch (IOException e1)
131     {
132       e1.printStackTrace();
133     }
134     if (ls2_proc != null)
135     {
136       BufferedReader outputReader = new BufferedReader(
137               new InputStreamReader(ls2_proc.getInputStream()));
138       BufferedReader errorReader = new BufferedReader(
139               new InputStreamReader(ls2_proc.getErrorStream()));
140       worker = new Worker(ls2_proc);
141       worker.start();
142       try
143       {
144         worker.join(timeout);
145       } catch (InterruptedException e)
146       {
147         // e.printStackTrace();
148       }
149       worker.setOutputReader(outputReader);
150       worker.setErrorReader(errorReader);
151     }
152     return worker;
153   }
154
155   class Echo
156   {
157     public Echo(final PrintStream out, final BufferedReader source)
158     {
159       try
160       {
161         new Thread(new Runnable()
162         {
163           @Override
164           public void run()
165           {
166             String line;
167             try
168             {
169               while ((line = source.readLine()) != null)
170               {
171                 out.print(line);
172               }
173             } catch (IOException q)
174             {
175             }
176             ;
177           };
178         }).start();
179       } catch (Exception ex)
180       {
181       }
182       ;
183
184     }
185
186   }
187
188   public void doEcho()
189   {
190     new Echo(System.out, getOutputReader());
191     new Echo(System.err, getErrorReader());
192   }
193 }