KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > oc4j > ide > OC4JLogger


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.oc4j.ide;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import org.netbeans.modules.j2ee.deployment.plugins.api.UISupport;
30 import org.openide.ErrorManager;
31 import org.openide.util.RequestProcessor;
32 import org.openide.windows.InputOutput;
33
34 /**
35  * This class is capable of tailing the specified file or input stream. It
36  * checks for changes at the specified intervals and outputs the changes to
37  * the given I/O panel in NetBeans
38  *
39  * @author Michal Mocnak
40  */

41 public class OC4JLogger {
42     
43     /**
44      * Amount of time in milliseconds to wait between checks of the input
45      * stream
46      */

47     private static final int delay = 1000;
48     
49     /**
50      * Singleton model pattern
51      */

52     private static Map JavaDoc<String JavaDoc, OC4JLogger> instances = new HashMap JavaDoc<String JavaDoc, OC4JLogger>();
53     
54     /**
55      * The I/O window where to output the changes
56      */

57     private InputOutput io;
58     
59     /**
60      * Creates and starts a new instance of OC4JLogger
61      *
62      * @param uri the uri of the server
63      */

64     private OC4JLogger(String JavaDoc uri) {
65         io = UISupport.getServerIO(uri);
66         
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 JavaDoc ioe) {
75             // no op
76
}
77         
78         io.select();
79     }
80     
81     /**
82      * Returns uri specific instance of OC4JLogger
83      *
84      * @param uri the uri of the server
85      * @return uri specific instamce of OC4JLogger
86      */

87     public static OC4JLogger getInstance(String JavaDoc uri) {
88         if (!instances.containsKey(uri))
89             instances.put(uri, new OC4JLogger(uri));
90         
91         return instances.get(uri);
92     }
93     
94     /**
95      * Reads a newly included InputSreams
96      *
97      * @param inputStreams InputStreams to read
98      */

99     public void readInputStreams(InputStream JavaDoc[] inputStreams) {
100         for(InputStream JavaDoc inputStream : inputStreams)
101             RequestProcessor.getDefault().post(new OC4JLoggerRunnable(inputStream));
102     }
103     
104     /**
105      * Reads a newly included Files
106      *
107      * @param files Files to read
108      */

109     public void readFiles(File JavaDoc[] files) {
110         for(InputStream JavaDoc inputStream : getInputStreamsFromFiles(files))
111             RequestProcessor.getDefault().post(new OC4JLoggerRunnable(inputStream));
112     }
113     
114     /**
115      * Writes a message into output
116      *
117      * @param s message to write
118      */

119     public synchronized void write(String JavaDoc s) {
120         io.getOut().println(s);
121     }
122     
123     /**
124      * Selects output panel
125      */

126     public synchronized void selectIO() {
127         io.select();
128     }
129     
130     private static InputStream JavaDoc[] getInputStreamsFromFiles(File JavaDoc[] files) {
131         InputStream JavaDoc[] inputStreams = new InputStream JavaDoc[files.length];
132         
133         try {
134             for(int i=0 ; i<files.length ; i++)
135                 inputStreams[i] = new FileInputStream JavaDoc(files[i]);
136         } catch(FileNotFoundException JavaDoc ex) {
137             return new InputStream JavaDoc[] {};
138         }
139         
140         return inputStreams;
141     }
142     
143     private class OC4JLoggerRunnable implements Runnable JavaDoc {
144         
145         private InputStream JavaDoc inputStream;
146         
147         public OC4JLoggerRunnable(InputStream JavaDoc inputStream) {
148             this.inputStream = inputStream;
149         }
150         
151         /**
152          * Implementation of the Runnable interface. Here all tailing is
153          * performed
154          */

155         public void run() {
156             try {
157                 // create a reader from the input stream
158
InputStreamReader JavaDoc reader = new InputStreamReader JavaDoc(inputStream);
159                 
160                 // read from the input stream and put all the changes to the
161
// I/O window
162
char[] chars = new char[1024];
163                 while (true) {
164                     // while there is something in the stream to be read - read that
165
while (reader.ready()) {
166                         write(new String JavaDoc(chars, 0, reader.read(chars)));
167                         selectIO();
168                     }
169                     
170                     // when the stream is empty - sleep for a while
171
try {
172                         Thread.sleep(delay);
173                     } catch (InterruptedException JavaDoc e) {
174                         // do nothing
175
}
176                 }
177             } catch (IOException JavaDoc e) {
178                 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
179             } finally {
180                 // close the opened stream
181
try {
182                     inputStream.close();
183                 } catch (IOException JavaDoc e) {
184                     ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
185                 }
186             }
187         }
188     }
189 }
Popular Tags