KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > j2ee > db > 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.j2ee.sun.ide.j2ee.db;
26 import java.io.*;
27 import org.openide.ErrorManager;
28 import org.openide.windows.*;
29 /**
30  *
31  * @author ludo
32  */

33 public class ExecSupport {
34
35     /** Creates a new instance of ExecSupport */
36     public ExecSupport() {
37     }
38
39     /**
40      * Redirect the standard output and error streams of the child
41      * process to an output window.
42      */

43     public void displayProcessOutputs(final Process JavaDoc child, String JavaDoc displayName, String JavaDoc initialMessage)
44     throws IOException, InterruptedException JavaDoc {
45         // Get a tab on the output window. If this client has been
46
// executed before, the same tab will be returned.
47
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             // not a critical error, continue
54
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
55         }
56         io.select();
57         ow.println(initialMessage);
58         final Thread JavaDoc[] copyMakers = new Thread JavaDoc[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 JavaDoc() {
63             public void run() {
64                 try {
65                     child.waitFor();
66                     Thread.sleep(2000); // time for copymakers
67
} catch (InterruptedException JavaDoc e) {
68                 } finally {
69                     try {
70                         copyMakers[0].interrupt();
71                         copyMakers[1].interrupt();
72                         copyMakers[2].interrupt();
73                     }
74                     catch (Exception JavaDoc e) {
75                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
76                     }
77                 }
78             }
79         }.start();
80     }
81        
82     
83     
84     /** This thread simply reads from given Reader and writes read chars to given Writer. */
85     static public class OutputCopier extends Thread JavaDoc {
86         final Writer os;
87         final Reader is;
88         /** while set to false at streams that writes to the OutputWindow it must be
89          * true for a stream that reads from the window.
90          */

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         /* Makes copy. */
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 JavaDoc 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 JavaDoc, 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