KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > base > IPCHelper


1 package org.columba.core.base;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.InputStreamReader JavaDoc;
7 import java.io.OutputStream JavaDoc;
8
9 import org.columba.core.io.StreamUtils;
10
11 /*
12  * Created on 15.07.2003
13  *
14  * To change the template for this generated file go to
15  * Window>Preferences>Java>Code Generation>Code and Comments
16  */

17
18 /**
19  * @author frd
20  *
21  * To change the template for this generated type comment go to
22  * Window>Preferences>Java>Code Generation>Code and Comments
23  */

24 public class IPCHelper {
25
26     static final java.util.logging.Logger JavaDoc LOG = java.util.logging.Logger
27         .getLogger("org.columba.core.base"); //$NON-NLS-1$
28

29     protected StreamThread outputStream = null;
30
31     protected StreamThread errorStream = null;
32
33     protected OutputStream JavaDoc inputStream = null;
34
35     protected Process JavaDoc p;
36
37     public IPCHelper() {
38     // nothing to do
39
}
40
41     /**
42          *
43          * execute command
44          *
45          * initialize streams
46          *
47          * @param command
48          * @throws Exception
49          */

50     public void executeCommand(String JavaDoc[] command) throws Exception JavaDoc {
51     this.p = Runtime.getRuntime().exec(command);
52
53     errorStream = new StreamThread(p.getErrorStream());
54     outputStream = new StreamThread(p.getInputStream());
55     inputStream = p.getOutputStream();
56
57     errorStream.start();
58     outputStream.start();
59     }
60
61     public void send(String JavaDoc in) throws Exception JavaDoc {
62     inputStream.write(in.getBytes());
63     inputStream.flush();
64     inputStream.close();
65     }
66
67     public void send(InputStream JavaDoc in) throws Exception JavaDoc {
68     StreamUtils.streamCopy(in, inputStream);
69     inputStream.flush();
70     inputStream.close();
71     }
72
73     public int waitFor() throws Exception JavaDoc {
74     int exitVal = p.waitFor();
75
76     return exitVal;
77     }
78
79     /**
80          *
81          * return error
82          *
83          * @return
84          * @throws Exception
85          */

86     public String JavaDoc getErrorString() throws Exception JavaDoc {
87     String JavaDoc str = errorStream.getBuffer();
88
89     return str;
90     }
91
92     /**
93          *
94          * return output
95          *
96          * @return
97          * @throws Exception
98          */

99     public String JavaDoc getOutputString() throws Exception JavaDoc {
100     String JavaDoc str = outputStream.getBuffer();
101
102     return str;
103     }
104
105     /*
106          * wait for stream threads to die
107          *
108          */

109     public void waitForThreads() throws Exception JavaDoc {
110     outputStream.join();
111     errorStream.join();
112     }
113
114     public class StreamThread extends Thread JavaDoc {
115     InputStream JavaDoc is;
116
117     StringBuffer JavaDoc buf;
118
119     public StreamThread(InputStream JavaDoc theInputStream) {
120         this.is = theInputStream;
121
122         buf = new StringBuffer JavaDoc();
123     }
124
125     @Override JavaDoc
126     public void run() {
127         try {
128         InputStreamReader JavaDoc isr = new InputStreamReader JavaDoc(is);
129         BufferedReader JavaDoc br = new BufferedReader JavaDoc(isr);
130         String JavaDoc line = null;
131
132         while ((line = br.readLine()) != null) {
133             LOG.info(">" + line); //$NON-NLS-1$
134
buf.append(line + "\n"); //$NON-NLS-1$
135
}
136         } catch (IOException JavaDoc ioe) {
137         ioe.printStackTrace();
138         }
139     }
140
141     public String JavaDoc getBuffer() {
142         return buf.toString();
143     }
144     }
145 }
146
Popular Tags