1 19 24 25 package org.netbeans.modules.derby; 26 import java.io.*; 27 import org.netbeans.api.progress.ProgressHandle; 28 import org.netbeans.api.progress.ProgressHandleFactory; 29 import org.openide.ErrorManager; 30 import org.openide.windows.*; 31 35 public class ExecSupport { 36 37 private String lookFor; 38 private OutputCopier[] copyMakers; 39 40 41 public ExecSupport() { 42 } 43 44 public void setStringToLookFor(String lookFor) { 45 this.lookFor = lookFor; 46 } 47 48 public boolean isStringFound() { 49 if (copyMakers == null) 50 return false; 51 return (copyMakers[0].stringFound() || 52 copyMakers[1].stringFound() || 53 copyMakers[2].stringFound()); 54 } 55 56 60 public void displayProcessOutputs(final Process child, String displayName) 61 throws IOException, InterruptedException { 62 InputOutput io = org.openide.windows.IOProvider.getDefault().getIO( 65 displayName, false); 66 try { 67 io.getOut().reset(); 68 } 69 catch (IOException e) { 70 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 72 } 73 io.select(); 74 copyMakers = new OutputCopier[3]; 75 (copyMakers[0] = new OutputCopier(new InputStreamReader(child.getInputStream()), io.getOut(), true, lookFor)).start(); 76 (copyMakers[1] = new OutputCopier(new InputStreamReader(child.getErrorStream()), io.getErr(), true, lookFor)).start(); 77 (copyMakers[2] = new OutputCopier(io.getIn(), new OutputStreamWriter(child.getOutputStream()), true)).start(); 78 new Thread () { 79 public void run() { 80 try { 81 int ret = child.waitFor(); 82 Thread.sleep(2000); } catch (InterruptedException e) { 84 } finally { 85 try { 86 copyMakers[0].interrupt(); 87 copyMakers[1].interrupt(); 88 copyMakers[2].interrupt(); 89 } 90 catch (Exception e) { 91 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 92 } 93 } 94 } 95 }.start(); 96 } 97 98 99 100 101 static public class OutputCopier extends Thread { 102 final Writer os; 103 final Reader is; 104 107 final boolean autoflush; 108 private boolean done = false; 109 private String stringToLookFor; 110 private boolean stringFound = false; 111 112 113 private static final int FOUND = SearchUtil.FOUND; 114 115 public OutputCopier(Reader is, Writer os, boolean b, String lookFor) { 116 this.os = os; 117 this.is = is; 118 autoflush = b; 119 this.stringToLookFor = lookFor; 120 } 121 122 public OutputCopier(Reader is, Writer os, boolean b) { 123 this(is, os, b, null); 124 } 125 126 public boolean stringFound() { 127 return stringFound; 128 } 129 130 131 public void run() { 132 int read; 133 int stringFoundChars = 0; 134 char[] buff = new char [256]; 135 try { 136 while ((read = read(is, buff, 0, 256)) > 0x0) { 137 if (stringToLookFor != null) { 138 stringFoundChars = SearchUtil.checkForString(stringToLookFor, stringFoundChars, buff, read); 139 if (stringFoundChars == FOUND) { 140 stringToLookFor = null; 141 stringFound = true; 142 } 143 } 144 if (os!=null){ 145 os.write(buff,0,read); 146 if (autoflush) os.flush(); 147 } 148 } 149 } catch (IOException ex) { 150 } catch (InterruptedException e) { 151 } 152 } 153 154 public void interrupt() { 155 super.interrupt(); 156 done = true; 157 } 158 159 private int read(Reader is, char[] buff, int start, int count) throws InterruptedException , IOException { 160 161 while (!is.ready() && !done) sleep(100); 162 163 return is.read(buff, start, count); 164 } 165 166 } 167 168 173 public boolean waitForMessage(String progressMessage, int timeout) { 174 int retryTime = 10; 175 ProgressHandle ph = null; 176 if (progressMessage != null) { 177 ph = ProgressHandleFactory.createHandle(progressMessage); 178 ph.start(); 179 } 180 try { 181 Connect connect = new Connect(retryTime); 182 Thread t = new Thread (connect); 183 t.start(); 184 try { 185 t.join(timeout); 186 } catch(InterruptedException ie) { 187 } 188 if (t.isAlive()) { 189 connect.finishLoop(); 190 t.interrupt(); } 192 return connect.getStatus(); 193 } 194 finally { 195 if (ph != null) 196 ph.finish(); 197 } 198 } 199 200 private class Connect implements Runnable { 201 202 int retryTime; 203 boolean status = false; 204 boolean loop = true; 205 206 public Connect(int retryTime) { 207 this.retryTime = retryTime; 208 } 209 210 public void finishLoop() { 211 loop = false; 212 } 213 214 public void run() { 215 while (loop) { 216 if (isStringFound()) { 217 status = true; 218 break; 219 } 220 try { 221 Thread.currentThread().sleep(retryTime); 222 } catch(InterruptedException ie) { 223 } 224 } 225 } 226 227 boolean getStatus() { 228 return status; 229 } 230 } 231 232 233 234 } 235 | Popular Tags |