1 /***
2 *
3 */
4 package de.cohesion.bssh.impl;
5
6 import java.io.BufferedReader;
7 import java.io.IOException;
8 import java.io.Reader;
9 import java.lang.Thread.State;
10
11 import de.cohesion.bssh.Command;
12 import de.cohesion.bssh.Member;
13 import de.cohesion.bssh.Result;
14 import de.cohesion.bssh.impl.lang.NotifyingThread;
15 import de.cohesion.bssh.impl.lang.ThreadStateListener;
16
17 /***
18 * @author schulzs
19 */
20 public class ResultImpl implements Result {
21
22 private static String drain(final Reader reader) throws IOException {
23 final BufferedReader br = new BufferedReader(reader);
24 StringBuilder b = new StringBuilder();
25 while (true) {
26 String line = br.readLine();
27 if (line == null) {
28 break;
29 } else {
30 if (b.length() != 0) {
31 b.append("\n");
32 }
33 }
34 b.append(line);
35 }
36 br.close();
37 return b.toString();
38 }
39
40 private String output, error;
41
42 private final Command cmd;
43
44 private final Member member;
45
46 private Exception e;
47
48 protected ResultImpl(final Member member, final Command cmd) {
49 this.member = member;
50 this.cmd = cmd;
51 }
52
53 public ResultImpl(final Member member, final Command cmd, final Exception e) {
54 this(member, cmd);
55 this.e = e;
56 }
57
58 public ResultImpl(final Member member, final Command cmd, final Reader out,
59 final Reader err) throws IOException {
60 this(member, cmd);
61 this.output = drain(out);
62 this.error = drain(err);
63 }
64
65 public String getOutput() {
66 return output;
67 }
68
69 public String getError() {
70 return error;
71 }
72
73 public Exception getException() {
74 return e;
75 }
76
77 public Command getCommand() {
78 return cmd;
79 }
80
81 public Member getMember() {
82 return member;
83 }
84
85 }