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.PluginLogger;
033: import com.caucho.netbeans.util.LogSupport;
034:
035: import org.netbeans.modules.j2ee.deployment.plugins.api.UISupport;
036: import org.openide.ErrorManager;
037: import org.openide.windows.InputOutput;
038: import org.openide.windows.OutputWriter;
039:
040: import java.io.IOException;
041: import java.io.Reader;
042: import java.util.logging.Level;
043:
044: public final class Console {
045: private static final PluginLogger log = new PluginLogger(
046: Console.class);
047:
048: private final String _uri;
049: private final InputOutput _inputOutput;
050:
051: private final OutputWriter _writer;
052: private final OutputWriter _errorWriter;
053: private final LogSupport _logSupport = new LogSupport();
054:
055: private ConsoleRedirector _consoleRedirector;
056:
057: public Console(String uri) {
058: _uri = uri;
059: _inputOutput = UISupport.getServerIO(uri);
060:
061: try {
062: _inputOutput.getOut().reset();
063: } catch (IOException e) {
064: log.log(Level.INFO, e);
065: }
066:
067: _writer = _inputOutput.getOut();
068: _errorWriter = _inputOutput.getErr();
069: _inputOutput.select();
070: }
071:
072: public String getUri() {
073: return _uri;
074: }
075:
076: public void println() {
077: _writer.println();
078: }
079:
080: public void println(String line) {
081: LogSupport.LineInfo lineInfo = _logSupport.analyzeLine(line);
082:
083: if (lineInfo.isError()) {
084: if (lineInfo.isAccessible()) {
085: try {
086: _errorWriter.println(line, _logSupport.getLink(
087: lineInfo.message(), lineInfo.path(),
088: lineInfo.line()));
089: } catch (IOException ex) {
090: ErrorManager.getDefault().notify(
091: ErrorManager.INFORMATIONAL, ex);
092: }
093: } else {
094: _errorWriter.println(line);
095: }
096: takeFocus();
097: } else {
098: _writer.println(line);
099: }
100: }
101:
102: public void start(Reader in, Reader err) {
103: if (_consoleRedirector != null)
104: throw new IllegalStateException();
105:
106: _consoleRedirector = new ConsoleRedirector(this , in, err);
107: }
108:
109: void flush() {
110: _writer.flush();
111: _errorWriter.flush();
112: }
113:
114: public void takeFocus() {
115: _inputOutput.select();
116: }
117:
118: public void stop() {
119: ConsoleRedirector consoleRedirector = _consoleRedirector;
120: _consoleRedirector = null;
121:
122: if (consoleRedirector != null)
123: consoleRedirector.destroy();
124: }
125:
126: public void destroy() {
127: try {
128: stop();
129: } finally {
130: _logSupport.detachAnnotation();
131: }
132: }
133: }
|