1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.spi;
19
20 import java.io.IOException;
21 import java.io.LineNumberReader;
22 import java.io.PrintWriter;
23 import java.io.StringReader;
24 import java.io.StringWriter;
25 import java.util.ArrayList;
26
27 /***
28 * ThrowableInformation is log4j's internal representation of
29 * throwables. It essentially consists of a string array, called
30 * 'rep', where the first element, that is rep[0], represents the
31 * string representation of the throwable (i.e. the value you get
32 * when you do throwable.toString()) and subsequent elements
33 * correspond the stack trace with the top most entry of the stack
34 * corresponding to the second entry of the 'rep' array that is
35 * rep[1].
36 *
37 * @author Ceki Gülcü
38 *
39 * */
40 public class ThrowableInformation implements java.io.Serializable {
41
42 static final long serialVersionUID = -4748765566864322735L;
43
44 private transient Throwable throwable;
45 private String[] rep;
46
47 public
48 ThrowableInformation(Throwable throwable) {
49 this.throwable = throwable;
50 }
51
52 /***
53 * Create new instance.
54 * @since 1.2.15
55 * @param r String representation of throwable.
56 */
57 public ThrowableInformation(final String[] r) {
58 if (r != null) {
59 rep = (String[]) r.clone();
60 }
61 }
62
63
64 public
65 Throwable getThrowable() {
66 return throwable;
67 }
68
69 public
70 String[] getThrowableStrRep() {
71 if(rep != null) {
72 return (String[]) rep.clone();
73 } else {
74 StringWriter sw = new StringWriter();
75 PrintWriter pw = new PrintWriter(sw);
76 throwable.printStackTrace(pw);
77 pw.flush();
78 LineNumberReader reader = new LineNumberReader(
79 new StringReader(sw.toString()));
80 ArrayList lines = new ArrayList();
81 try {
82 String line = reader.readLine();
83 while(line != null) {
84 lines.add(line);
85 line = reader.readLine();
86 }
87 } catch(IOException ex) {
88 lines.add(ex.toString());
89 }
90 rep = new String[lines.size()];
91 lines.toArray(rep);
92 }
93 return rep;
94 }
95 }
96
97