001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Sam
028: */
029:
030: package com.caucho.netbeans;
031:
032: import com.caucho.netbeans.util.LogSupport;
033:
034: import org.openide.ErrorManager;
035:
036: import java.io.BufferedReader;
037: import java.io.IOException;
038: import java.io.Reader;
039:
040: /**
041: * Reads from the standard and error output and write to the console.
042: */
043: public final class ConsoleRedirector {
044: private final Console _console;
045: private BufferedReader _inReader;
046: private BufferedReader _errReader;
047: private final LogSupport _logSupport = new LogSupport();
048: private final ConsoleWriterThread _thread;
049:
050: private volatile boolean _isActive = true;
051:
052: public ConsoleRedirector(Console console, Reader in, Reader err) {
053: _console = console;
054: _inReader = new BufferedReader(in);
055: _errReader = new BufferedReader(err);
056:
057: _thread = new ConsoleWriterThread("resin-" + console.getUri()
058: + "-console-writer");
059: _thread.start();
060: }
061:
062: public void destroy() {
063: if (_isActive) {
064: _isActive = false;
065: _thread.interrupt();
066: }
067: }
068:
069: private class ConsoleWriterThread extends Thread {
070: public ConsoleWriterThread(String name) {
071: super (name);
072:
073: setDaemon(true);
074: }
075:
076: public void run() {
077: try {
078: while (_isActive) {
079:
080: // read max 256 lines at a time
081:
082: int lines = 0;
083:
084: while (_isActive && lines < 256) {
085: boolean isInReady = _inReader.ready();
086: boolean isErrReady = _errReader.ready();
087:
088: if (!(isInReady || isErrReady))
089: break;
090:
091: if (isInReady) {
092: String line = _inReader.readLine();
093:
094: if (line == null)
095: _isActive = false;
096: else {
097: _console.println(line);
098: lines++;
099: }
100: }
101:
102: if (isErrReady) {
103: String line = _errReader.readLine();
104:
105: if (line == null)
106: _isActive = false;
107: else {
108: _console.println(line);
109: lines++;
110: }
111: }
112: }
113:
114: _console.flush();
115:
116: sleep(100);
117: }
118: } catch (IOException e) {
119: ErrorManager.getDefault().notify(
120: ErrorManager.INFORMATIONAL, e);
121: } catch (InterruptedException e) {
122: // no-op
123: }
124: }
125: }
126: }
|