KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > simulator > app > ErrorContext


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.simulator.app;
5
6 import java.io.PrintStream JavaDoc;
7 import java.io.PrintWriter JavaDoc;
8 import java.io.StringWriter JavaDoc;
9
10 public class ErrorContext {
11   private final String JavaDoc message;
12   private final Thread JavaDoc thread;
13   private Throwable JavaDoc throwable;
14
15   public ErrorContext(String JavaDoc message) {
16     this.message = message;
17     this.thread = Thread.currentThread();
18   }
19
20   public ErrorContext(String JavaDoc message, Throwable JavaDoc throwable) {
21     this(message);
22     this.throwable = throwable;
23   }
24
25   public ErrorContext(Throwable JavaDoc t) {
26     this("", t);
27   }
28
29   public String JavaDoc getMessage() {
30     return message;
31   }
32
33   public Thread JavaDoc getThread() {
34     return thread;
35   }
36
37   public Throwable JavaDoc getThrowable() {
38     return throwable;
39   }
40
41   public String JavaDoc toString() {
42
43     StringWriter JavaDoc sw = new StringWriter JavaDoc();
44     PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
45     dump(pw);
46     pw.flush();
47     return sw.getBuffer().toString();
48   }
49
50   // Ok. It's totally retarted to have these two dump methods, but I can't for the life of me figure out
51
// how collapse the two methods together. The java.io package has me stumped at the moment
52
// --Orion
53

54   public void dump(PrintWriter JavaDoc out) {
55     out.println(getClass().getName() + " [" + message + ", thread: " + thread + "]");
56     if (throwable != null) {
57       throwable.printStackTrace(out);
58     }
59   }
60
61   public void dump(PrintStream JavaDoc out) {
62     out.println(getClass().getName() + " [" + message + ", thread: " + thread + "]");
63     if (throwable != null) {
64       throwable.printStackTrace(out);
65     }
66   }
67 }
Popular Tags