01: package org.objectweb.celtix.bus.jaxws.io;
02:
03: import java.lang.reflect.Method;
04: import javax.xml.namespace.QName;
05: import javax.xml.ws.WebFault;
06: import javax.xml.ws.WebServiceException;
07: import org.w3c.dom.*;
08:
09: import org.objectweb.celtix.bindings.DataWriter;
10: import org.objectweb.celtix.bus.bindings.xml.XMLFault;
11: import org.objectweb.celtix.bus.jaxws.JAXBDataBindingCallback;
12: import org.objectweb.celtix.bus.jaxws.JAXBEncoderDecoder;
13: import org.objectweb.celtix.context.ObjectMessageContext;
14:
15: public class XMLFaultWriter<T> implements DataWriter<T> {
16: final JAXBDataBindingCallback callback;
17:
18: public XMLFaultWriter(JAXBDataBindingCallback cb) {
19: callback = cb;
20: }
21:
22: public void write(Object obj, T output) {
23: WebFault wfAnnotation = obj.getClass().getAnnotation(
24: WebFault.class);
25: if (wfAnnotation != null) {
26: QName elName = new QName(wfAnnotation.targetNamespace(),
27: wfAnnotation.name());
28: write(obj, elName, output);
29: }
30: }
31:
32: public void write(Object obj, QName elName, T output) {
33: Object faultInfo = getFaultInfo((Throwable) obj);
34: if (faultInfo != null) {
35: XMLFault fault = (XMLFault) output;
36: Node detail = fault.addFaultDetail();
37: JAXBEncoderDecoder.marshall(callback.getJAXBContext(),
38: callback.getSchema(), faultInfo, elName, detail);
39: fault.setFaultDetail(detail);
40: }
41: }
42:
43: public void writeWrapper(ObjectMessageContext objCtx,
44: boolean isOutbound, T output) {
45: throw new UnsupportedOperationException();
46: }
47:
48: private Object getFaultInfo(Throwable fault) {
49: try {
50: Method faultInfoMethod = fault.getClass().getMethod(
51: "getFaultInfo");
52: if (faultInfoMethod != null) {
53: return faultInfoMethod.invoke(fault);
54: }
55: } catch (Exception ex) {
56: throw new WebServiceException(
57: "Could not get faultInfo out of Exception", ex);
58: }
59:
60: return null;
61: }
62: }
|