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