KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > j2ee > LogViewerSupport


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.ide.j2ee;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.WeakHashMap JavaDoc;
30 import org.netbeans.modules.j2ee.deployment.plugins.api.UISupport;
31 import org.openide.ErrorManager;
32 import org.openide.windows.InputOutput;
33
34
35
36
37 public final class LogViewerSupport extends Thread JavaDoc {
38     
39     
40     private InputOutput io;
41     
42     private String JavaDoc url;
43     private static final HashMap JavaDoc logsMap = new HashMap JavaDoc();
44     // known open logs
45
// private static Set openLogs = new HashSet();
46
/**
47      * frequency to check file changes
48      */

49     private long sampleInterval = 10000;
50     
51     /**
52      * The log file to tail
53      */

54     private File JavaDoc logfile;
55     
56     /**
57      * Defines whether the log file tailer should include the entire contents
58      * of the exising log file or tail from the end of the file when the tailer starts
59      */

60     private boolean startAtBeginning = false;
61     
62     /**
63      * are we working, reading the log file?
64      */

65     private boolean working = false;
66     private LogHyperLinkSupport.AppServerLogSupport logSupport;
67     private BufferedReader JavaDoc reader = null;
68     
69     private boolean initRingerDone=false;
70     /* ring buffer constants:
71      */

72     static private int OLD_LINES = 600;
73     static private int MAX_LINES = 25000;
74     static private int LINES = 2000;
75
76     /**
77      * Creates a new log file tailer
78      *
79      * @param file The file to tail
80      * @param sampleInterval How often to check for updates to the log file (default = 10s)
81      * @param startAtBeginning Should the tailer simply tail or should it process the entire
82      * file and continue tailing (true) or simply start tailing from the
83      * end of the file
84      */

85
86     public static LogViewerSupport getLogViewerSupport(final File JavaDoc file, final String JavaDoc url, final long sampleInterval, final boolean startAtBeginning ) {
87         synchronized (logsMap) {
88             LogViewerSupport logViewer = (LogViewerSupport)logsMap.get(url);
89             if (logViewer==null) {
90                 logViewer = new LogViewerSupport( file, url, sampleInterval, startAtBeginning );
91                 logsMap.put(url,logViewer);
92             }
93
94             logViewer.sampleInterval = sampleInterval;
95             logViewer.startAtBeginning = startAtBeginning;
96             return logViewer;
97         }
98     }
99
100     /**
101      * stop and remove the log viewer thread for the given server (url)
102      * @param url: the server url
103      * nop is there is no thread for this server
104      * otherwise, stop the thread and close the log file, so that there is no lock on it anymore
105      */

106     public static void removeLogViewerSupport(final String JavaDoc url) {
107         synchronized (logsMap) {
108             LogViewerSupport logViewer = (LogViewerSupport)logsMap.get(url);
109             if (logViewer!=null) {
110                 logsMap.remove(url);
111                 logViewer.working =false;
112             }
113         }
114     }
115     
116     private LogViewerSupport(final File JavaDoc file, final String JavaDoc url, final long sampleInterval, final boolean startAtBeginning) {
117         this.logfile = file;
118         this.url = url;
119         this.sampleInterval = sampleInterval;
120         this.startAtBeginning = startAtBeginning;
121         io = UISupport.getServerIO(url);
122         
123         logSupport = new LogHyperLinkSupport.AppServerLogSupport("", "/");
124         // let process know that it doesn't have to wait for me.
125
setDaemon(true);
126         start();
127     }
128     
129     
130     
131     protected void printLine( String JavaDoc line ) {
132         String JavaDoc s = filterLine(line);
133         if ((!s.equals(""))&&((!s.equals(" ")))){//NOI18N
134

135             LogHyperLinkSupport.AppServerLogSupport.LineInfo lineInfo = logSupport.analyzeLine(s);
136             if (lineInfo.isError()) {
137                 if (lineInfo.isAccessible()) {
138                     try {
139                         io.getOut().println(s, logSupport.getLink(lineInfo.message(), lineInfo.path(), lineInfo.line()));
140                     } catch (IOException JavaDoc ex) {
141                         ErrorManager.getDefault().notify(ex);
142                     }
143                 } else {
144                     io.getOut().println(s);
145                 }
146             } else {
147                 io.getOut().println(s);
148             }
149             
150         }
151         
152     }
153     
154     public void stopTailing() {
155         this.working = false;
156     }
157     
158     
159     private void initRingBuffer(Ring ring){
160
161         try {
162             // Start tailing
163
if (reader!=null){
164                 try {
165                     reader.close();
166                 } catch (IOException JavaDoc ex) {
167                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
168                 }
169                 reader = null;
170             }
171             
172             if(logfile.exists()) {
173                 reader = new BufferedReader JavaDoc(new FileReader JavaDoc(logfile));
174                 int c;
175                 String JavaDoc line;
176
177                 // Read the log file without
178
// displaying everything
179
try {
180                     while ((line = reader.readLine()) != null) {
181                         ring.add(line);
182                     }
183                 } catch (IOException JavaDoc e) {
184                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
185                 }
186                 // Now show the last OLD_LINES
187
if (startAtBeginning){
188                     ring.output();
189
190                 }
191                 ring.setMaxCount(LINES);
192             }
193         } catch (Exception JavaDoc e){
194             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
195         }
196         
197     }
198
199     public void run() {
200         // The file pointer keeps track of where we are in the file
201
long currentIndex = 0;
202         long currentNbofFileLogs = logfile.getParentFile().list().length;
203         boolean needTosleep=true;
204         boolean needToRotate=false;
205         boolean alreadyRotated=false;
206         working = true;
207         String JavaDoc line;
208         int lines =0;
209
210         Ring ring = new Ring(OLD_LINES);
211         currentIndex = logfile.length();
212         try{
213             while( working /*&& !io.isClosed()*/) {
214                 needTosleep=true;
215                 try {
216                     if (initRingerDone==false){
217                         //this init is initializing the reader
218
initRingBuffer(ring);
219                         currentIndex = logfile.length();
220                         initRingerDone=true;
221                     }
222                     if (lines >= MAX_LINES) {
223                         io.getOut().reset();
224                         lines = ring.output();
225                     }
226                     // Compare the length of the file to the file pointer
227
long fileLength = logfile.length();
228                     long newNbofFileLogs = logfile.getParentFile().list().length;
229                     
230                     if( fileLength < currentIndex ) {
231                         needToRotate =true;
232                     }
233                     
234                     if( currentNbofFileLogs < newNbofFileLogs ) {
235                         needToRotate =true;
236                         currentNbofFileLogs = newNbofFileLogs;
237                     }
238                     
239                     if (needToRotate ){
240                         // Log file must have been rotated or deleted;
241
// reopen the file and reset the file pointer
242

243                         //flush where we are.
244
try {
245                             line = (reader != null) ? reader.readLine() : null;
246                             if(line != null) {
247                                 alreadyRotated = false;
248                             }
249                             while( line != null ) {
250                                 printLine( line );
251                                 ring.add(line);
252                                 
253                                 line = reader.readLine();
254                                 lines++;
255                             }
256                         } catch (IOException JavaDoc ex) {
257                             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
258                             needToRotate =true;
259                         }
260                         
261                         //reopen the reader on the new log file:
262
if(!alreadyRotated) {
263                             // only print this once until we get more messages.
264
printLine("----Log File Rotated---");
265                             alreadyRotated = true;
266                         }
267                         
268                         try {
269                             if(reader != null) {
270                                 reader.close();
271                             }
272                         } catch (IOException JavaDoc ex) {
273                             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
274                         }
275                         
276                         if(logfile.exists()) {
277                             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(logfile));
278                             currentIndex = 0;
279                             needToRotate = false;
280                         } else {
281                             reader = null;
282                         }
283                     }
284                     
285                     
286                     try {
287                         line = (reader != null) ? reader.readLine() : null;
288                         if(line != null) {
289                             alreadyRotated = false;
290                         }
291                         while( line != null ) {
292                             printLine( line );
293                             ring.add(line);
294                             line = reader.readLine();
295                             lines++;
296                         }
297                     } catch (IOException JavaDoc ex) {
298                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
299                         needTosleep=false; //try to catch up
300
needToRotate =true;
301                     }
302                     //calculate the current log situation
303
currentIndex = logfile.length();
304                     currentNbofFileLogs = logfile.getParentFile().list().length;
305                     
306                     
307                 } catch(Exception JavaDoc ex) {
308                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
309                     working = true;//we continue
310
} finally{
311                     try {
312                         synchronized(this) {
313                             if (needTosleep){
314                                 wait(100);
315                             }
316                         }
317                     } catch(InterruptedException JavaDoc ex) {
318                         // PMD no op - the thread was interrupted
319
}
320                 }
321             }
322         } catch( Exception JavaDoc e ) {
323             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
324             working =false;//we continue
325
} finally{
326             //we close the reader
327
if (reader!=null) {
328                 try {
329                     reader.close();
330                 } catch (IOException JavaDoc ex) {
331                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
332                 }
333             }
334         }
335     }
336
337     /*
338      * return the 6th element of a log line entry, or the line if this element does
339      * not exist, or if the line cannot be parsed correclty.
340      *
341      */

