summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/wizards/bsp/BSPThread.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/wizards/bsp/BSPThread.java')
-rw-r--r--plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/wizards/bsp/BSPThread.java92
1 files changed, 92 insertions, 0 deletions
diff --git a/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/wizards/bsp/BSPThread.java b/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/wizards/bsp/BSPThread.java
new file mode 100644
index 0000000..f6b19ac
--- /dev/null
+++ b/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/wizards/bsp/BSPThread.java
@@ -0,0 +1,92 @@
1package org.yocto.sdk.remotetools.wizards.bsp;
2
3import java.io.BufferedReader;
4import java.io.InputStreamReader;
5import java.util.ArrayList;
6
7/**
8 * Receives a command to be run on a separate thread in the background
9 * It contains an BSPAction object that will collect the output & error
10 * Output lines are processed and collected into the items of BSPAction
11 * @author ioana.grigoropol
12 *
13 */
14public abstract class BSPThread implements Runnable {
15 public static final String SUCCESS = "success";
16 public static final String ERROR = "error";
17
18 private BSPAction bspAction;
19 private String command;
20
21 /**
22 * Receives the command to be run in the background
23 * @param command
24 */
25 public BSPThread(String command) {
26 this.command = command;
27 this.bspAction = new BSPAction(null, null);
28 }
29
30 @Override
31 public void run() {
32 ArrayList<String> values = new ArrayList<String>();
33
34 try {
35 ProcessBuilder builder = new ProcessBuilder(new String[] {"bash", "-c", command});
36 // redirect error stream to collect both output & error
37 builder.redirectErrorStream(true);
38 Process process = builder.start();
39 BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
40 String line = null;
41 String errorMessage = "";
42 while ( (line = br.readLine()) != null) {
43 String[] result = processLine(line);
44 String status = result[0];
45 String value = result[1];
46 if (status.equals(ERROR) && !value.isEmpty()) {
47 errorMessage += value;
48 continue;
49 }
50 if (!value.isEmpty())
51 values.add(value);
52 }
53 int exitVal = process.waitFor();
54
55 // if the background process did not exit with 0 code, we should set the status accordingly
56 if (exitVal != 0) {
57 bspAction.setMessage(errorMessage);
58 bspAction.setItems(null);
59 }
60 } catch (Exception e) {
61 bspAction.setMessage(e.getMessage());
62 bspAction.setItems(null);
63 }
64 if (!values.isEmpty()) {
65 bspAction.setMessage(null);
66 bspAction.setItems(values.toArray(new String[values.size()]));
67 }
68 }
69
70 /**
71 * Each command ran in the background will have a different output and a different way of processing it
72 * @param line
73 * @return
74 */
75 protected abstract String[] processLine(String line);
76
77 public BSPAction getBspAction() {
78 return bspAction;
79 }
80
81 public void setBspAction(BSPAction bspAction) {
82 this.bspAction = bspAction;
83 }
84
85 public String getCommand() {
86 return command;
87 }
88
89 public void setCommand(String command) {
90 this.command = command;
91 }
92}