1 16 package org.apache.cocoon.components.notification; 17 18 import org.apache.avalon.framework.component.Component; 19 20 import org.apache.commons.lang.SystemUtils; 21 import org.apache.commons.lang.exception.ExceptionUtils; 22 import org.xml.sax.SAXParseException ; 23 24 import javax.xml.transform.SourceLocator ; 25 import javax.xml.transform.TransformerException ; 26 import java.io.PrintWriter ; 27 import java.io.StringWriter ; 28 import java.io.Writer ; 29 import java.util.Map ; 30 31 38 public class DefaultNotifyingBuilder implements NotifyingBuilder, Component { 39 40 49 public Notifying build (Object sender, Object o) { 50 if (o instanceof Notifying) { 51 return (Notifying) o; 52 } else if (o instanceof Throwable ) { 53 Throwable t = (Throwable ) o; 54 SimpleNotifyingBean n = new SimpleNotifyingBean(sender); 55 n.setType(Notifying.ERROR_NOTIFICATION); 56 n.setTitle("An Error Occurred"); 57 58 if (t != null) { 59 Throwable rootCause = getRootCause(t); 60 61 n.setSource(t.getClass().getName()); 62 63 if (rootCause.getMessage() != null) { 65 n.setMessage(rootCause.getMessage()); 66 } else { 67 n.setMessage(t.getMessage()); 68 } 69 70 n.setDescription(t.toString()); 71 n.addExtraDescription(Notifying.EXTRA_CAUSE, rootCause.toString()); 72 73 if (rootCause instanceof SAXParseException ) { 74 SAXParseException saxParseException = (SAXParseException ) rootCause; 75 76 n.addExtraDescription(Notifying.EXTRA_LOCATION, 77 String.valueOf(saxParseException.getSystemId())); 78 n.addExtraDescription(Notifying.EXTRA_LINE, 79 String.valueOf(saxParseException.getLineNumber())); 80 n.addExtraDescription(Notifying.EXTRA_COLUMN, 81 String.valueOf(saxParseException.getColumnNumber())); 82 } else if (rootCause instanceof TransformerException ) { 83 TransformerException transformerException = (TransformerException ) rootCause; 84 SourceLocator sourceLocator = transformerException.getLocator(); 85 86 if (null != sourceLocator) { 87 n.addExtraDescription(Notifying.EXTRA_LOCATION, 88 String.valueOf(sourceLocator.getSystemId())); 89 n.addExtraDescription(Notifying.EXTRA_LINE, 90 String.valueOf(sourceLocator.getLineNumber())); 91 n.addExtraDescription(Notifying.EXTRA_COLUMN, 92 String.valueOf(sourceLocator.getColumnNumber())); 93 } 94 } 95 96 StringWriter sw = new StringWriter (); 98 rootCause.printStackTrace(new PrintWriter (sw)); 99 n.addExtraDescription(Notifying.EXTRA_STACKTRACE, sw.toString()); 100 101 sw = new StringWriter (); 103 appendTraceChain(sw, t); 104 n.addExtraDescription(Notifying.EXTRA_FULLTRACE, sw.toString()); 105 } 106 107 return n; 108 } else { 109 SimpleNotifyingBean n = new SimpleNotifyingBean(sender); 110 n.setType(Notifying.UNKNOWN_NOTIFICATION); 111 n.setTitle("Object Notification"); 112 n.setMessage(String.valueOf(o)); 113 n.setDescription("No details available."); 114 return n; 115 } 116 } 117 118 133 public Notifying build(Object sender, Object o, String type, String title, 134 String source, String message, String description, Map extra) { 135 SimpleNotifyingBean n = (SimpleNotifyingBean) build (sender, o); 137 138 if (type != null) 139 n.setType(type); 140 if (title != null) 141 n.setTitle(title); 142 if (source != null) 143 n.setSource(source); 144 if (message != null) 145 n.setMessage(message); 146 if (description != null) 147 n.setDescription(description); 148 if (extra != null) 149 n.addExtraDescriptions(extra); 150 151 return n; 152 } 153 154 155 158 private static void appendTraceChain(Writer out, Throwable t) { 159 PrintWriter pw = new PrintWriter (out); 160 if (SystemUtils.isJavaVersionAtLeast(140)) { 161 t.printStackTrace(pw); 162 } else { 163 for (Throwable cause = t; cause != null; cause = ExceptionUtils.getCause(cause)) { 164 if (cause != t) { 165 pw.println(); 166 } 167 cause.printStackTrace(pw); 168 } 169 } 170 } 171 172 175 public static Throwable getRootCause (Throwable t) { 176 Throwable rootCause = ExceptionUtils.getRootCause(t); 177 return rootCause != null ? rootCause : t; 178 } 179 } 180 | Popular Tags |