342     private String JavaDoc filterLine(String JavaDoc line){
343         if (!line.startsWith("[#")){ //NOI18N
344
if (line.endsWith("|#]")){//NOI18N
345
line = line.substring(0,line.length()-3); //remove the last 3 chars.
346
}
347             return line;
348         }
349         
350         String JavaDoc s[] = line.split("\\|");//NOI18N
351
if (s==null){
352             return line;
353         }
354         
355         
356         if (s.length<=6){
357             return "";//NOI18N
358
}
359         
360         return s[6];
361     }
362
363     /**
364      * display the log viewer dialog
365      * @param forced reset view if true, otherwise refresh
366      * @return The output window
367      * @throws java.io.IOException encountered issue while doing a reset on the stdout
368      */

369     public InputOutput showLogViewer(boolean forced) throws IOException JavaDoc{
370         io = UISupport.getServerIO(url);
371         
372         
373         // System.out.println("we retart the thread in showlogviewwer forced="+forced +" Closed?="+io.isClosed());
374

375         working = true;
376         if (forced &&(io.isClosed())){
377             initRingerDone=false;
378             io.getOut().reset();
379         }
380         
381         io.select();
382         
383         return io;
384         
385     }
386     
387     private class Ring {
388         private int maxCount;
389         private int count;
390         private LinkedList JavaDoc anchor;
391         
392         public Ring(int max) {
393             maxCount = max;
394             count = 0;
395             anchor = new LinkedList JavaDoc();
396         }
397         
398         public String JavaDoc add(String JavaDoc line) {
399             if (line == null || line.equals("")) { // NOI18N
400
return null;
401             }
402             
403             while (count >= maxCount) {
404                 anchor.removeFirst();
405                 count--;
406             }
407             
408             anchor.addLast(line);
409             count++;
410             
411             return line;
412         }
413         
414         public void setMaxCount(int newMax) {
415             maxCount = newMax;
416         }
417         
418         public int output() {
419             int i = 0;
420             Iterator JavaDoc it = anchor.iterator();
421             
422             while (it.hasNext()) {
423                 printLine((String JavaDoc)it.next());
424                 i++;
425             }
426             return i;
427         }
428         
429         public void reset() {
430             anchor = new LinkedList JavaDoc();
431         }
432     }
433     
434 }
435
Popular Tags