Source Code Cross Referenced for ForkedJavaOverride.java in  » IDE-Netbeans » library » org » apache » tools » ant » module » bridge » impl » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » library » org.apache.tools.ant.module.bridge.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
w___w__w__.__j_a___v_a__2_s_.___c___o__m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.