KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ws7 > j2ee > WS70LogViewer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.sun.ws7.j2ee;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.io.File JavaDoc;
25 import java.util.*;
26 import java.io.*;
27
28 import org.openide.ErrorManager;
29 import org.openide.windows.InputOutput;
30 import org.openide.windows.*;
31 /** Connects the output stream of a file to the IDE output window.
32  *
33  * @author ludo Adapted by Mukesh Garg for Webserver70
34  */

35 public class WS70LogViewer extends Thread JavaDoc {
36     boolean shouldStop = false;
37     FileInputStream filestream=null;
38     BufferedReader ins;
39     InputOutput io;
40     File JavaDoc fileName;
41     
42     /** Connects a given process to the output window. Returns immediately, but threads are started that
43      * copy streams of the process to/from the output window.
44      * @param process process whose streams to connect to the output window
45      * @param url deployment manager URL
46      */

47     public WS70LogViewer(final File JavaDoc fileName) {
48         this.fileName=fileName;
49     }
50     
51     
52     public void run() {
53         int MAX_LINES = 15000;
54         int LINES = 2000;
55         int OLD_LINES = 600;
56         int lines;
57         Ring ring = new Ring(OLD_LINES);
58         int c;
59         String JavaDoc line;
60         
61                                 // Read the log file without
62
// displaying everything
63
try {
64             while ((line = ins.readLine()) != null) {
65                 ring.add(line);
66             } // end of while ((line = ins.readLine()) != null)
67
} catch (IOException JavaDoc e) {
68             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
69         } // end of try-catch
70

71                                 // Now show the last OLD_LINES
72
lines = ring.output();
73         ring.setMaxCount(LINES);
74         
75         while (shouldStop ==false) {
76             try {
77                 if (lines >= MAX_LINES) {
78                     io.getOut().reset();
79                     lines = ring.output();
80                 } // end of if (lines >= MAX_LINES)
81

82                 while ((line = ins.readLine()) != null) {
83                     if ((line = ring.add(line)) != null) {
84                         io.getOut().println(line);
85                         lines++;
86                     } // end of if ((line = ring.add(line)) != null)
87
}
88                 
89             }catch (IOException JavaDoc e) {
90                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
91             }
92             try {
93                 sleep(10000);
94                 ////System.out.println("io close or not"+io.isClosed());
95
if (io.isClosed()){//tab is closed by the user
96
shouldStop =true;
97                 }
98                 else{
99                                 // it is possilbe in the case of only
100
// 1 tab, that the tab is hidden, not
101
// closed. In this case we need to
102
// detect that and close our stream
103
// anyway to unlock the log file
104
shouldStop =true; //assume the tab is hidden
105
TopComponent.Registry rr= TopComponent.getRegistry();
106                     Set ss = rr.getOpened();
107                     Iterator ttt = ss.iterator();
108                     while (ttt.hasNext()){
109                         Object JavaDoc o = ttt.next();
110                         String JavaDoc sss=""+o;
111                         if (sss.startsWith("org.netbeans.core.output2.OutputWindow")){
112                             // the tab is not hidden so we should not stopped!!!
113
shouldStop =false;
114                         }
115                     }
116                 }
117             }catch (Exception JavaDoc e){
118                 ErrorManager.getDefault().notify(ErrorManager.ERROR, e);
119             }
120         }
121         ///System.out.println("end of infinite loop for log viewer\n\n\n\n");
122
stopUpdatingLogViewer();
123         
124     }
125     /* display the log viewer dialog
126      *
127      **/

128     
129     public void showLogViewer(InputOutput inputoutput) throws IOException JavaDoc{
130         shouldStop = false;
131         io = inputoutput;
132         io.getOut().reset();
133         io.select();
134         filestream = new FileInputStream(fileName);
135         // RAVE ins = new BufferedReader(new InputStreamReader(filestream,"UTF-8"));//NOI18N
136
// Use the default charset!
137
ins = new BufferedReader(new InputStreamReader JavaDoc(filestream));
138         
139         start();
140     }
141     
142     /* stop to update the log viewer dialog
143      *
144      **/

145     
146     public void stopUpdatingLogViewer() {
147         shouldStop = true;
148         
149         try{
150             ins.close();
151             filestream.close();
152             io.closeInputOutput();
153             io.setOutputVisible(false);
154         }
155         catch (IOException JavaDoc e){
156             
157         }
158     }
159     
160
161     private class Ring {
162         private int maxCount;
163         private int count;
164         private LinkedList anchor;
165         
166         public Ring(int max) {
167             maxCount = max;
168             count = 0;
169             anchor = new LinkedList();
170         }
171     
172         public String JavaDoc add(String JavaDoc line) {
173             if (line == null || line.equals("")) { // NOI18N
174
return null;
175             } // end of if (line == null || line.equals(""))
176

177             while (count >= maxCount) {
178                 anchor.removeFirst();
179                 count--;
180             } // end of while (count >= maxCount)
181

182             anchor.addLast(line);
183             count++;
184             
185             return line;
186         }
187
188         public void setMaxCount(int newMax) {
189             maxCount = newMax;
190         }
191         
192         public int output() {
193             int i = 0;
194             Iterator it = anchor.iterator();
195             
196             while (it.hasNext()) {
197                 io.getOut().println((String JavaDoc)it.next());
198                 i++;
199             } // end of while (it.hasNext())
200

201             return i;
202         }
203         
204         public void reset() {
205             anchor = new LinkedList();
206         }
207     }
208 }
209
210
Popular Tags