KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > weblogic9 > util > WLTailer


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

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

40     private static final int delay = 1000;
41     
42     /**
43      * The file for which to track changes
44      */

45     private File file;
46     
47     /**
48      * The input stream for which to track changes
49      */

50     private InputStream inputStream;
51     
52     /**
53      * The I/O window where to output the changes
54      */

55     private InputOutput io;
56     
57     /**
58      * Creates and starts a new instance of WSTailer
59      *
60      * @param file the file for which to track changes
61      * @param ioPanelName the I/O window where to output the changes
62      */

63     public WLTailer(File file, String JavaDoc uri) {
64         // save the parameters
65
this.file = file;
66         io = UISupport.getServerIO(uri);
67         if (io == null) {
68             return; // finish, it looks like this server instance has been unregistered
69
}
70
71         // clear the old output
72
try {
73             io.getOut().reset();
74         } catch (IOException ioe) {
75             // no op
76
}
77         io.select();
78         start();
79     }
80     
81     /**
82      * Creates and starts a new instance of WSTailer
83      *
84      * @param file the input stream for which to track changes
85      * @param ioPanelName the I/O window where to output the changes
86      */

87     public WLTailer(InputStream inputStream, String JavaDoc uri) {
88         // save the parameters
89
this.inputStream = inputStream;
90         io = UISupport.getServerIO(uri);
91         if (io == null) {
92             return; // finish, it looks like this server instance has been unregistered
93
}
94
95         // clear the old output
96
try {
97             io.getOut().reset();
98         } catch (IOException ioe) {
99             // no op
100
}
101         io.select();
102         start();
103     }
104     
105     /**
106      * Implementation of the Runnable interface. Here all tailing is
107      * performed
108      */

109     public void run() {
110         try {
111             // check the source for the tailing, if it is a file we create a
112
// new FileInputStream
113
if (file != null) {
114                 inputStream = new FileInputStream(file);
115             }
116             
117             // create a reader from the input stream
118
InputStreamReader reader = new InputStreamReader(inputStream);
119
120             // read from the input stream and put all the changes to the
121
// I/O window
122
char[] chars = new char[1024];
123             while (true) {
124                 // while there is something in the stream to be read - read that
125
while (reader.ready()) {
126                     io.getOut().println(new String JavaDoc(chars, 0, reader.read(chars)));
127                 }
128                 
129                 // when the stream is empty - sleep for a while
130
try {
131                     Thread.sleep(delay);
132                 } catch (InterruptedException JavaDoc e) {
133                     // do nothing
134
}
135             }
136         } catch (FileNotFoundException e) {
137             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
138             return;
139         } catch (IOException e) {
140             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
141         } finally {
142             // close the opened stream
143
try {
144                 inputStream.close();
145             } catch (IOException e) {
146                 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
147             }
148         }
149     }
150     
151 }
152
Popular Tags