1 package org.apache.slide.projector.processor.core; 2 3 import java.util.Locale ; 4 import java.util.Map ; 5 import java.util.logging.Logger ; 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 logger = Logger.getLogger(ExceptionRenderer.class.getName()); 27 28 final static String OK = "ok"; 29 30 public final static String EXCEPTION = "exception"; 31 public final static String CLASS = "class"; 32 public final static String TITLE = "title"; 33 public final static String STACK_TRACE = "stackTrace"; 34 public final static String TEXT = "text"; 35 public final static String SUMMARY = "summary"; 36 public final static String DETAILS = "details"; 37 public final static String LOCALE = "locale"; 38 39 public Result process(Map parameter, Context context) throws Exception { 40 Locale locale = (((LocaleValue)parameter.get(LOCALE)).getLocale()); 41 ObjectValue exception = (ObjectValue)parameter.get(EXCEPTION); 42 Throwable throwable = (Throwable )exception.getObject(); 43 Result result = new Result(OK); 44 String title = null, text = null, summary = null; 45 StringBuffer details = new StringBuffer (); 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 []trace = throwable.getStackTrace(); 72 StringBuffer buffer = new StringBuffer (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 details, Throwable throwable, Locale 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 |