KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > websphere6 > util > WSTailer


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 package org.netbeans.modules.j2ee.websphere6.util;
20
21 import java.io.*;
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.openide.*;
27 import org.openide.windows.*;
28
29 /**
30  * This class is capable of tailing the specified file or input stream. It
31  * checks for changes at the specified intervals and outputs the changes to
32  * the given I/O panel in NetBeans
33  *
34  * @author Kirill Sorokin
35  */

36 public class WSTailer extends Thread JavaDoc {
37     
38     /**
39      * Amount of time in milliseconds to wait between checks of the input
40      * stream
41      */

42     private static final int delay = 1000;
43         
44     private static Map JavaDoc isIOPanelOpen = Collections.synchronizedMap((Map JavaDoc)new HashMap JavaDoc(2,1));
45     
46     /**
47      * The file for which to track changes
48      */

49     private File file;
50     
51     /**
52      * The input stream for which to track changes
53      */

54     private InputStream inputStream;
55     
56     /**
57      * The I/O window where to output the changes
58      */

59     private InputOutput io;
60     
61     /**
62      * Creates a new instance of WSTailer
63      *
64      * @param file the file for which to track changes
65      * @param ioPanelName the I/O window where to output the changes
66      */

67     public WSTailer(File file, String JavaDoc ioPanelName) {
68         // save the parameters
69
this.file = file;
70         this.io = IOProvider.getDefault().getIO(ioPanelName, false);
71     }
72     
73     /**
74      * Creates a new instance of WSTailer
75      *
76      * @param file the input stream for which to track changes
77      * @param ioPanelName the I/O window where to output the changes
78      */

79     public WSTailer(InputStream inputStream, String JavaDoc ioPanelName) {
80         // save the parameters
81
this.inputStream = inputStream;
82         this.io = IOProvider.getDefault().getIO(ioPanelName, false);
83     }
84     
85     /**
86      * Implementation of the Runnable interface. Here all tailing is
87      * performed
88      */

89     public void run() {
90         if(isIOPanelOpen.containsKey(io)) {
91             return;
92         }
93         
94         isIOPanelOpen.put(io, new Object JavaDoc());
95         try {
96             
97             if(io.isClosed()) {
98                 io.getOut().reset();
99             }
100             this.io.select();
101             
102             // check the source for the tailing, if it is a file we create a
103
// new FileInputStream
104
if (file != null) {
105                 while (inputStream == null) {
106                     try {
107                         inputStream = new FileInputStream(file);
108                     } catch (IOException e) {
109                         try {
110                             Thread.sleep(delay);
111                         } catch (InterruptedException JavaDoc ex) {
112                             continue;
113                         }
114                     }
115                 }
116             }
117             // create a reader from the input stream
118
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
119             // read from the input stream and put all the changes to the
120
// I/O window
121
String JavaDoc line;
122             while (!io.isClosed()) {
123                 // while there is something in the stream to be read - read that
124
while (reader.ready()) {
125                     line = reader.readLine();
126                     if(line!=null) {
127                         io.getOut().println(line);
128                         io.getOut().flush();
129                     }
130                 }
131                 
132                 // when the stream is empty - sleep for a while
133
try {
134                     Thread.sleep(delay);
135                 } catch (InterruptedException JavaDoc e) {
136                     // do nothing
137
}
138             }
139         } catch (FileNotFoundException e) {
140             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
141             return;
142         } catch (IOException e) {
143             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
144         } finally {
145             // close the opened stream
146
try {
147                 isIOPanelOpen.remove(io);
148                 inputStream.close();
149             } catch (IOException e) {
150                 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
151             }
152         }
153     }
154     
155 }
156
Popular Tags