KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > bluej > ant > task > BlueJava


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.bluej.ant.task;
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.io.OutputStream JavaDoc;
26 import java.io.OutputStreamWriter JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.Reader JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.WeakHashMap JavaDoc;
32 import org.apache.tools.ant.BuildException;
33 import org.apache.tools.ant.Task;
34 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;
35 import org.apache.tools.ant.taskdefs.Java;
36 import org.apache.tools.ant.taskdefs.Redirector;
37 import org.openide.util.RequestProcessor;
38 import org.openide.windows.IOProvider;
39 import org.openide.windows.InputOutput;
40 import org.openide.windows.OutputWriter;
41
42 /**
43  * Ant task for redirecting the output of java task to the netbeans output window.
44  * @author Milos Kleint
45  */

46 public class BlueJava extends Java {
47     
48     public BlueJava() {
49         redirector = new MyRedirector(this);
50     }
51
52     
53     private class MyRedirector extends Redirector {
54         public MyRedirector(Task task) {
55             super(task);
56         }
57         public ExecuteStreamHandler createHandler() throws BuildException {
58             createStreams();
59             return new NbOutputStreamHandler(getProject().getName());
60         }
61         
62     }
63     
64     private static final RequestProcessor PROCESSOR = new RequestProcessor("Netbeans-Bluej Run IO redirection", 5);
65     /**
66      * All tabs which were used for some process which has now ended.
67      * These are closed when you start a fresh process.
68      * Map from tab to tab display name.
69      * @see "#43001"
70      */

71     private static final Map JavaDoc freeTabs = new WeakHashMap JavaDoc();
72     
73     
74     private static class NbOutputStreamHandler implements ExecuteStreamHandler {
75         private InputOutput io;
76         private RequestProcessor.Task outTask;
77         private RequestProcessor.Task errTask;
78         private RequestProcessor.Task inTask;
79         private Input input;
80         private String JavaDoc displayName;
81         public NbOutputStreamHandler(String JavaDoc name) {
82             displayName = "Run " + name;
83                 // OutputWindow
84
// if (AntSettings.getDefault().getAutoCloseTabs()) { // #47753
85
synchronized (freeTabs) {
86                     Iterator JavaDoc it = freeTabs.entrySet().iterator();
87                     while (it.hasNext()) {
88                         Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
89                         InputOutput free = (InputOutput)entry.getKey();
90                         String JavaDoc freeName = (String JavaDoc)entry.getValue();
91                         if (io == null && freeName.equals(displayName)) {
92                             // Reuse it.
93
io = free;
94                             try {
95                                 io.getOut().reset();
96                                 io.getErr().reset();
97                             } catch (IOException JavaDoc ex) {
98                                 ex.printStackTrace();
99                             }
100                                 // useless: io.flushReader();
101
} else {
102                                 // Discard it.
103
free.closeInputOutput();
104                         }
105                     }
106                     freeTabs.clear();
107                 }
108 // }
109
if (io == null) {
110                     io = IOProvider.getDefault().getIO(displayName, true);
111                 }
112             
113         }
114         public void stop() {
115             if (input != null) {
116                 input.closeReader();
117             }
118             if (inTask != null) {
119                 inTask.waitFinished();
120             }
121             if (errTask != null) {
122                 errTask.waitFinished();
123             }
124             if (outTask != null) {
125                 outTask.waitFinished();
126             }
127             synchronized (freeTabs) {
128                 freeTabs.put(io, displayName);
129             }
130         }
131
132         public void start() throws IOException JavaDoc {
133             io.select();
134         }
135
136         public void setProcessOutputStream(InputStream JavaDoc inputStream) throws IOException JavaDoc {
137             Output out = new Output(inputStream, io.getOut());
138             outTask = PROCESSOR.post(out);
139         }
140
141         public void setProcessErrorStream(InputStream JavaDoc inputStream) throws IOException JavaDoc {
142             Output err = new Output(inputStream, io.getErr());
143             errTask = PROCESSOR.post(err);
144         }
145
146         public void setProcessInputStream(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
147             input = new Input(io.getIn(), outputStream);
148             inTask = PROCESSOR.post(input);
149         }
150         
151     }
152     
153     private static class Output implements Runnable JavaDoc {
154         private InputStreamReader JavaDoc str;
155         private OutputWriter writer;
156         public Output(InputStream JavaDoc instream, OutputWriter out) {
157             str = new InputStreamReader JavaDoc(instream);
158             writer = out;
159         }
160         
161         public void run() {
162             try {
163                 int chr = str.read();
164                 while (chr != -1) {
165                     if (chr == (int)'\n') {
166                         writer.println();
167                     } else {
168                         writer.write(chr);
169                     }
170                     chr = str.read();
171                 }
172             } catch (IOException JavaDoc ex) {
173                 ex.printStackTrace();
174             } finally {
175                 try {
176                     str.close();
177                 } catch (IOException JavaDoc ex) {
178                     ex.printStackTrace();
179                 }
180                 closeWriter();
181             }
182         }
183         
184         public void closeWriter() {
185             writer.close();
186         }
187     }
188     
189     private static class Input implements Runnable JavaDoc {
190         private Reader JavaDoc ioReader;
191         private BufferedReader JavaDoc str;
192         private PrintWriter JavaDoc writer;
193         public Input(Reader JavaDoc instream, OutputStream JavaDoc out) {
194             ioReader = instream;
195             str = new BufferedReader JavaDoc(instream);
196             writer = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(out));
197         }
198         
199         public void closeReader() {
200                 try {
201                     //somehow the original reader needs to be closed first..
202
ioReader.close();
203                     str.close();
204                     writer.close();
205                 } catch (IOException JavaDoc exc) {
206                     exc.printStackTrace();
207                 }
208         }
209         
210         public void run() {
211             try {
212                 String JavaDoc line = str.readLine();
213                 while (line != null) {
214                     if (!writer.checkError()) {
215                         writer.println(line);
216                         writer.flush();
217                     } else {
218                         break;
219                     }
220                     line = str.readLine();
221                 }
222             } catch (IOException JavaDoc ex) {
223                 ex.printStackTrace();
224             } finally {
225                 writer.close();
226             }
227         }
228         
229     }
230     
231 }
232
233
Popular Tags