1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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 * The ExceptionContext class holds all information related to an error
32 * ocurred in a jsf environment, handled by a redirection using
33 * ErrorRedirectJSFPageHandler.
34 *
35 * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
36 * @version $Revision: 689684 $ $Date: 2008-08-27 19:50:09 -0500 (Wed, 27 Aug 2008) $
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 //do nothing
81 }
82 finally
83 {
84 try
85 {
86 if (writer != null)
87 writer.close();
88 }
89 catch (IOException e)
90 {
91 //do nothing
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 //do nothing
138 }
139 finally
140 {
141 try
142 {
143 if (writer != null)
144 writer.close();
145 }
146 catch (IOException e)
147 {
148 //do nothing
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 //do nothing
172 }
173 finally
174 {
175 try
176 {
177 if (writer != null)
178 writer.close();
179 }
180 catch (IOException e)
181 {
182 //do nothing
183 }
184 }
185 }
186 }
187 return _tree;
188 }
189
190 }