KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > rubyproject > execution > OutputForwarder


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 package org.netbeans.modules.ruby.rubyproject.execution;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.lang.InterruptedException JavaDoc;
26 import java.util.List JavaDoc;
27 import org.netbeans.modules.ruby.rubyproject.execution.OutputRecognizer.FileLocation;
28 import org.netbeans.modules.ruby.rubyproject.execution.OutputRecognizer.RecognizedOutput;
29 import org.openide.ErrorManager;
30 import org.openide.windows.OutputWriter;
31
32
33 /**
34  * <p>An OutputForwarder takes output for example from a child process and
35  * pumps it into a Program I/O window.
36  * In other words it "redirects" or forwards output from a child process.
37  * The forwarder also tokenizes the output into lines, and runs
38  * {@link OutputRecognizer} objects on the output, which for example may
39  * create hyperlinks or record state regarding the output.
40  * <p>
41  * Great care is taken to make this forwarding interruptible, such that it
42  * won't block if process execution is terminated or interrupted. To do this,
43  * it's generally going to sleep for short intervals when there is no available
44  * I/O rather than calling a blocking I/O operation.
45  *
46  *
47  * @author Tor Norbye
48  */

49 class OutputForwarder implements Runnable JavaDoc {
50     private StopAction stopAction;
51     private InputStream JavaDoc str;
52     private OutputWriter writer;
53     private FileLocator fileLocator;
54     private List JavaDoc<OutputRecognizer> recognizers;
55     private String JavaDoc role;
56
57     public OutputForwarder(InputStream JavaDoc instream, OutputWriter out, FileLocator fileLocator,
58         List JavaDoc<OutputRecognizer> recognizers, StopAction stopAction, String JavaDoc role) {
59         str = instream;
60         writer = out;
61         this.fileLocator = fileLocator;
62         this.recognizers = recognizers;
63         this.stopAction = stopAction;
64         this.role = role;
65     }
66
67     public void processLine(String JavaDoc line) throws IOException JavaDoc {
68         if (fileLocator == null) {
69             writer.println(line);
70         } else {
71             FileLocation location = null;
72
73             for (OutputRecognizer recognizer : recognizers) {
74                 RecognizedOutput loc = recognizer.processLine(line);
75
76                 if (loc instanceof FileLocation) {
77                     location = (FileLocation)loc;
78
79                     // Keep processing to give all processors a chance
80
// to interpret the output even if they are not
81
// providing a file location. (The Webrick listener
82
// for examples records whether a port conflict message
83
// is seen.)
84
} // TODO: Handle other forms of RecognizedOutput
85
}
86
87             if (location != null) {
88                 writer.println(line, new OutputProcessor(location.file, location.line, fileLocator));
89             } else {
90                 writer.println(line);
91             }
92         }
93     }
94
95     public void run() {
96         BufferedReader JavaDoc read = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(str), 1200);
97
98         stopAction.addReader(read);
99
100         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
101
102         try {
103             while (true) {
104                 if (!read.ready()) {
105                     try {
106                         Thread.sleep(50);
107                     } catch (InterruptedException JavaDoc ie) {
108                         return;
109                     }
110
111                     if (stopAction.process == null) {
112                         // process finished
113
return;
114                     }
115                     
116                     if (!read.ready() && sb.length() > 0) {
117                         // Some output has been written - not a complete line
118
// and the process seems to be stalling so emit what
119
// we've got.
120
String JavaDoc line = sb.toString();
121                         sb.setLength(0);
122
123                         writer.print(line);
124                     }
125
126                     continue;
127                 }
128
129                 while (read.ready()) {
130                     int c = -1;
131                     c = read.read();
132
133                     if (c == -1) {
134                         String JavaDoc line = sb.toString();
135                         sb.setLength(0);
136                         
137                         processLine(line);
138
139                         return;
140                     } else if (c == '\n') {
141                         String JavaDoc line = sb.toString();
142                         sb.setLength(0);
143
144                         processLine(line);
145
146                         try {
147                             // writer.flush();
148
} catch (Exception JavaDoc e) {
149                             // Happens on kill
150
return;
151                         }
152                     } else {
153                         sb.append((char)c);
154                     }
155                 }
156
157                 if (Thread.interrupted()) {
158                     return;
159                 }
160             }
161         } catch (IOException JavaDoc ioexc) {
162             //ErrorManager.getDefault().notify(ioexc);
163
} catch (Throwable JavaDoc t) {
164             System.err.println("Caught throwable " + t);
165         } finally {
166             try {
167                 // Suck the rest of the I/O out
168
if (str.available() > 0) {
169                     String JavaDoc line = read.readLine();
170
171                     if (line != null) {
172                         processLine(line);
173                     }
174                 }
175
176                 read.close();
177                 writer.close();
178             } catch (IOException JavaDoc ioexc) {
179                 ErrorManager.getDefault().notify(ioexc);
180             }
181         }
182     }
183 }
184
Popular Tags