1 package org.columba.mail.spam.spamassassin; 2 import java.io.BufferedReader ; 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.io.InputStreamReader ; 6 import java.io.OutputStream ; 7 8 import org.columba.core.io.StreamUtils; 9 10 11 17 18 24 public class IPCHelper { 25 26 private static final java.util.logging.Logger LOG = 27 java.util.logging.Logger.getLogger("org.columba.mail.spam.spamassassin"); 29 protected StreamThread outputStream = null; 30 protected StreamThread errorStream = null; 31 protected OutputStream inputStream = null; 32 protected Process p; 33 34 public IPCHelper() { 35 } 36 37 46 public void executeCommand(String command) throws Exception { 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 in) throws Exception { 58 inputStream.write(in.getBytes()); 59 inputStream.flush(); 60 inputStream.close(); 61 } 62 63 public void send(InputStream in) throws Exception { 64 StreamUtils.streamCopy(in, inputStream); 65 inputStream.flush(); 66 inputStream.close(); 67 } 68 69 public int waitFor() throws Exception { 70 int exitVal = p.waitFor(); 71 72 return exitVal; 73 } 74 75 82 public String getErrorString() throws Exception { 83 String str = errorStream.getBuffer(); 84 85 return str; 86 } 87 88 95 public String getOutputString() throws Exception { 96 String str = outputStream.getBuffer(); 97 98 return str; 99 } 100 101 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); 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 } 140 | Popular Tags |