KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > rcp > exceptionhandler > wizard > ErrorReport


1 /*
2  * Created on 04.04.2005
3  */

4 package com.nightlabs.rcp.exceptionhandler.wizard;
5
6 import java.io.InputStream JavaDoc;
7 import java.io.InputStreamReader JavaDoc;
8 import java.io.OutputStream JavaDoc;
9 import java.io.PrintWriter JavaDoc;
10 import java.io.Serializable JavaDoc;
11 import java.io.StringWriter JavaDoc;
12 import java.text.SimpleDateFormat JavaDoc;
13 import java.util.Date JavaDoc;
14 import java.util.Properties JavaDoc;
15
16 import com.nightlabs.io.DataBuffer;
17
18 /**
19  * @author Simon Lehmann - simon@nightlabs.de
20  */

21 public class ErrorReport
22 implements Serializable JavaDoc
23 {
24     private Throwable JavaDoc thrownException;
25     private Throwable JavaDoc triggerException;
26     private String JavaDoc userComment;
27     private Properties JavaDoc systemProperties;
28 // private Calendar calendar;
29

30     protected ErrorReport()
31     {
32     }
33
34     public ErrorReport(Throwable JavaDoc thrownException, Throwable JavaDoc triggerException)
35     {
36         setThrownException(thrownException);
37         setTriggerException(triggerException);
38 // setUserComment(userComment);
39
this.systemProperties = System.getProperties();
40         
41     }
42
43     /**
44      * @return Returns the thrownException.
45      */

46     public Throwable JavaDoc getThrownException()
47     {
48         return thrownException;
49     }
50     /**
51      * @param thrownException The thrownException to set.
52      */

53     public void setThrownException(Throwable JavaDoc error)
54     {
55         if (error == null)
56             throw new NullPointerException JavaDoc("Parameter thrownException must not be null!");
57         this.thrownException = error;
58     }
59     /**
60      * @return Returns the userComment.
61      */

62     public String JavaDoc getUserComment()
63     {
64         return userComment;
65     }
66     /**
67      * @param userComment The userComment to set.
68      */

69     public void setUserComment(String JavaDoc userComment)
70     {
71         this.userComment = userComment;
72     }
73     public Properties JavaDoc getSystemProperties()
74     {
75         return systemProperties;
76     }
77     public void setSystemProperties(Properties JavaDoc systemProperties)
78     {
79         this.systemProperties = systemProperties;
80     }
81     /**
82      * Formats the thrownException report into sth. like this:
83      *
84      * ---
85      * User Comment:
86      * bla bla bla
87      *
88      * Error:
89      * java.lang.Exception: shfgiushf
90      * at xxx
91      * ---
92      *
93      * @see java.lang.Object#toString()
94      */

95     public String JavaDoc toString()
96     {
97         StringBuffer JavaDoc props = new StringBuffer JavaDoc();
98         try {
99             DataBuffer db = new DataBuffer(1024);
100             OutputStream JavaDoc out = db.createOutputStream();
101             systemProperties.storeToXML(out, "");
102             out.close();
103     
104             InputStream JavaDoc in = db.createInputStream();
105             InputStreamReader JavaDoc reader = new InputStreamReader JavaDoc(in, "UTF-8");
106             while (reader.ready()) {
107                 props.append((char)reader.read());
108             }
109         } catch (Exception JavaDoc x) {
110             props.append("Fuck, dumping the properties failed: " + x.getMessage());
111         }
112
113 // StringBuffer props = new StringBuffer();
114
// for (Iterator it = systemProperties.entrySet().iterator(); it.hasNext(); ) {
115
// Map.Entry me = (Map.Entry)it.next();
116
// props.append(me.getKey());
117
// props.append('=');
118
// props.append(me.getValue());
119
// props.append('\n');
120
// }
121

122         return
123                 "Time:\n"+ getCurrentTimeAsString() +"\n\nUser Comment:\n" + userComment +
124                 "\n\nError:\n" + getErrorStackTraceAsString() +
125                 "\nSystem Properties:\n" + props.toString();
126     }
127     
128     public String JavaDoc getErrorStackTraceAsString()
129     {
130         StringWriter JavaDoc sw = new StringWriter JavaDoc();
131         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
132         thrownException.printStackTrace(pw);
133         pw.close();
134         return sw.getBuffer().toString();
135     }
136     
137     
138     public String JavaDoc getCurrentTimeAsString()
139     {
140         SimpleDateFormat JavaDoc bartDateFormat =
141     new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss");
142         String JavaDoc timestring = new String JavaDoc();
143         Date JavaDoc date = new Date JavaDoc();
144         timestring =bartDateFormat.format(date);
145         return timestring;
146
147     }
148         
149
150                 
151         
152         
153         
154 // public static void test1() throws IOException
155
// {
156
// throw new IOException("bla");
157
// }
158
// public static void test0() throws IOException
159
// {
160
// test1();
161
// }
162
// public static void invoke()
163
// {
164
// try {
165
// test0();
166
// } catch (IOException e) {
167
// throw new IllegalArgumentException(e);
168
// }
169
// }
170
// public static void main(String[] a1)
171
// {
172
// try {
173
// invoke();
174
// } catch (Exception e) {
175
// ErrorReport er = new ErrorReport(e, "This shitty program crashed another time!");
176
// System.out.println(er.toString());
177
// }
178
// }
179
public Throwable JavaDoc getTriggerException()
180     {
181         return triggerException;
182     }
183     public void setTriggerException(Throwable JavaDoc triggerException)
184     {
185         if (triggerException == null)
186             throw new NullPointerException JavaDoc("Parameter triggerException must not be null!");
187             this.triggerException = triggerException;
188     }
189 }
190
Popular Tags