KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > core > ExceptionRenderer


1 package org.apache.slide.projector.processor.core;
2
3 import java.util.Locale JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.logging.Logger JavaDoc;
6
7 import org.apache.slide.projector.Context;
8 import org.apache.slide.projector.ProcessException;
9 import org.apache.slide.projector.Processor;
10 import org.apache.slide.projector.Result;
11 import org.apache.slide.projector.descriptor.AnyValueDescriptor;
12 import org.apache.slide.projector.descriptor.LocaleValueDescriptor;
13 import org.apache.slide.projector.descriptor.ParameterDescriptor;
14 import org.apache.slide.projector.descriptor.ResultDescriptor;
15 import org.apache.slide.projector.descriptor.ResultEntryDescriptor;
16 import org.apache.slide.projector.descriptor.StateDescriptor;
17 import org.apache.slide.projector.i18n.DefaultMessage;
18 import org.apache.slide.projector.i18n.ErrorMessage;
19 import org.apache.slide.projector.i18n.MessageNotFoundException;
20 import org.apache.slide.projector.i18n.ParameterMessage;
21 import org.apache.slide.projector.value.LocaleValue;
22 import org.apache.slide.projector.value.ObjectValue;
23 import org.apache.slide.projector.value.StringValue;
24
25 public class ExceptionRenderer implements Processor {
26     private static Logger JavaDoc logger = Logger.getLogger(ExceptionRenderer.class.getName());
27
28     final static String JavaDoc OK = "ok";
29
30     public final static String JavaDoc EXCEPTION = "exception";
31     public final static String JavaDoc CLASS = "class";
32     public final static String JavaDoc TITLE = "title";
33     public final static String JavaDoc STACK_TRACE = "stackTrace";
34     public final static String JavaDoc TEXT = "text";
35     public final static String JavaDoc SUMMARY = "summary";
36     public final static String JavaDoc DETAILS = "details";
37     public final static String JavaDoc LOCALE = "locale";
38
39     public Result process(Map JavaDoc parameter, Context context) throws Exception JavaDoc {
40         Locale JavaDoc locale = (((LocaleValue)parameter.get(LOCALE)).getLocale());
41         ObjectValue exception = (ObjectValue)parameter.get(EXCEPTION);
42         Throwable JavaDoc throwable = (Throwable JavaDoc)exception.getObject();
43         Result result = new Result(OK);
44         String JavaDoc title = null, text = null, summary = null;
45         StringBuffer JavaDoc details = new StringBuffer JavaDoc();
46         if ( throwable instanceof ProcessException ) {
47             try {
48                 title = ((ProcessException)throwable).getErrorMessage().getTitle(locale);
49                 text = ((ProcessException)throwable).getErrorMessage().getText(locale);
50                 summary = ((ProcessException)throwable).getErrorMessage().getSummary(locale);
51             } catch ( MessageNotFoundException e ) {
52                 title = ((ProcessException)throwable).getErrorMessage().getId();
53                 text = ((ProcessException)throwable).getErrorMessage().getId();
54                 summary = ((ProcessException)throwable).getErrorMessage().getId();
55             }
56             appendNestedDetails(details, throwable, locale);
57         } else {
58             title = throwable.getLocalizedMessage();
59             text = throwable.getLocalizedMessage();
60             summary = throwable.getLocalizedMessage();
61             appendNestedDetails(details, throwable, locale);
62         }
63         if ( title == null ) title = "";
64         if ( text == null ) text = "";
65         if ( summary == null ) summary = "";
66         result.addResultEntry(TITLE, new StringValue(title));
67         result.addResultEntry(TEXT, new StringValue(text));
68         result.addResultEntry(SUMMARY, new StringValue(summary));
69         result.addResultEntry(DETAILS, new StringValue(details.toString()));
70         result.addResultEntry(CLASS, new StringValue(throwable.getClass().getName()));
71         StackTraceElement JavaDoc []trace = throwable.getStackTrace();
72         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(256);
73         for ( int i = 0; i < trace.length; i++ ) {
74             buffer.append(trace[i].toString());
75             buffer.append(" ");
76         }
77         result.addResultEntry(STACK_TRACE, new StringValue(buffer.toString()));
78         return result;
79     }
80
81     private void appendNestedDetails(StringBuffer JavaDoc details, Throwable JavaDoc throwable, Locale JavaDoc locale) {
82         if ( details.length() > 0 ) details.append(' ');
83         if ( throwable instanceof ProcessException ) {
84             ErrorMessage message = ((ProcessException)throwable).getErrorMessage();
85             details.append(message.getDetails(locale, ""));
86         } else {
87             details.append(throwable.getMessage());
88         }
89         if ( throwable.getCause() != null ) appendNestedDetails(details, throwable.getCause(), locale);
90     }
91
92     public ParameterDescriptor[] getParameterDescriptors() {
93         return new ParameterDescriptor[]{
94             new ParameterDescriptor(EXCEPTION, new ParameterMessage("exceptionRenderer/exception"), new AnyValueDescriptor()),
95             new ParameterDescriptor(LOCALE, new ParameterMessage("exceptionRenderer/locale"), new LocaleValueDescriptor(), new LocaleValue(Locale.getDefault()))
96         };
97     }
98
99
100     public ResultDescriptor getResultDescriptor() {
101         return new ResultDescriptor(new StateDescriptor[]{ StateDescriptor.OK_DESCRIPTOR },
102                 new ResultEntryDescriptor[]{
103                     new ResultEntryDescriptor(TITLE, new DefaultMessage("exceptionRenderer/result/title"), "text/plain", true),
104                     new ResultEntryDescriptor(TEXT, new DefaultMessage("exceptionRenderer/result/text"), "text/plain", true),
105                     new ResultEntryDescriptor(SUMMARY, new DefaultMessage("exceptionRenderer/result/summary"), "text/plain", true),
106                     new ResultEntryDescriptor(DETAILS, new DefaultMessage("exceptionRenderer/result/details"), "text/plain", true),
107                     new ResultEntryDescriptor(CLASS, new DefaultMessage("exceptionRenderer/result/class"), "text/plain", true),
108                     new ResultEntryDescriptor(STACK_TRACE, new DefaultMessage("exceptionRenderer/result/stackTrace"), "text/plain", true)
109                 });
110     }
111 }
112
Popular Tags