| 1 package org.objectweb.celtix.bus.jaxws.io; 2 3 import java.lang.reflect.Method ; 4 import java.lang.reflect.ParameterizedType ; 5 import java.lang.reflect.Type ; 6 import java.util.concurrent.Future ; 7 8 import javax.jws.WebParam; 9 import javax.xml.namespace.QName ; 10 import javax.xml.ws.Holder; 11 import javax.xml.ws.WebServiceException; 12 13 import org.w3c.dom.Node ; 14 15 import org.objectweb.celtix.bindings.DataReader; 16 import org.objectweb.celtix.bus.jaxws.ClassHelper; 17 import org.objectweb.celtix.bus.jaxws.JAXBDataBindingCallback; 18 import org.objectweb.celtix.bus.jaxws.JAXBEncoderDecoder; 19 import org.objectweb.celtix.context.ObjectMessageContext; 20 import org.objectweb.celtix.helpers.NodeUtils; 21 import org.objectweb.celtix.jaxb.JAXBUtils; 22 23 public class NodeDataReader<T> implements DataReader<T> { 24 final JAXBDataBindingCallback callback; 25 26 public NodeDataReader(JAXBDataBindingCallback cb) { 27 callback = cb; 28 } 29 30 public Object read(int idx, T input) { 31 return read(null, idx, input); 32 } 33 34 public Object read(QName name, int idx, T input) { 35 Class <?> cls; 36 if (idx == -1) { 37 cls = callback.getMethod().getReturnType(); 38 } else { 39 cls = callback.getMethod().getParameterTypes()[idx]; 40 if (cls.isAssignableFrom(Holder.class)) { 41 Type [] genericParameterTypes = callback.getMethod().getGenericParameterTypes(); 43 ParameterizedType paramType = (ParameterizedType )genericParameterTypes[idx]; 45 cls = JAXBEncoderDecoder.getClassFromType( 46 paramType.getActualTypeArguments()[0]); 47 } 48 } 49 Node xmlNode = (Node )input; 50 51 return JAXBEncoderDecoder.unmarshall(callback.getJAXBContext(), 52 callback.getSchema(), xmlNode, name, cls); 53 } 54 55 public void readWrapper(ObjectMessageContext objCtx, boolean isOutBound, T input) { 56 Node xmlNode = (Node )input; 57 String wrapperType = isOutBound ? callback.getResponseWrapperType() 58 : callback.getRequestWrapperType(); 59 60 Node childNode = NodeUtils.getChildElementNode(xmlNode); 61 Object [] methodArgs = objCtx.getMessageObjects(); 62 63 QName elName = isOutBound ? callback.getResponseWrapperQName() 64 : callback.getRequestWrapperQName(); 65 66 Object obj = null; 67 68 try { 69 obj = JAXBEncoderDecoder.unmarshall(callback.getJAXBContext(), 70 callback.getSchema(), childNode, 71 elName, ClassHelper.forName(wrapperType)); 72 } catch (ClassNotFoundException e) { 73 throw new WebServiceException("Could not unmarshall wrapped type (" + wrapperType + ") ", e); 74 } 75 76 if (isOutBound && callback.getWebResult() != null) { 77 Method method = callback.getMethod(); 78 if (JAXBUtils.isAsync(method)) { 79 Method syncMethod = callback.getSyncMethod(); 80 Type gtype = method.getGenericReturnType(); 81 82 if (Future .class.equals(method.getReturnType())) { 83 Type types[] = method.getGenericParameterTypes(); 84 if (types.length > 0 && types[types.length - 1] instanceof ParameterizedType ) { 85 gtype = types[types.length - 1]; 86 } 87 } 88 if (gtype instanceof ParameterizedType 89 && ((ParameterizedType )gtype).getActualTypeArguments().length == 1 90 && ((ParameterizedType )gtype).getActualTypeArguments()[0] instanceof Class ) { 91 Class cls = (Class )((ParameterizedType )gtype).getActualTypeArguments()[0]; 92 if (cls.getName().equals(wrapperType)) { 93 syncMethod = null; 94 } 95 } 96 method = syncMethod; 97 } 98 if (method != null) { 99 Object retVal = callback.getWrappedPart(callback.getWebResultQName().getLocalPart(), 100 obj, 101 method.getReturnType()); 102 objCtx.setReturn(retVal); 103 } else { 104 objCtx.setReturn(obj); 105 } 106 } 107 108 WebParam.Mode ignoreParamMode = isOutBound ? WebParam.Mode.IN : WebParam.Mode.OUT; 109 int noArgs = callback.getMethod().getParameterTypes().length; 110 try { 111 for (int idx = 0; idx < noArgs; idx++) { 112 WebParam param = callback.getWebParam(idx); 113 if ((param.mode() != ignoreParamMode) && !param.header()) { 114 Class <?> cls = callback.getMethod().getParameterTypes()[idx]; 115 if (param.mode() != WebParam.Mode.IN) { 116 Type [] genericParameterTypes = callback.getMethod().getGenericParameterTypes(); 118 ParameterizedType paramType = (ParameterizedType )genericParameterTypes[idx]; 120 Class <?> c = 121 JAXBEncoderDecoder.getClassFromType(paramType.getActualTypeArguments()[0]); 122 Object partValue = callback.getWrappedPart(param.name(), obj, c); 123 cls.getField("value").set(methodArgs[idx], partValue); 126 } else { 127 methodArgs[idx] = callback.getWrappedPart(param.name(), obj, cls); 128 } 129 } 130 } 131 } catch (IllegalAccessException iae) { 132 throw new WebServiceException("Could not unwrap the parts.", iae); 133 } catch (NoSuchFieldException nsfe) { 134 throw new WebServiceException("Could not unwrap the parts.", nsfe); 135 } 136 } 137 } 138 | Popular Tags |