From 41ac47d732eed8392d60d0f6773e5a279d49b999 Mon Sep 17 00:00:00 2001 From: Adrian Dudau Date: Thu, 12 Dec 2013 13:36:50 +0100 Subject: initial commit of Enea Linux 3.1 Migrated from the internal git server on the dora-enea branch Signed-off-by: Adrian Dudau --- .../sdk/remotetools/wizards/bsp/BSPThread.java | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/wizards/bsp/BSPThread.java (limited to 'plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/wizards/bsp/BSPThread.java') 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 @@ +package org.yocto.sdk.remotetools.wizards.bsp; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayList; + +/** + * Receives a command to be run on a separate thread in the background + * It contains an BSPAction object that will collect the output & error + * Output lines are processed and collected into the items of BSPAction + * @author ioana.grigoropol + * + */ +public abstract class BSPThread implements Runnable { + public static final String SUCCESS = "success"; + public static final String ERROR = "error"; + + private BSPAction bspAction; + private String command; + + /** + * Receives the command to be run in the background + * @param command + */ + public BSPThread(String command) { + this.command = command; + this.bspAction = new BSPAction(null, null); + } + + @Override + public void run() { + ArrayList values = new ArrayList(); + + try { + ProcessBuilder builder = new ProcessBuilder(new String[] {"bash", "-c", command}); + // redirect error stream to collect both output & error + builder.redirectErrorStream(true); + Process process = builder.start(); + BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line = null; + String errorMessage = ""; + while ( (line = br.readLine()) != null) { + String[] result = processLine(line); + String status = result[0]; + String value = result[1]; + if (status.equals(ERROR) && !value.isEmpty()) { + errorMessage += value; + continue; + } + if (!value.isEmpty()) + values.add(value); + } + int exitVal = process.waitFor(); + + // if the background process did not exit with 0 code, we should set the status accordingly + if (exitVal != 0) { + bspAction.setMessage(errorMessage); + bspAction.setItems(null); + } + } catch (Exception e) { + bspAction.setMessage(e.getMessage()); + bspAction.setItems(null); + } + if (!values.isEmpty()) { + bspAction.setMessage(null); + bspAction.setItems(values.toArray(new String[values.size()])); + } + } + + /** + * Each command ran in the background will have a different output and a different way of processing it + * @param line + * @return + */ + protected abstract String[] processLine(String line); + + public BSPAction getBspAction() { + return bspAction; + } + + public void setBspAction(BSPAction bspAction) { + this.bspAction = bspAction; + } + + public String getCommand() { + return command; + } + + public void setCommand(String command) { + this.command = command; + } +} -- cgit v1.2.3-54-g00ecf