001: package org.columba.core.base;
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: static final java.util.logging.Logger LOG = java.util.logging.Logger
027: .getLogger("org.columba.core.base"); //$NON-NLS-1$
028:
029: protected StreamThread outputStream = null;
030:
031: protected StreamThread errorStream = null;
032:
033: protected OutputStream inputStream = null;
034:
035: protected Process p;
036:
037: public IPCHelper() {
038: // nothing to do
039: }
040:
041: /**
042: *
043: * execute command
044: *
045: * initialize streams
046: *
047: * @param command
048: * @throws Exception
049: */
050: public void executeCommand(String[] command) throws Exception {
051: this .p = Runtime.getRuntime().exec(command);
052:
053: errorStream = new StreamThread(p.getErrorStream());
054: outputStream = new StreamThread(p.getInputStream());
055: inputStream = p.getOutputStream();
056:
057: errorStream.start();
058: outputStream.start();
059: }
060:
061: public void send(String in) throws Exception {
062: inputStream.write(in.getBytes());
063: inputStream.flush();
064: inputStream.close();
065: }
066:
067: public void send(InputStream in) throws Exception {
068: StreamUtils.streamCopy(in, inputStream);
069: inputStream.flush();
070: inputStream.close();
071: }
072:
073: public int waitFor() throws Exception {
074: int exitVal = p.waitFor();
075:
076: return exitVal;
077: }
078:
079: /**
080: *
081: * return error
082: *
083: * @return
084: * @throws Exception
085: */
086: public String getErrorString() throws Exception {
087: String str = errorStream.getBuffer();
088:
089: return str;
090: }
091:
092: /**
093: *
094: * return output
095: *
096: * @return
097: * @throws Exception
098: */
099: public String getOutputString() throws Exception {
100: String 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 {
110: outputStream.join();
111: errorStream.join();
112: }
113:
114: public class StreamThread extends Thread {
115: InputStream is;
116:
117: StringBuffer buf;
118:
119: public StreamThread(InputStream theInputStream) {
120: this .is = theInputStream;
121:
122: buf = new StringBuffer();
123: }
124:
125: @Override
126: public void run() {
127: try {
128: InputStreamReader isr = new InputStreamReader(is);
129: BufferedReader br = new BufferedReader(isr);
130: String 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 ioe) {
137: ioe.printStackTrace();
138: }
139: }
140:
141: public String getBuffer() {
142: return buf.toString();
143: }
144: }
145: }
|