KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > guiframework > event > handlers > ErrorHandler


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.guiframework.event.handlers;
25
26 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
27 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
28 import com.sun.enterprise.tools.guiframework.view.event.ErrorEvent;
29
30 import com.iplanet.jato.RequestContext;
31 import com.iplanet.jato.RequestManager;
32 import com.iplanet.jato.view.View;
33 import com.iplanet.jato.view.ViewBean;
34 import com.iplanet.jato.view.event.ChildContentDisplayEvent;
35 import com.iplanet.jato.view.event.JspDisplayEvent;
36
37 import java.util.EventObject JavaDoc;
38
39 import javax.servlet.ServletRequest JavaDoc;
40
41
42 /**
43  * This class provides a simple implementation for an error handler.
44  */

45 public class ErrorHandler {
46
47     /**
48      * <P>This method is valid only for ErrorEvent objects. It will cast to
49      * this event type and pull out all the error information. It will then
50      * return this information as the following output parameters:</P>
51      *
52      * <UL><LI>EXCEPTION -- The exception itself.</LI>
53      * <LI>EXCEPTION_MESSAGE -- The top exception message.</LI>
54      * <LI>EXCEPTION_CLASS_NAME -- The top exception class name.</LI>
55      * <LI>EXCEPTION_VIEW -- View which corresponds to the ViewDescriptor
56      * that is handling the Exception / Error.</LI>
57      * <LI>EXCEPTION_VIEW_NAME -- View which corresponds to the
58      * ViewDescriptor that is handling the Exception / Error.</LI>
59      * <LI>EXCEPTION_VIEW_DESCRIPTOR -- ViewDescriptor which is handling
60      * the Exception / Error.</LI>
61      * <LI>EXCEPTION_VIEW_DESCRIPTOR_NAME -- ViewDescriptor which is
62      * handling the Exception / Error.</LI>
63      * <LI>EXCEPTION_VIEW_BEAN -- The responsible ViewBean (may be same
64      * as View).</LI>
65      * <LI>EXCEPTION_VIEW_BEAN_NAME -- The responsible ViewBean (may be
66      * same as View).</LI>
67      * <LI>EXCEPTION_STACK_TRACE -- The regular stack trace</LI>
68      * <LI>EXCEPTION_CAUSE_VIEW -- The responsible View object.</LI>
69      * <LI>EXCEPTION_CAUSE_VIEW_DESCRIPTOR -- The responsible
70      * ViewDescriptor.</LI>
71      * <LI>EXCEPTION_CAUSE_MESSAGE -- The cause exception message (null if
72      * no chained exceptions).</LI>
73      * <LI>EXCEPTION_CAUSE_CLASS_NAME -- The cause exception class name
74      * (null if no chained exceptions).</LI>
75      * <LI>EXCEPTION_CAUSE_FULL_TRACE -- The full stack trace to the cause
76      * exception.</LI>
77      * <LI>REQUEST_PARAMETERS -- The parameters passed from the browser to
78      * the server.</LI></UL>
79      */

80     public void handleError(RequestContext reqCtx, HandlerContext handlerCtx) {
81     ErrorEvent errEvent = (ErrorEvent)handlerCtx.getEvent();
82
83     ServletRequest JavaDoc request =
84         RequestManager.getRequestContext().getRequest();
85
86     handlerCtx.setOutputValue(EXCEPTION,
87         errEvent.getException());
88
89     handlerCtx.setOutputValue(EXCEPTION_MESSAGE,
90         errEvent.getExceptionMessage());
91
92     handlerCtx.setOutputValue(EXCEPTION_CLASS_NAME,
93         errEvent.getExceptionClassName());
94
95     View vw = errEvent.getView();
96     handlerCtx.setOutputValue(EXCEPTION_VIEW, vw);
97
98     handlerCtx.setOutputValue(EXCEPTION_VIEW_NAME,
99         (vw == null) ? "(Null View)" : vw.getName());
100
101     ViewDescriptor desc = errEvent.getViewDescriptor();
102     handlerCtx.setOutputValue(EXCEPTION_VIEW_DESCRIPTOR, desc);
103
104     handlerCtx.setOutputValue(EXCEPTION_VIEW_DESCRIPTOR_NAME,
105         (desc == null) ? "(Null ViewDescriptor)" : desc.getName());
106
107     vw = errEvent.getViewBean();
108     handlerCtx.setOutputValue(EXCEPTION_VIEW_BEAN, vw);
109
110     handlerCtx.setOutputValue(EXCEPTION_VIEW_BEAN_NAME,
111         (vw == null) ? "(Null ViewBean)" : vw.getName());
112
113     handlerCtx.setOutputValue(EXCEPTION_STACK_TRACE,
114         errEvent.getRegularTrace());
115
116     handlerCtx.setOutputValue(EXCEPTION_CAUSE_VIEW,
117         errEvent.getCauseView());
118
119     handlerCtx.setOutputValue(EXCEPTION_CAUSE_VIEW_DESCRIPTOR,
120         errEvent.getCauseViewDescriptor());
121
122     handlerCtx.setOutputValue(EXCEPTION_CAUSE_MESSAGE,
123         errEvent.getCauseMessage());
124
125     handlerCtx.setOutputValue(EXCEPTION_CAUSE_CLASS_NAME,
126         errEvent.getCauseClassName());
127
128     handlerCtx.setOutputValue(EXCEPTION_CAUSE_FULL_TRACE,
129         errEvent.getFullTrace());
130
131     // Get the Request Parameters
132
java.util.Enumeration JavaDoc names = request.getParameterNames();
133     String JavaDoc name = null;
134     String JavaDoc nvps = "";
135     while (names.hasMoreElements()) {
136         name = names.nextElement().toString();
137         nvps += "\t'"+name+"'='"+request.getParameter(name)+"'\n";
138     }
139     handlerCtx.setOutputValue(REQUEST_PARAMETERS, nvps);
140     }
141
142     public void printTag(RequestContext reqCtx, HandlerContext handlerCtx) {
143     System.out.println("#######");
144     System.out.println("The tag object = "+((JspDisplayEvent)handlerCtx.getEvent()).getSourceTag());
145     System.out.println("#######");
146     }
147
148     /**
149      * This handler is intended for endDisplay events only, and only after
150      * the ErrorHandler.handleError event has been invoked. This handler
151      * will add information about the error to the HTML as an HTML comment
152      * so that this info is available via the HTML source.
153      */

154     public String JavaDoc addErrorInfo(RequestContext reqCtx, HandlerContext handlerCtx) {
155     if (!(handlerCtx.getEvent() instanceof ChildContentDisplayEvent)) {
156         return null;
157     }
158     ChildContentDisplayEvent dispEvent = (ChildContentDisplayEvent)handlerCtx.getEvent();
159     ServletRequest JavaDoc request =
160         RequestManager.getRequestContext().getRequest();
161
162     // Get the original String
163
StringBuffer JavaDoc buf = new StringBuffer JavaDoc(dispEvent.getContent());
164     buf.append("\n\n<!--\n\n");
165     buf.append(" Exception Info:\n");
166     buf.append("===================\n\n");
167     buf.append(" Exception Type: "+
168         request.getAttribute(EXCEPTION_CLASS_NAME)+"\n");
169     buf.append(" Exception Message: "+
170         request.getAttribute(EXCEPTION_MESSAGE)+"\n");
171     buf.append(" Root Cause: "+
172         request.getAttribute(EXCEPTION_CAUSE_CLASS_NAME)+"\n");
173     buf.append(" Root Message: "+
174         request.getAttribute(EXCEPTION_CAUSE_MESSAGE)+"\n\n");
175     buf.append("===================\n\n");
176
177     buf.append(" View Info:\n");
178     buf.append("===================\n\n");
179     buf.append(" ViewBean: "+
180         request.getAttribute(EXCEPTION_VIEW_BEAN_NAME)+"\n");
181     buf.append(" View: "+
182         request.getAttribute(EXCEPTION_VIEW_NAME)+"\n");
183     buf.append(" ViewDescriptor: "+
184         request.getAttribute(EXCEPTION_VIEW_DESCRIPTOR_NAME)+"\n");
185     buf.append("Request Parameters:\n"+
186         request.getAttribute(REQUEST_PARAMETERS)+"\n\n");
187     buf.append("==================\n\n");
188
189     buf.append("Below are 2 stack traces to help diagnose the problem.\n");
190     buf.append("The first is the traditional stack trace, the second is\n");
191     buf.append("a full stack trace to the root cause.\n\n");
192     buf.append(" Stack Trace:\n");
193     buf.append("===================\n\n");
194     buf.append(""+request.getAttribute(EXCEPTION_STACK_TRACE)+"\n\n");
195     buf.append(" Full Trace:\n");
196     buf.append("===================\n\n");
197     buf.append(""+request.getAttribute(EXCEPTION_CAUSE_FULL_TRACE)+"\n\n");
198     buf.append("===================\n\n");
199     buf.append("-->\n\n");
200
201     return buf.toString();
202     }
203
204
205
206     //////////////////////////////////////////////////////////////////
207
// Class Variables
208
//////////////////////////////////////////////////////////////////
209

210
211     /**
212      * Request attribute key for the exception.
213      */

214     public static final String JavaDoc EXCEPTION =
215     "EXCEPTION";
216
217     /**
218      * Request Attribute key for the top exception message.
219      */

220     public static final String JavaDoc EXCEPTION_MESSAGE =
221     "EXCEPTION_MESSAGE";
222
223     /**
224      * Request attribute key for the top exception class name.
225      */

226     public static final String JavaDoc EXCEPTION_CLASS_NAME =
227     "EXCEPTION_CLASS_NAME";
228
229     /**
230      * Request attribute key for the View which corresponds to the
231      * ViewDescriptor that is handling the Exception / Error.
232      */

233     public static final String JavaDoc EXCEPTION_VIEW =
234     "EXCEPTION_VIEW";
235
236     /**
237      * Request attribute key for the View name which corresponds to the
238      * ViewDescriptor that is handling the Exception / Error.
239      */

240     public static final String JavaDoc EXCEPTION_VIEW_NAME =
241     "EXCEPTION_VIEW_NAME";
242
243     /**
244      * Request attribute key for the ViewDescriptor which is handling the
245      * Exception / Error.
246      */

247     public static final String JavaDoc EXCEPTION_VIEW_DESCRIPTOR =
248     "EXCEPTION_VIEW_DESCRIPTOR";
249
250     /**
251      * Request attribute key for the ViewDescriptor name which is handling the
252      * Exception / Error.
253      */

254     public static final String JavaDoc EXCEPTION_VIEW_DESCRIPTOR_NAME =
255     "EXCEPTION_VIEW_DESCRIPTOR_NAME";
256
257     /**
258      * Request attribute key for the responsible ViewBean (may be same as
259      * View).
260      */

261     public static final String JavaDoc EXCEPTION_VIEW_BEAN =
262     "EXCEPTION_VIEW_BEAN";
263
264     /**
265      * Request attribute key for the responsible ViewBean name (may be same as
266      * View).
267      */

268     public static final String JavaDoc EXCEPTION_VIEW_BEAN_NAME =
269     "EXCEPTION_VIEW_BEAN_NAME";
270
271     /**
272      * Request attribute key for the regular stack trace
273      */

274     public static final String JavaDoc EXCEPTION_STACK_TRACE =
275     "EXCEPTION_STACK_TRACE";
276
277     /**
278      * Request attribute key for the responsible View object.
279      */

280     public static final String JavaDoc EXCEPTION_CAUSE_VIEW =
281     "EXCEPTION_CAUSE_VIEW";
282
283     /**
284      * Request attribute key for the responsible ViewDescriptor.
285      */

286     public static final String JavaDoc EXCEPTION_CAUSE_VIEW_DESCRIPTOR =
287     "EXCEPTION_CAUSE_VIEW_DESCRIPTOR";
288
289     /**
290      * Request attribute key for the cause exception message (null if no
291      * chained exceptions).
292      */

293     public static final String JavaDoc EXCEPTION_CAUSE_MESSAGE =
294     "EXCEPTION_CAUSE_MESSAGE";
295
296     /**
297      * Request attribute key for the cause exception class name (null if no
298      * chained exceptions).
299      */

300     public static final String JavaDoc EXCEPTION_CAUSE_CLASS_NAME =
301     "EXCEPTION_CAUSE_CLASS_NAME";
302
303     /**
304      * Request attribute key for the full stack trace to the cause
305      */

306     public static final String JavaDoc EXCEPTION_CAUSE_FULL_TRACE =
307     "EXCEPTION_CAUSE_FULL_TRACE";
308
309     /**
310      * Request attribute key for the request parameters sent from the browser.
311      */

312     public static final String JavaDoc REQUEST_PARAMETERS = "REQUEST_PARAMETERS";
313 }
314
Popular Tags