1 19 24 25 package org.netbeans.modules.j2ee.sun.ide.j2ee.db; 26 import java.io.*; 27 import org.openide.ErrorManager; 28 import org.openide.windows.*; 29 33 public class ExecSupport { 34 35 36 public ExecSupport() { 37 } 38 39 43 public void displayProcessOutputs(final Process child, String displayName, String initialMessage) 44 throws IOException, InterruptedException { 45 InputOutput io = org.openide.windows.IOProvider.getDefault().getIO( displayName, false); 48 OutputWriter ow = io.getOut(); 49 try { 50 io.getOut().reset(); 51 } 52 catch (IOException e) { 53 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 55 } 56 io.select(); 57 ow.println(initialMessage); 58 final Thread [] copyMakers = new Thread [3]; 59 (copyMakers[0] = new OutputCopier(new InputStreamReader(child.getInputStream()), ow, true)).start(); 60 (copyMakers[1] = new OutputCopier(new InputStreamReader(child.getErrorStream()), io.getErr(), true)).start(); 61 (copyMakers[2] = new OutputCopier(io.getIn(), new OutputStreamWriter(child.getOutputStream()), true)).start(); 62 new Thread () { 63 public void run() { 64 try { 65 child.waitFor(); 66 Thread.sleep(2000); } catch (InterruptedException e) { 68 } finally { 69 try { 70 copyMakers[0].interrupt(); 71 copyMakers[1].interrupt(); 72 copyMakers[2].interrupt(); 73 } 74 catch (Exception e) { 75 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 76 } 77 } 78 } 79 }.start(); 80 } 81 82 83 84 85 static public class OutputCopier extends Thread { 86 final Writer os; 87 final Reader is; 88 91 final boolean autoflush; 92 private boolean done = false; 93 94 public OutputCopier(Reader is, Writer os, boolean b) { 95 this.os = os; 96 this.is = is; 97 autoflush = b; 98 } 99 100 101 public void run() { 102 int read; 103 char[] buff = new char [256]; 104 try { 105 while ((read = read(is, buff, 0, 256)) > 0x0) { 106 if (os!=null){ 107 os.write(buff,0,read); 108 if (autoflush) { 109 os.flush(); 110 } 111 } 112 } 113 } catch (IOException ex) { 114 } catch (InterruptedException e) { 115 } 116 } 117 118 public void interrupt() { 119 super.interrupt(); 120 done = true; 121 } 122 123 private int read(Reader is, char[] buff, int start, int count) throws InterruptedException , IOException { 124 125 while (!is.ready() && !done) sleep(100); 126 127 return is.read(buff, start, count); 128 } 129 } 130 131 132 } 133 | Popular Tags |