KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > spam > spamassassin > IPCHelper


1 package org.columba.mail.spam.spamassassin;
2 import java.io.BufferedReader JavaDoc;
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.io.InputStreamReader JavaDoc;
6 import java.io.OutputStream JavaDoc;
7
8 import org.columba.core.io.StreamUtils;
9
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     private static final java.util.logging.Logger JavaDoc LOG =
27         java.util.logging.Logger.getLogger("org.columba.mail.spam.spamassassin"); //$NON-NLS-1$
28

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

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

82     public String JavaDoc getErrorString() throws Exception JavaDoc {
83         String JavaDoc str = errorStream.getBuffer();
84
85         return str;
86     }
87
88     /**
89      *
90      * return output
91      *
92      * @return @throws
93      * Exception
94      */

95     public String JavaDoc getOutputString() throws Exception JavaDoc {
96         String JavaDoc str = outputStream.getBuffer();
97
98         return str;
99     }
100
101     /*
102      * wait for stream threads to die
103      *
104      */

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