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: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * Po-Ting Wu: Copy from studio-plugin/src/org/netbeans/modules/j2ee/sun/ide/j2ee/LogViewerSupport.java
044: */
045:
046: package org.netbeans.core.actions;
047:
048: import java.io.IOException;
049: import java.io.InputStreamReader;
050: import java.io.File;
051: import java.util.*;
052: import java.io.*;
053: import java.util.logging.Level;
054: import java.util.logging.Logger;
055: import org.openide.filesystems.*;
056: import org.openide.util.*;
057: import org.openide.windows.IOProvider;
058: import org.openide.windows.InputOutput;
059: import org.openide.windows.*;
060:
061: /** Connects the output stream of a file to the IDE output window.
062: *
063: * @author ludo
064: */
065: public class LogViewerSupport implements Runnable {
066: boolean shouldStop = false;
067: FileInputStream filestream = null;
068: BufferedReader ins;
069: InputOutput io;
070: File fileName;
071: String ioName;
072: int lines;
073: Ring ring;
074: private final RequestProcessor.Task task = RequestProcessor
075: .getDefault().create(this );
076:
077: /** Connects a given process to the output window. Returns immediately, but threads are started that
078: * copy streams of the process to/from the output window.
079: * @param process process whose streams to connect to the output window
080: * @param ioName name of the output window tab to use
081: */
082: public LogViewerSupport(final File fileName, final String ioName) {
083:
084: this .fileName = fileName;
085: this .ioName = ioName;
086: }
087:
088: private void init() {
089: final int LINES = 2000;
090: final int OLD_LINES = 2000;
091: ring = new Ring(OLD_LINES);
092: String line;
093:
094: // Read the log file without
095: // displaying everything
096: try {
097: while ((line = ins.readLine()) != null) {
098: ring.add(line);
099: } // end of while ((line = ins.readLine()) != null)
100: } catch (IOException e) {
101: Logger.getLogger(LogViewerSupport.class.getName()).log(
102: Level.INFO, null, e);
103: } // end of try-catch
104:
105: // Now show the last OLD_LINES
106: lines = ring.output();
107: ring.setMaxCount(LINES);
108: }
109:
110: public void run() {
111: final int MAX_LINES = 10000;
112: String line;
113:
114: ////System.out.println("io close or not"+io.isClosed());
115: if (io.isClosed()) {//tab is closed by the user
116: shouldStop = true;
117: } else {
118: // it is possilbe in the case of only
119: // 1 tab, that the tab is hidden, not
120: // closed. In this case we need to
121: // detect that and close our stream
122: // anyway to unlock the log file
123: shouldStop = true; //assume the tab is hidden
124: TopComponent.Registry rr = TopComponent.getRegistry();
125: for (TopComponent tc : rr.getOpened()) {
126: if (tc.toString().startsWith(
127: "org.netbeans.core.output2.OutputWindow")) {
128: // the tab is not hidden so we should not stopped!!!
129: shouldStop = false;
130: break;
131: }
132: }
133: }
134:
135: if (!shouldStop) {
136: try {
137: if (lines >= MAX_LINES) {
138: io.getOut().reset();
139: lines = ring.output();
140: } // end of if (lines >= MAX_LINES)
141:
142: while ((line = ins.readLine()) != null) {
143: if ((line = ring.add(line)) != null) {
144: io.getOut().println(line);
145: lines++;
146: } // end of if ((line = ring.add(line)) != null)
147: }
148:
149: } catch (IOException e) {
150: Logger.getLogger(LogViewerSupport.class.getName()).log(
151: Level.INFO, null, e);
152: }
153: task.schedule(10000);
154: } else {
155: ///System.out.println("end of infinite loop for log viewer\n\n\n\n");
156: stopUpdatingLogViewer();
157: }
158: }
159:
160: /* display the log viewer dialog
161: *
162: **/
163:
164: public void showLogViewer() throws IOException {
165: shouldStop = false;
166: io = IOProvider.getDefault().getIO(ioName, false);
167: io.getOut().reset();
168: io.select();
169: filestream = new FileInputStream(fileName);
170: ins = new BufferedReader(new InputStreamReader(filestream));
171:
172: init();
173: task.schedule(0);
174: }
175:
176: /* stop to update the log viewer dialog
177: *
178: **/
179:
180: public void stopUpdatingLogViewer() {
181: try {
182: ins.close();
183: filestream.close();
184: io.closeInputOutput();
185: io.setOutputVisible(false);
186: } catch (IOException e) {
187: Logger.getLogger(LogViewerSupport.class.getName()).log(
188: Level.INFO, null, e);
189: }
190: }
191:
192: private class Ring {
193: private int maxCount;
194: private int count;
195: private LinkedList<String> anchor;
196:
197: public Ring(int max) {
198: maxCount = max;
199: count = 0;
200: anchor = new LinkedList<String>();
201: }
202:
203: public String add(String line) {
204: if (line == null || line.equals("")) { // NOI18N
205: return null;
206: } // end of if (line == null || line.equals(""))
207:
208: while (count >= maxCount) {
209: anchor.removeFirst();
210: count--;
211: } // end of while (count >= maxCount)
212:
213: anchor.addLast(line);
214: count++;
215:
216: return line;
217: }
218:
219: public void setMaxCount(int newMax) {
220: maxCount = newMax;
221: }
222:
223: public int output() {
224: int i = 0;
225: for (String s : anchor) {
226: io.getOut().println(s);
227: i++;
228: }
229:
230: return i;
231: }
232:
233: public void reset() {
234: anchor = new LinkedList<String>();
235: }
236: }
237: }
|