KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > logger > listeners > ListenerTools


1 /*
2  * ListenerTools.java
3  *
4  * Created on April 5, 2005, 2:23 PM
5  */

6
7 /**
8  *
9  * @author loicsegapelli
10  *
11  */

12
13 package org.netbeans.modules.logger.listeners;
14
15 import java.io.*;
16 import java.util.Properties JavaDoc;
17 import org.netbeans.modules.logger.UserInput;
18
19
20 /**
21  * Provides subclasses listeners with common methods concerning writing their
22  * output into <CODE>.log</CODE> files.
23  *
24  * Also contains <CODE>final</CODE> variables useful for other classes.
25  */

26 public abstract class ListenerTools {
27     
28     /**
29      * We use a buffer to write into files. The reason for this is that sometimes
30      * <CODE>listeners</CODE> receive an <CODE>event</CODE> and start to log its
31      * "properties" but eventually find out it is not interesting so they need to
32      * cancel what they were logging.
33      */

34     private StringBuffer JavaDoc buffer;
35     
36     /**
37      * user name; retrieved from the form
38      */

39     private static String JavaDoc name;
40
41     /**
42      * user level; retrieved from the form
43      */

44     private static String JavaDoc level;
45     
46     
47     /**
48      * id for filename. For instance, <CODE>TopComponent.Registery</CODE>
49      * listener asks for <I>TCR</I>, <CODE>WindowManager</CODE> for
50      * <I>WM</I>
51      */

52     private String JavaDoc id;
53     
54     /**
55      * timestamp; indicates at what time the session started. Used as an identifier
56      * for <CODE>.log</CODE> files
57      */

58     public static final String JavaDoc TS = Long.toString(System.currentTimeMillis());
59     /**
60      * file separator specific to the OS
61      */

62     public static final String JavaDoc SEP = System.getProperty("file.separator");
63     /**
64      * path directing to the directory where Logger Module output files are stored, ie
65      * <I>userdir</I>/var/log/ui
66      */

67     public static final String JavaDoc LOG_PATH = findOutLogPath();
68     /**
69      * path directing to the directory where Logger Module output files are stored, ie
70      * <I>userdir</I>/config/Windows2Local/Modes/
71      */

72     public static final String JavaDoc LAYOUT_PATH = findOutLayoutPath();
73     
74     
75     /**
76      * <CODE>errorOut</CODE> is specific for the whole class, hence it is
77      * <CODE>static</CODE>. Used to log <CODE>Error</CODE>s and
78      * <CODE>Exception</CODE>s
79      */

80     private static FileWriter errorOut;
81     
82     
83     /**
84      * specific writer for each subclass of <CODE>ListenerTools</CODE>
85      */

86     private FileWriter out;
87     
88     
89     /**
90      * Creates a new instance of <CODE>ListenerTools</CODE>
91      * @param who some identifier for the output file
92      */

93     public ListenerTools(String JavaDoc who) {
94         if(name==null) findOutProperties();
95         id = who;
96         buffer = new StringBuffer JavaDoc();
97         File outFile = new File(LOG_PATH+TS+"."+level+"."+name+"."+who+".log");
98         try{
99             out = new FileWriter(outFile);
100         } catch(IOException e){
101             logError(e);
102         }
103     }
104     
105     /**
106      * initializes the error <CODE>FileWriter</CODE>
107      */

108     private static void initErrorLog(){
109         File outFile = new File (LOG_PATH + TS + "." + level + "." + name + ".ERR.log");
110         try{
111             errorOut = new FileWriter(outFile);
112         } catch(IOException e){
113             // we cannot log into our err file... so let's print this in the terminal
114
System.out.println("Logger Module:"+e.getMessage());
115         }
116     }
117     
118     
119     /**
120      * tries to retrieve the user name and level entered during a previous session
121      * @return <CODE>true</CODE> if the property file is found; <CODE>false</CODE>
122      * otherwise
123      */

124     public static boolean findOutProperties(){
125         try{
126             String JavaDoc paramsPath = new String JavaDoc(LOG_PATH+"properties");
127             File f = new File(paramsPath);
128             if(f.exists()){
129                 FileInputStream input = new FileInputStream(f);
130                 Properties JavaDoc p = new Properties JavaDoc();
131                 p.load(input);
132                 name = p.getProperty("name");
133                 level = p.getProperty("level");
134                 input.close();
135                 return true;
136             } else{
137                 name = "name";
138                 level = UserInput.LEVEL2.split("\\s+")[0];
139             }
140         } catch(FileNotFoundException e){
141             logError(e);
142         } catch(IOException e){
143             logError(e);
144         }
145         return false;
146     }
147     
148     /**
149      * Saves user input (<I>name</I> and <I>level</I>) into a <I>properties</I>
150      * file
151      * @param myLevel user level of experience using NB; entered in the form
152      * @param myName username/nickname; entered in the form
153      */

154     public static void saveParameters(String JavaDoc myLevel, String JavaDoc myName){
155         level = myLevel;
156         name = myName;
157         try{
158             String JavaDoc paramsPath = new String JavaDoc(LOG_PATH+"properties");
159             File f = new File(paramsPath);
160             FileOutputStream output = new FileOutputStream(f);
161             Properties JavaDoc p = new Properties JavaDoc();
162             p.setProperty("level", myLevel);
163             p.setProperty("name", myName);
164             p.store(output, null);
165             output.close();
166         } catch(IOException e){
167             logError(e);
168         }
169     }
170     
171     /**
172      * used for initializing <CODE>LOG_PATH</CODE>
173      * @return value for <CODE>LOG_PATH</CODE>
174      */

175     private static String JavaDoc findOutLogPath(){
176         String JavaDoc path = System.getProperty("netbeans.user");
177         path = path + SEP + "var" + SEP + "log" + SEP + "ui" + SEP;
178         File f = new File(path);
179         f.mkdirs();
180         return path;
181     }
182     
183     /**
184      * used for initializing <CODE>LAYOUT_PATH</CODE>
185      * @return value for <CODE>LAYOUT_PATH</CODE>
186      */

187     private static String JavaDoc findOutLayoutPath(){
188         String JavaDoc path=System.getProperty("netbeans.user");
189         path=path+SEP+"config"+SEP+"Windows2Local"+SEP+"Modes"+SEP;
190         File f = new File(path);
191         f.mkdirs();
192         return path;
193     }
194     
195     
196     /**
197      * given an object, returns a String corresponding to the classname of the object.
198      * @param o the object whose classname should be retrieved
199      * @return class name of object o
200      */

201     protected String JavaDoc getClassName(Object JavaDoc o){
202         if(o==null) return "null";
203         String JavaDoc s = o.getClass().getName();
204         return s.substring(s.lastIndexOf('.')+1);
205     }
206     
207     /**
208      * close the file output
209      */

210     public void close(){
211         try{
212             out.close();
213             errorOut.close();
214         }catch(IOException e){
215             System.out.println("Logger Module:"+e.getMessage());
216         }
217     }
218     
219     /**
220      * creates a new buffer
221      */

222     protected void newBuffer(){
223         buffer = new StringBuffer JavaDoc();
224     }
225     
226     /**
227      * appends a string to the output buffer
228      * @param s the string to append
229      */

230     protected void append(String JavaDoc s){
231         if(buffer!=null) buffer.append("\n"+s);
232     }
233     
234     /**
235      * erases buffer content
236      */

237     protected void erase(){
238         buffer = null;
239     }
240     
241     /**
242      * writes buffer content into the output
243      */

244     protected void flush(){
245         if(buffer!=null){
246             try{
247                 out.write(buffer.toString()+"\n");
248                 out.flush();
249             }catch(IOException e){
250                 logError(e);
251             }
252         }
253         buffer=null;
254     }
255     
256     /**
257      * if some <CODE>Error</CODE> or <CODE>Exception</CODE> has been caught within
258      * the Logger Module it will be logged here so that we can correct it in a further
259      * version of our module
260      * @param t a throwable: either <CODE>Error</CODE> or <CODE>Exception</CODE>
261      */

262     public static void logError(Throwable JavaDoc t){
263         t.printStackTrace(System.out);
264         if(errorOut==null){
265             initErrorLog();
266         }
267         
268         StackTraceElement JavaDoc element [] = t.getStackTrace();
269         
270         try{
271             errorOut.write(t.toString());
272             for(int i=0; i<element.length; i++){
273                 errorOut.write(element[i].toString()+"\n");
274             }
275             errorOut.write("\n");
276             errorOut.flush();
277         }catch(IOException e){
278             // well, we cannot log this one, can we?
279
// use System.out.println instead
280
System.out.println("LoggerModule:"+e.getMessage());
281         }
282     }
283     
284 }
285
Popular Tags