KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > derby > ExecSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * ExecSupport.java
21  *
22  * Created on March 5, 2004, 12:57 PM
23  */

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 /**
32  *
33  * @author ludo
34  */

35 public class ExecSupport {
36
37     private String JavaDoc lookFor;
38     private OutputCopier[] copyMakers;
39
40     /** Creates a new instance of ExecSupport */
41     public ExecSupport() {
42     }
43
44     public void setStringToLookFor(String JavaDoc 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     /**
57      * Redirect the standard output and error streams of the child
58      * process to an output window.
59      */

60     public void displayProcessOutputs(final Process JavaDoc child, String JavaDoc displayName)
61     throws IOException, InterruptedException JavaDoc {
62         // Get a tab on the output window. If this client has been
63
// executed before, the same tab will be returned.
64
InputOutput io = org.openide.windows.IOProvider.getDefault().getIO(
65             displayName, false);
66         try {
67             io.getOut().reset();
68         }
69         catch (IOException e) {
70             // not a critical error, continue
71
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 JavaDoc() {
79             public void run() {
80                 try {
81                     int ret = child.waitFor();
82                     Thread.sleep(2000); // time for copymakers
83
} catch (InterruptedException JavaDoc e) {
84                 } finally {
85                     try {
86                         copyMakers[0].interrupt();
87                         copyMakers[1].interrupt();
88                         copyMakers[2].interrupt();
89                     }
90                     catch (Exception JavaDoc e) {
91                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
92                     }
93                 }
94             }
95         }.start();
96     }
97        
98     
99     
100     /** This thread simply reads from given Reader and writes read chars to given Writer. */
101     static public class OutputCopier extends Thread JavaDoc {
102         final Writer os;
103         final Reader is;
104         /** while set to false at streams that writes to the OutputWindow it must be
105          * true for a stream that reads from the window.
106          */

107         final boolean autoflush;
108         private boolean done = false;
109         private String JavaDoc 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 JavaDoc 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         /* Makes copy. */
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 JavaDoc 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 JavaDoc, IOException {
160             
161             while (!is.ready() && !done) sleep(100);
162             
163             return is.read(buff, start, count);
164         }
165
166     }
167
168     /** Waits for startup of a server, waits until the message set through the setStringToLookFor() method.
169      * @param progressMessage message to be displayed in the progress bar. If null, no progress bar is shown.
170      * @param timeout timeout
171      * @return true if the connection was successfully established, false if timed out
172      */

173     public boolean waitForMessage(String JavaDoc 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 JavaDoc t = new Thread JavaDoc(connect);
183             t.start();
184             try {
185                 t.join(timeout);
186             } catch(InterruptedException JavaDoc ie) {
187             }
188             if (t.isAlive()) {
189                 connect.finishLoop();
190                 t.interrupt();//for thread deadlock
191
}
192             return connect.getStatus();
193         }
194         finally {
195             if (ph != null)
196                 ph.finish();
197         }
198     }
199     
200     private class Connect implements Runnable JavaDoc {
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 JavaDoc ie) {
223                 }
224             }
225         }
226
227         boolean getStatus() {
228             return status;
229         }
230     }
231     
232     
233     
234 }
235
Popular Tags