diff options
author | Adrian Dudau <adrian.dudau@enea.com> | 2014-06-26 13:23:09 +0200 |
---|---|---|
committer | Adrian Dudau <adrian.dudau@enea.com> | 2014-06-26 13:24:09 +0200 |
commit | c7da892cb23d50d4d85746c9a0b6b14bf570989d (patch) | |
tree | e7136073f386d6156f51766c498c52c30c8df85f /plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/wizards/bsp/BSPThread.java | |
download | eclipse-poky-kepler-c7da892cb23d50d4d85746c9a0b6b14bf570989d.tar.gz |
initial commit for Enea Linux 4.0daisy
Migrated from the internal git server on the daisy-enea branch
Signed-off-by: Adrian Dudau <adrian.dudau@enea.com>
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.java | 92 |
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 @@ | |||
1 | package org.yocto.sdk.remotetools.wizards.bsp; | ||
2 | |||
3 | import java.io.BufferedReader; | ||
4 | import java.io.InputStreamReader; | ||
5 | import 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 | */ | ||
14 | public 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 | } | ||