1 | public class IPCHelper {↵ | | 1 | public class IPCHelper {↵
|
|
2 | ↵ | | 2 | ↵
|
3 | static final java.util.logging.Logger LOG = ↵ | | 3 | private static final java.util.logging.Logger LOG = ↵
|
4 | java.util.logging.Logger↵ | | 4 | java.util.logging.Logger↵
|
5 | .getLogger("org.columba.core.base"); //$NON-NLS-1$↵ | | 5 | .getLogger("org.columba.mail.spam.spamassassin"); //$NON-NLS-1$↵
|
| | | 6 | ↵
|
6 | protected StreamThread outputStream = null;↵ | | 7 | protected StreamThread outputStream = null;↵
|
|
7 | protected StreamThread errorStream = null;↵ | | 8 | protected StreamThread errorStream = null;↵
|
|
8 | protected OutputStream inputStream = null;↵ | | 9 | protected OutputStream inputStream = null;↵
|
|
9 | protected Process p;↵ | | 10 | protected Process p;↵
|
|
10 | public IPCHelper() {↵ | | 11 | public IPCHelper() {↵
|
11 | // nothing to do↵ | | |
|
12 | }↵ | | 12 | }↵
|
|
13 | /**↵ | | 13 | /**↵
|
14 | * ↵ | | 14 | ↵
|
15 | ↵ | | 15 | *↵
|
16 | * execute command↵ | | 16 | * execute command↵
|
17 | * ↵ | | 17 | ↵
|
18 | ↵ | | 18 | *↵
|
19 | * initialize streams↵ | | 19 | * initialize streams↵
|
20 | * ↵ | | 20 | *↵
|
21 | * @param command↵ | | 21 | * @param command↵
|
22 | * @throws Exception↵ | | 22 | * @throws Exception↵
|
23 | */↵ | | 23 | */↵
|
24 | public void executeCommand(String[] command) throws Exception {↵ | | 24 | public void executeCommand(String command) throws Exception {↵
|
25 | this.p = Runtime.getRuntime().exec(command);↵ | | 25 | p = Runtime.getRuntime().exec(command);↵
|
|
26 | errorStream = new StreamThread(p.getErrorStream());↵ | | 26 | errorStream = new StreamThread(p.getErrorStream());↵
|
27 | outputStream = new StreamThread(p.getInputStream());↵ | | 27 | outputStream = new StreamThread(p.getInputStream());↵
|
28 | inputStream = p.getOutputStream();↵ | | 28 | inputStream = p.getOutputStream();↵
|
|
29 | errorStream.start();↵ | | 29 | errorStream.start();↵
|
30 | outputStream.start();↵ | | 30 | outputStream.start();↵
|
31 | }↵ | | 31 | }↵
|
|
32 | public void send(String in) throws Exception {↵ | | 32 | public void send(String in) throws Exception {↵
|
33 | inputStream.write(in.getBytes());↵ | | 33 | inputStream.write(in.getBytes());↵
|
34 | inputStream.flush();↵ | | 34 | inputStream.flush();↵
|
35 | inputStream.close();↵ | | 35 | inputStream.close();↵
|
36 | }↵ | | 36 | }↵
|
|
37 | public void send(InputStream in) throws Exception {↵ | | 37 | public void send(InputStream in) throws Exception {↵
|
38 | StreamUtils.streamCopy(in, inputStream);↵ | | 38 | StreamUtils.streamCopy(in, inputStream);↵
|
39 | inputStream.flush();↵ | | 39 | inputStream.flush();↵
|
40 | inputStream.close();↵ | | 40 | inputStream.close();↵
|
41 | }↵ | | 41 | }↵
|
|
42 | public int waitFor() throws Exception {↵ | | 42 | public int waitFor() throws Exception {↵
|
43 | int exitVal = p.waitFor();↵ | | 43 | int exitVal = p.waitFor();↵
|
|
44 | return exitVal;↵ | | 44 | return exitVal;↵
|
45 | }↵ | | 45 | }↵
|
|
46 | /**↵ | | 46 | /**↵
|
47 | * ↵ | | 47 | ↵
|
48 | ↵ | | 48 | *↵
|
49 | * return error↵ | | 49 | * return error↵
|
50 | * ↵ | | 50 | ↵
|
51 | ↵ | | 51 | *↵
|
52 | * @return↵ | | 52 | * @return↵
|
53 | * @throws↵ | | 53 | @throws↵
|
54 | Exception↵ | | 54 | * Exception↵
|
55 | */↵ | | 55 | */↵
|
56 | public String getErrorString() throws Exception {↵ | | 56 | public String getErrorString() throws Exception {↵
|
57 | String str = errorStream.getBuffer();↵ | | 57 | String str = errorStream.getBuffer();↵
|
|
58 | return str;↵ | | 58 | return str;↵
|
59 | }↵ | | 59 | }↵
|
|
60 | /**↵ | | 60 | /**↵
|
61 | * ↵ | | 61 | ↵
|
62 | ↵ | | 62 | *↵
|
63 | * return output↵ | | 63 | * return output↵
|
64 | * ↵ | | 64 | *↵
|
65 | * @return↵ | | 65 | * @return @throws↵
|
66 | * @throws Exception↵ | | 66 | * Exception↵
|
67 | */↵ | | 67 | */↵
|
68 | public String getOutputString() throws Exception {↵ | | 68 | public String getOutputString() throws Exception {↵
|
69 | String str = outputStream.getBuffer();↵ | | 69 | String str = outputStream.getBuffer();↵
|
|
70 | return str;↵ | | 70 | return str;↵
|
71 | }↵ | | 71 | }↵
|
|
72 | /*↵ | | 72 | /*↵
|
73 | * wait for stream threads to die↵ | | 73 | * wait for stream threads to die↵
|
74 | * ↵ | | 74 | ↵
|
75 | ↵ | | 75 | *↵
|
76 | */↵ | | 76 | */↵
|
77 | public void waitForThreads() throws Exception {↵ | | 77 | public void waitForThreads() throws Exception {↵
|
78 | outputStream.join();↵ | | 78 | outputStream.join();↵
|
79 | errorStream.join();↵ | | 79 | errorStream.join();↵
|
80 | }↵ | | 80 | }↵
|
|
81 | public class StreamThread extends Thread {↵ | | 81 | public class StreamThread extends Thread {↵
|
82 | InputStream is;↵ | | 82 | InputStream is;↵
|
|
83 | StringBuffer buf;↵ | | 83 | StringBuffer buf;↵
|
|
84 | public StreamThread(InputStream theInputStream) {↵ | | 84 | public StreamThread(InputStream ↵
|
85 | this.is = theInputStream;↵ | | |
|
|
86 | ↵ | | 85 | is) {↵
|
| | | 86 | this.is = is;↵
|
|
87 | buf = new StringBuffer();↵ | | 87 | buf = new StringBuffer();↵
|
88 | }↵ | | |
|
|
89 | @Override↵ | | |
|
90 | ↵ | | 88 | }↵
|
|
91 | public void run() {↵ | | 89 | public void run() {↵
|
92 | try {↵ | | 90 | try {↵
|
93 | InputStreamReader isr = new InputStreamReader(is);↵ | | 91 | InputStreamReader isr = new InputStreamReader(is);↵
|
94 | BufferedReader br = new BufferedReader(isr);↵ | | 92 | BufferedReader br = new BufferedReader(isr);↵
|
95 | String line = null;↵ | | 93 | String line = null;↵
|
|
96 | while ((line = br.readLine()) != null) {↵ | | 94 | while ((line = br.readLine()) != null) {↵
|
97 | LOG.info(">" + line); //$NON-NLS-1$↵ | | 95 | LOG.info(">" + line); //$NON-NLS-1$↵
|
98 | buf.append(line + "\n"); //$NON-NLS-1$↵ | | 96 | buf.append(line + "\n");↵
|
99 | }↵ | | |
|
100 | ↵ | | |
|
| | | 97 | }↵
|
101 | } catch (IOException ioe) {↵ | | 98 | } catch (IOException ioe) {↵
|
102 | ioe.printStackTrace();↵ | | 99 | ioe.printStackTrace();↵
|
103 | }↵ | | |
|
104 | }↵ | | |
|
|
105 | ↵ | | 100 | }↵
|
| | | 101 | }↵
|
|
106 | public String getBuffer() {↵ | | 102 | public String getBuffer() {↵
|
107 | return buf.toString();↵ | | 103 | return buf.toString();↵
|
108 | | | 104 |
|