[702] | 1 | package net.oni2.aeinstaller.backend.appexecution;
|
---|
[623] | 2 |
|
---|
| 3 | import java.io.BufferedReader;
|
---|
| 4 | import java.io.File;
|
---|
| 5 | import java.io.IOException;
|
---|
| 6 | import java.io.InputStream;
|
---|
| 7 | import java.io.InputStreamReader;
|
---|
[703] | 8 | import java.util.Date;
|
---|
[623] | 9 | import java.util.List;
|
---|
| 10 | import java.util.Vector;
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | * @author Christian Illy
|
---|
| 14 | */
|
---|
| 15 | public class AppExecution {
|
---|
| 16 | /**
|
---|
| 17 | * Execute a short running application and wait for termination
|
---|
| 18 | *
|
---|
| 19 | * @param cmdLine
|
---|
| 20 | * List of command and arguments
|
---|
[702] | 21 | * @return Error code and list of output lines
|
---|
[623] | 22 | * @throws IOException
|
---|
| 23 | * Exc
|
---|
| 24 | */
|
---|
[702] | 25 | public static AppExecutionResult executeAndWait(Vector<String> cmdLine)
|
---|
[623] | 26 | throws IOException {
|
---|
[703] | 27 | long start = new Date().getTime();
|
---|
[623] | 28 | ProcessBuilder pb = new ProcessBuilder(cmdLine);
|
---|
| 29 | pb.redirectErrorStream(true);
|
---|
| 30 | Process proc = pb.start();
|
---|
| 31 |
|
---|
| 32 | InputStream is = proc.getInputStream();
|
---|
| 33 | InputStreamReader isr = new InputStreamReader(is);
|
---|
| 34 | BufferedReader br = new BufferedReader(isr);
|
---|
| 35 |
|
---|
| 36 | String line;
|
---|
| 37 | Vector<String> lines = new Vector<String>();
|
---|
| 38 |
|
---|
| 39 | while ((line = br.readLine()) != null) {
|
---|
| 40 | lines.add(line);
|
---|
| 41 | }
|
---|
[702] | 42 | try {
|
---|
| 43 | proc.waitFor();
|
---|
| 44 | } catch (InterruptedException e) {
|
---|
| 45 | // TODO Auto-generated catch block
|
---|
| 46 | e.printStackTrace();
|
---|
| 47 | }
|
---|
[703] | 48 | return new AppExecutionResult(proc.exitValue(), cmdLine, lines,
|
---|
| 49 | (int) (new Date().getTime() - start));
|
---|
[623] | 50 | }
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * Execute an app without waiting
|
---|
| 54 | *
|
---|
| 55 | * @param cmd
|
---|
| 56 | * Command and parameters
|
---|
| 57 | * @param workingDirectory
|
---|
| 58 | * Working directory of app
|
---|
| 59 | */
|
---|
| 60 | public static void execute(List<String> cmd, File workingDirectory) {
|
---|
| 61 | try {
|
---|
| 62 | ProcessBuilder pb = new ProcessBuilder(cmd);
|
---|
| 63 | pb.directory(workingDirectory);
|
---|
| 64 | pb.start();
|
---|
| 65 | } catch (IOException e) {
|
---|
| 66 | e.printStackTrace();
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | }
|
---|