KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > spi > ThrowableInformation


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.spi;
18
19 import java.io.Writer JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 /**
24   * ThrowableInformation is log4j's internal representation of
25   * throwables. It essentially consists of a string array, called
26   * 'rep', where the first element, that is rep[0], represents the
27   * string representation of the throwable (i.e. the value you get
28   * when you do throwable.toString()) and subsequent elements
29   * correspond the stack trace with the top most entry of the stack
30   * corresponding to the second entry of the 'rep' array that is
31   * rep[1].
32   *
33   * @author Ceki Gülcü
34   *
35   * */

36 public class ThrowableInformation implements java.io.Serializable JavaDoc {
37
38   static final long serialVersionUID = -4748765566864322735L;
39
40   private transient Throwable JavaDoc throwable;
41   private String JavaDoc[] rep;
42   
43   public
44   ThrowableInformation(Throwable JavaDoc throwable) {
45     this.throwable = throwable;
46   }
47
48   public
49   Throwable JavaDoc getThrowable() {
50     return throwable;
51   }
52   
53   public
54   String JavaDoc[] getThrowableStrRep() {
55     if(rep != null) {
56       return (String JavaDoc[]) rep.clone();
57     } else {
58       VectorWriter vw = new VectorWriter();
59       throwable.printStackTrace(vw);
60       rep = vw.toStringArray();
61       return rep;
62     }
63   }
64 }
65
66 /**
67   * VectorWriter is a seemingly trivial implemtantion of PrintWriter.
68   * The throwable instance that we are trying to represnt is asked to
69   * print itself to a VectorWriter.
70   *
71   * By our design choice, r string representation of the throwable
72   * does not contain any line separators. It follows that println()
73   * methods of VectorWriter ignore the 'ln' part.
74   * */

75 class VectorWriter extends PrintWriter JavaDoc {
76     
77   private Vector JavaDoc v;
78   
79   VectorWriter() {
80     super(new NullWriter());
81     v = new Vector JavaDoc();
82   }
83
84   public void print(Object JavaDoc o) {
85     v.addElement(o.toString());
86   }
87   
88   public void print(char[] chars) {
89     v.addElement(new String JavaDoc(chars));
90   }
91   
92   public void print(String JavaDoc s) {
93     v.addElement(s);
94   }
95
96   public void println(Object JavaDoc o) {
97     v.addElement(o.toString());
98   }
99   
100   // JDK 1.1.x apprenly uses this form of println while in
101
// printStackTrace()
102
public
103   void println(char[] chars) {
104     v.addElement(new String JavaDoc(chars));
105   }
106   
107   public
108   void println(String JavaDoc s) {
109     v.addElement(s);
110   }
111
112   public void write(char[] chars) {
113     v.addElement(new String JavaDoc(chars));
114   }
115
116   public void write(char[] chars, int off, int len) {
117     v.addElement(new String JavaDoc(chars, off, len));
118   }
119
120   public void write(String JavaDoc s, int off, int len) {
121     v.addElement(s.substring(off, off+len));
122   }
123
124   public void write(String JavaDoc s) {
125      v.addElement(s);
126   }
127
128   public String JavaDoc[] toStringArray() {
129     int len = v.size();
130     String JavaDoc[] sa = new String JavaDoc[len];
131     for(int i = 0; i < len; i++) {
132       sa[i] = (String JavaDoc) v.elementAt(i);
133     }
134     return sa;
135   }
136
137 }
138
139 class NullWriter extends Writer JavaDoc {
140   
141   public void close() {
142     // blank
143
}
144
145   public void flush() {
146     // blank
147
}
148
149   public void write(char[] cbuf, int off, int len) {
150     // blank
151
}
152 }
153
154
Popular Tags