001: package org.columba.mail.spam.spamassassin;
002:
003: import java.io.BufferedReader;
004: import java.io.IOException;
005: import java.io.InputStream;
006: import java.io.InputStreamReader;
007: import java.io.OutputStream;
008:
009: import org.columba.core.io.StreamUtils;
010:
011: /*
012: * Created on 15.07.2003
013: *
014: * To change the template for this generated file go to
015: * Window>Preferences>Java>Code Generation>Code and Comments
016: */
017:
018: /**
019: * @author frd
020: *
021: * To change the template for this generated type comment go to
022: * Window>Preferences>Java>Code Generation>Code and Comments
023: */
024: public class IPCHelper {
025:
026: private static final java.util.logging.Logger LOG = java.util.logging.Logger
027: .getLogger("org.columba.mail.spam.spamassassin"); //$NON-NLS-1$
028:
029: protected StreamThread outputStream = null;
030: protected StreamThread errorStream = null;
031: protected OutputStream inputStream = null;
032: protected Process p;
033:
034: public IPCHelper() {
035: }
036:
037: /**
038: *
039: * execute command
040: *
041: * initialize streams
042: *
043: * @param command
044: * @throws Exception
045: */
046: public void executeCommand(String command) throws Exception {
047: p = Runtime.getRuntime().exec(command);
048:
049: errorStream = new StreamThread(p.getErrorStream());
050: outputStream = new StreamThread(p.getInputStream());
051: inputStream = p.getOutputStream();
052:
053: errorStream.start();
054: outputStream.start();
055: }
056:
057: public void send(String in) throws Exception {
058: inputStream.write(in.getBytes());
059: inputStream.flush();
060: inputStream.close();
061: }
062:
063: public void send(InputStream in) throws Exception {
064: StreamUtils.streamCopy(in, inputStream);
065: inputStream.flush();
066: inputStream.close();
067: }
068:
069: public int waitFor() throws Exception {
070: int exitVal = p.waitFor();
071:
072: return exitVal;
073: }
074:
075: /**
076: *
077: * return error
078: *
079: * @return @throws
080: * Exception
081: */
082: public String getErrorString() throws Exception {
083: String str = errorStream.getBuffer();
084:
085: return str;
086: }
087:
088: /**
089: *
090: * return output
091: *
092: * @return @throws
093: * Exception
094: */
095: public String getOutputString() throws Exception {
096: String str = outputStream.getBuffer();
097:
098: return str;
099: }
100:
101: /*
102: * wait for stream threads to die
103: *
104: */
105: public void waitForThreads() throws Exception {
106: outputStream.join();
107: errorStream.join();
108: }
109:
110: public class StreamThread extends Thread {
111: InputStream is;
112: StringBuffer buf;
113:
114: public StreamThread(InputStream is) {
115: this .is = is;
116:
117: buf = new StringBuffer();
118: }
119:
120: public void run() {
121: try {
122: InputStreamReader isr = new InputStreamReader(is);
123: BufferedReader br = new BufferedReader(isr);
124: String 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 ioe) {
131: ioe.printStackTrace();
132: }
133: }
134:
135: public String getBuffer() {
136: return buf.toString();
137: }
138: }
139: }
|