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