KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > exceptions > ExceptionsHandler


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.exceptions;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.logging.Handler JavaDoc;
25 import java.util.logging.LogRecord JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import org.openide.windows.WindowManager;
28
29 /**
30  *
31  * @author jindra
32  */

33 public class ExceptionsHandler extends Handler JavaDoc{
34     //---------------prom --------------//
35
private NotifyDialog notifyDialog = null;
36     private LinkedList JavaDoc<LinkedList JavaDoc<LogRecord JavaDoc>> transport = null;
37     private static ExceptionsHandler defaultHandler = new ExceptionsHandler();
38     
39     //------------constructors-------------//
40
/** Creates a new instance of NbErrorManager */
41     private ExceptionsHandler() {
42     }
43     
44     //--------------------Handler interface------------//
45
public void publish(final LogRecord JavaDoc rec) {
46         if (rec.getThrown() != null){
47             ExceptionLogger.logError(rec);
48         }
49         if ((rec.getThrown() != null)&&(rec.getLevel().intValue() >= Level.INFO.intValue())){
50             LinkedList JavaDoc<LogRecord JavaDoc> recs = new LinkedList JavaDoc<LogRecord JavaDoc>();
51             for (Iterator JavaDoc<LogRecord JavaDoc> it = Collector.getDefault().getQueue().iterator(); it.hasNext();) {
52                 recs.addLast(it.next()); // shadow clone
53
}
54             recs.addFirst(rec);// just for displaying - removed before send
55
addRecord(recs);
56             WindowManager.getDefault().invokeWhenUIReady(new Runnable JavaDoc(){
57                 public void run(){
58                     if (notifyDialog == null) notifyDialog = new NotifyDialog();
59                     notifyDialog.notify(transport);
60                     if (rec.getLevel().intValue() > Level.INFO.intValue()) notifyDialog.setVisible(true);
61                 }
62             });
63         }
64         if ((rec.getThrown()== null)&&(rec.getLevel().intValue() >= Level.WARNING.intValue())) ExceptionLogger.log(rec);
65     }
66     
67     public void showNotifyDialog(){
68         if (notifyDialog == null) notifyDialog = new NotifyDialog();
69         notifyDialog.setVisible(true);
70     }
71     
72     
73     public static boolean equalsThrows(Throwable JavaDoc thr1, Throwable JavaDoc thr2){// true => are the same
74
// not null
75
if ((thr1 == null) && (thr2 == null)) return true;// both of them are null
76
if ((thr1 == null) || (thr2 == null)) return false;// just one of them is null
77
// compaire stack traces
78
int i;
79         StackTraceElement JavaDoc[] recStack = thr1.getStackTrace();
80         StackTraceElement JavaDoc[] st1 = thr2.getStackTrace();
81         if (recStack.length == st1.length){
82             i = 0;
83             while (i < st1.length){
84                 if (st1[i].toString().equals(recStack[i].toString())){
85                     ++i;
86                 } else i=st1.length + 10;//break while cycle
87
}
88             if (i==st1.length){// while cycle finished and found no difference
89
if ((thr1.getCause()!= null)||(thr2.getCause()!= null)){
90                     return equalsThrows(thr1.getCause(), thr2.getCause());
91                 }else return true;
92             }
93         }
94         return false;
95     }
96     
97     
98     
99     public static synchronized ExceptionsHandler getInstance() {
100         return defaultHandler;
101     }
102     
103     public void flush() {
104         // do nothing
105
}
106     
107     public void close() throws SecurityException JavaDoc {
108         // do nothing
109
}
110     
111     
112     //-------------------private --------------------//
113
private void addRecord(LinkedList JavaDoc<LogRecord JavaDoc> rec) {
114         if (transport == null) transport = new LinkedList JavaDoc<LinkedList JavaDoc<LogRecord JavaDoc>>();
115         transport.add(rec);
116     }
117     
118 }
119
Popular Tags