1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.tomahawk.util;
20
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
24 import java.text.DateFormat;
25 import java.util.Date;
26
27 import javax.faces.component.UIComponent;
28 import javax.faces.context.FacesContext;
29
30
31
32
33
34
35
36
37
38 public class ExceptionContext
39 {
40
41 private Throwable _exception;
42
43 private UIComponent _viewRoot;
44
45 private String _cause;
46
47 private String _tree;
48
49 private String _vars;
50
51 public ExceptionContext(Throwable _exception, UIComponent viewRoot)
52 {
53 super();
54 this._exception = _exception;
55 this._viewRoot = viewRoot;
56 }
57
58 public void setException(Throwable exception)
59 {
60 this._exception = exception;
61 }
62
63 public Throwable getException()
64 {
65 return _exception;
66 }
67
68 public String getCause()
69 {
70 if (_cause == null)
71 {
72 StringWriter writer = null;
73 try
74 {
75 writer = new StringWriter(256);
76 ErrorPageWriter.writeCause(writer, _exception);
77 }
78 catch (IOException e)
79 {
80
81 }
82 finally
83 {
84 try
85 {
86 if (writer != null)
87 writer.close();
88 }
89 catch (IOException e)
90 {
91
92 }
93 }
94 _cause = writer.toString();
95 }
96 return _cause;
97 }
98
99 public String getStackTrace()
100 {
101 StringWriter str = new StringWriter(256);
102 PrintWriter pstr = new PrintWriter(str);
103 _exception.printStackTrace(pstr);
104 pstr.close();
105 return str.toString();
106 }
107
108 public String getNow()
109 {
110 Date now = new Date();
111 return DateFormat.getDateTimeInstance().format(now);
112 }
113
114 public UIComponent getViewRoot()
115 {
116 return _viewRoot;
117 }
118
119 public void setViewRoot(UIComponent viewRoot)
120 {
121 this._viewRoot = viewRoot;
122 }
123
124 public String getVars()
125 {
126 if (_vars == null)
127 {
128 StringWriter writer = null;
129 try
130 {
131 writer = new StringWriter(256);
132 ErrorPageWriter.writeVariables(writer, FacesContext.getCurrentInstance());
133 _vars = writer.toString();
134 }
135 catch (IOException e)
136 {
137
138 }
139 finally
140 {
141 try
142 {
143 if (writer != null)
144 writer.close();
145 }
146 catch (IOException e)
147 {
148
149 }
150 }
151 }
152 return _vars;
153 }
154
155 public String getTree()
156 {
157 if (_tree == null)
158 {
159 if (_viewRoot != null)
160 {
161 StringWriter writer = null;
162 try
163 {
164 writer = new StringWriter(256);
165 ErrorPageWriter.writeComponent(writer, _viewRoot,
166 ErrorPageWriter.getErrorId(_exception));
167 _tree = writer.toString();
168 }
169 catch (IOException e)
170 {
171
172 }
173 finally
174 {
175 try
176 {
177 if (writer != null)
178 writer.close();
179 }
180 catch (IOException e)
181 {
182
183 }
184 }
185 }
186 }
187 return _tree;
188 }
189
190 }