From: Dejan K. <de...@us...> - 2004-12-23 09:47:30
|
Update of /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core/pipeline/stage In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3395/babeldoc/modules/core/src/com/babeldoc/core/pipeline/stage Modified Files: ExternalApplicationPipelineStage.java Log Message: Fixing some problems when running some kind of applications. Implementing tips explained in http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps_p.html Index: ExternalApplicationPipelineStage.java =================================================================== RCS file: /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core/pipeline/stage/ExternalApplicationPipelineStage.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** ExternalApplicationPipelineStage.java 30 Jul 2004 01:33:04 -0000 1.7 --- ExternalApplicationPipelineStage.java 23 Dec 2004 09:47:20 -0000 1.8 *************** *** 67,70 **** --- 67,71 ---- import com.babeldoc.core.I18n; + import com.babeldoc.core.LogService; import com.babeldoc.core.option.ConfigOption; import com.babeldoc.core.option.IConfigOptionType; *************** *** 73,76 **** --- 74,78 ---- import java.io.BufferedReader; import java.io.IOException; + import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; *************** *** 168,182 **** os.close(); } ! ! if (pipeIn) { ! StringBuffer response = new StringBuffer(); ! String line; ! BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); ! while ((line = in.readLine()) != null) { ! System.out.println(line); ! response.append(line); ! response.append("\n"); ! } ! this.getDocument().put("ExternalApplicationResponse", new String(response)); } } catch (IOException e) { --- 170,189 ---- os.close(); } ! StreamHandler stdout = new StreamHandler(process.getInputStream()); ! StreamHandler stderr = new StreamHandler(process.getInputStream()); ! try { ! stdout.start(); ! stderr.start(); ! int status = process.waitFor(); //we should wait for process completition ! while (! stdout.isFinished() || !stderr.isFinished()) { ! Thread.sleep(50); ! } ! } catch (InterruptedException e) { ! e.printStackTrace(); //TODO: handle this ! } ! ! if (pipeIn) { ! this.getDocument().put("ExternalApplicationResponse", stdout.getOutput()); ! this.getDocument().put("ExternalApplicationError", stderr.getOutput()); } } catch (IOException e) { *************** *** 187,188 **** --- 194,229 ---- } } + + class StreamHandler extends Thread { + InputStream is; + + + private volatile boolean finished = false; + private StringBuffer output; + StreamHandler(InputStream is) { + this.is = is; + this.output = new StringBuffer(); + } + + public void run() { + try { + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + String line = null; + while ((line = br.readLine()) != null) { + output.append(line).append("\n"); + } + } catch (IOException ioe) { + ioe.printStackTrace(); + } + finished = true; + } + + public boolean isFinished() { + return finished; + } + public String getOutput() { + return output.toString(); + } + + } \ No newline at end of file |