public Notifying build(Object sender,
Object o) {
if (o instanceof Notifying) {
return (Notifying) o;
} else if (o instanceof Throwable) {
Throwable t = (Throwable) o;
SimpleNotifyingBean n = new SimpleNotifyingBean(sender);
n.setType(Notifying.ERROR_NOTIFICATION);
n.setTitle("An Error Occurred");
if (t != null) {
Throwable rootCause = getRootCause(t);
n.setSource(t.getClass().getName());
// NullPointerException usually does not have a message
if (rootCause.getMessage() != null) {
n.setMessage(rootCause.getMessage());
} else {
n.setMessage(t.getMessage());
}
n.setDescription(t.toString());
n.addExtraDescription(Notifying.EXTRA_CAUSE, rootCause.toString());
if (rootCause instanceof SAXParseException) {
SAXParseException saxParseException = (SAXParseException) rootCause;
n.addExtraDescription(Notifying.EXTRA_LOCATION,
String.valueOf(saxParseException.getSystemId()));
n.addExtraDescription(Notifying.EXTRA_LINE,
String.valueOf(saxParseException.getLineNumber()));
n.addExtraDescription(Notifying.EXTRA_COLUMN,
String.valueOf(saxParseException.getColumnNumber()));
} else if (rootCause instanceof TransformerException) {
TransformerException transformerException = (TransformerException) rootCause;
SourceLocator sourceLocator = transformerException.getLocator();
if (null != sourceLocator) {
n.addExtraDescription(Notifying.EXTRA_LOCATION,
String.valueOf(sourceLocator.getSystemId()));
n.addExtraDescription(Notifying.EXTRA_LINE,
String.valueOf(sourceLocator.getLineNumber()));
n.addExtraDescription(Notifying.EXTRA_COLUMN,
String.valueOf(sourceLocator.getColumnNumber()));
}
}
// Add root cause exception stacktrace
StringWriter sw = new StringWriter();
rootCause.printStackTrace(new PrintWriter(sw));
n.addExtraDescription(Notifying.EXTRA_STACKTRACE, sw.toString());
// Add full exception chain
sw = new StringWriter();
appendTraceChain(sw, t);
n.addExtraDescription(Notifying.EXTRA_FULLTRACE, sw.toString());
}
return n;
} else {
SimpleNotifyingBean n = new SimpleNotifyingBean(sender);
n.setType(Notifying.UNKNOWN_NOTIFICATION);
n.setTitle("Object Notification");
n.setMessage(String.valueOf(o));
n.setDescription("No details available.");
return n;
}
}
Builds a Notifying object (SimpleNotifyingBean in this case)
that tries to explain what the Object o can reveal. |