001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028:
029: package org.apache.tools.ant.module.bridge.impl;
030:
031: import java.io.ByteArrayOutputStream;
032: import java.io.IOException;
033: import java.io.InputStream;
034: import java.io.OutputStream;
035: import org.apache.tools.ant.BuildException;
036: import org.apache.tools.ant.Project;
037: import org.apache.tools.ant.Task;
038: import org.apache.tools.ant.module.bridge.AntBridge;
039: import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;
040: import org.apache.tools.ant.taskdefs.Java;
041: import org.apache.tools.ant.taskdefs.LogOutputStream;
042: import org.apache.tools.ant.taskdefs.Redirector;
043: import org.openide.util.RequestProcessor;
044:
045: /**
046: * Replacement for Ant's java task which directly sends I/O to the output without line buffering.
047: * Idea from ide/projectimport/bluej/antsrc/org/netbeans/bluej/ant/task/BlueJava.java.
048: * See issue #56341.
049: */
050: public class ForkedJavaOverride extends Java {
051:
052: private static final RequestProcessor PROCESSOR = new RequestProcessor(
053: ForkedJavaOverride.class.getName(), Integer.MAX_VALUE);
054:
055: public ForkedJavaOverride() {
056: redirector = new NbRedirector(this );
057: super .setFork(true);
058: }
059:
060: @Override
061: public void setFork(boolean fork) {
062: // #47465: ignore! Does not work to be set to false.
063: }
064:
065: private class NbRedirector extends Redirector {
066:
067: private String outEncoding = System
068: .getProperty("file.encoding"); // NOI18N
069: private String errEncoding = System
070: .getProperty("file.encoding"); // NOI18N
071:
072: public NbRedirector(Task task) {
073: super (task);
074: }
075:
076: public @Override
077: ExecuteStreamHandler createHandler() throws BuildException {
078: createStreams();
079: return new NbOutputStreamHandler();
080: }
081:
082: public @Override
083: synchronized void setOutputEncoding(String outputEncoding) {
084: outEncoding = outputEncoding;
085: super .setOutputEncoding(outputEncoding);
086: }
087:
088: public @Override
089: synchronized void setErrorEncoding(String errorEncoding) {
090: errEncoding = errorEncoding;
091: super .setErrorEncoding(errorEncoding);
092: }
093:
094: private class NbOutputStreamHandler implements
095: ExecuteStreamHandler {
096:
097: private RequestProcessor.Task outTask;
098: private RequestProcessor.Task errTask;
099:
100: //private RequestProcessor.Task inTask;
101:
102: //long init = System.currentTimeMillis();
103: NbOutputStreamHandler() {
104: }
105:
106: public void start() throws IOException {
107: }
108:
109: public void stop() {
110: /* XXX causes process to hang at end
111: if (inTask != null) {
112: inTask.waitFinished();
113: }
114: */
115: if (errTask != null) {
116: errTask.waitFinished();
117: }
118: if (outTask != null) {
119: outTask.waitFinished();
120: }
121: }
122:
123: public void setProcessOutputStream(InputStream inputStream)
124: throws IOException {
125: OutputStream os = getOutputStream();
126: Integer logLevel = null;
127: if (os == null || os instanceof LogOutputStream) {
128: os = AntBridge.delegateOutputStream(false);
129: logLevel = Project.MSG_INFO;
130: }
131: outTask = PROCESSOR.post(new Copier(inputStream, os,
132: logLevel, outEncoding/*, init*/));
133: }
134:
135: public void setProcessErrorStream(InputStream inputStream)
136: throws IOException {
137: OutputStream os = getErrorStream();
138: Integer logLevel = null;
139: if (os == null || os instanceof LogOutputStream) {
140: os = AntBridge.delegateOutputStream(true);
141: logLevel = Project.MSG_WARN;
142: }
143: errTask = PROCESSOR.post(new Copier(inputStream, os,
144: logLevel, errEncoding/*, init*/));
145: }
146:
147: public void setProcessInputStream(OutputStream outputStream)
148: throws IOException {
149: InputStream is = getInputStream();
150: if (is == null) {
151: is = AntBridge.delegateInputStream();
152: }
153: /*inTask = */PROCESSOR.post(new Copier(is,
154: outputStream, null, null/*, init*/));
155: }
156:
157: }
158:
159: }
160:
161: private class Copier implements Runnable {
162:
163: private final InputStream in;
164: private final OutputStream out;
165: //final long init;
166: private final Integer logLevel;
167: private final String encoding;
168: private final RequestProcessor.Task flusher;
169: private final ByteArrayOutputStream currentLine;
170:
171: public Copier(InputStream in, OutputStream out,
172: Integer logLevel, String encoding/*, long init*/) {
173: this .in = in;
174: this .out = out;
175: this .logLevel = logLevel;
176: this .encoding = encoding;
177: //this.init = init;
178: if (logLevel != null) {
179: flusher = PROCESSOR.create(new Runnable() {
180: public void run() {
181: maybeFlush();
182: }
183: });
184: currentLine = new ByteArrayOutputStream();
185: } else {
186: flusher = null;
187: currentLine = null;
188: }
189: }
190:
191: public void run() {
192: /*
193: StringBuilder content = new StringBuilder();
194: long tick = System.currentTimeMillis();
195: content.append(String.format("[init: %1.1fsec]", (tick - init) / 1000.0));
196: */
197: try {
198: try {
199: int c;
200: while ((c = in.read()) != -1) {
201: /*
202: long newtick = System.currentTimeMillis();
203: if (newtick - tick > 100) {
204: content.append(String.format("[%1.1fsec]", (newtick - tick) / 1000.0));
205: }
206: tick = newtick;
207: content.append((char) c);
208: */
209: if (logLevel == null) {
210: // Input gets sent immediately.
211: out.write(c);
212: out.flush();
213: } else {
214: // Output and err are buffered (for a time) looking for a complete line.
215: synchronized (this ) {
216: if (c == '\n') {
217: log(currentLine.toString(encoding),
218: logLevel);
219: currentLine.reset();
220: } else if (c != '\r') {
221: currentLine.write(c);
222: flusher.schedule(250);
223: }
224: }
225: }
226: }
227: } finally {
228: if (logLevel != null) {
229: maybeFlush();
230: }
231: }
232: } catch (IOException x) {
233: // ignore IOException: Broken pipe from FileOutputStream.writeBytes in BufferedOutputStream.flush
234: } catch (ThreadDeath d) {
235: // OK, build just stopped.
236: return;
237: }
238: //System.err.println("copied " + in + " to " + out + "; content='" + content + "'");
239: }
240:
241: private synchronized void maybeFlush() {
242: try {
243: currentLine.writeTo(out);
244: out.flush();
245: } catch (IOException x) {
246: // probably safe to ignore
247: } catch (ThreadDeath d) {
248: // OK, build just stopped.
249: }
250: currentLine.reset();
251: }
252:
253: }
254:
255: }
|