1 package org.objectweb.celtix.tools.processors.java2.internal; 2 3 import java.lang.annotation.Annotation ; 4 import java.lang.reflect.GenericArrayType ; 5 import java.lang.reflect.Method ; 6 import java.lang.reflect.ParameterizedType ; 7 import java.lang.reflect.Type ; 8 import java.rmi.RemoteException ; 9 import java.util.ArrayList ; 10 import java.util.List ; 11 import java.util.logging.Logger ; 12 13 import javax.jws.Oneway; 14 import javax.jws.WebParam; 15 import javax.jws.WebResult; 16 import javax.jws.soap.SOAPBinding; 17 import javax.xml.namespace.QName ; 18 import javax.xml.ws.Holder; 19 import javax.xml.ws.RequestWrapper; 20 import javax.xml.ws.ResponseWrapper; 21 import javax.xml.ws.WebFault; 22 23 import com.sun.xml.bind.api.TypeReference; 24 25 import org.objectweb.celtix.common.i18n.Message; 26 import org.objectweb.celtix.common.logging.LogUtils; 27 import org.objectweb.celtix.tools.common.ToolException; 28 import org.objectweb.celtix.tools.common.model.JavaMethod; 29 import org.objectweb.celtix.tools.common.model.JavaParameter; 30 import org.objectweb.celtix.tools.common.model.JavaType; 31 import org.objectweb.celtix.tools.common.model.WSDLException; 32 import org.objectweb.celtix.tools.common.model.WSDLModel; 33 import org.objectweb.celtix.tools.common.model.WSDLParameter; 34 import org.objectweb.celtix.tools.processors.java2.JavaToWSDLProcessor; 35 import org.objectweb.celtix.tools.utils.AnnotationUtil; 36 37 public class DocWrapperMethodProcessor { 38 private static final Logger LOG = LogUtils.getL7dLogger(JavaToWSDLProcessor.class); 39 private WSDLModel model; 40 41 public DocWrapperMethodProcessor(WSDLModel wmodel) { 42 model = wmodel; 43 } 44 45 public void process(JavaMethod javaMethod, Method method) { 46 javaMethod.setSoapStyle(SOAPBinding.Style.DOCUMENT); 47 javaMethod.setWrapperStyle(true); 48 setMethodUse(javaMethod, method); 49 50 RequestWrapper reqWrapper = method.getAnnotation(RequestWrapper.class); 52 String reqClassName = ""; 53 String reqName = method.getName(); 54 String reqNS = model.getTargetNameSpace(); 55 if (reqWrapper != null) { 56 reqClassName = reqWrapper.className().length() > 0 ? reqWrapper.className() : reqClassName; 57 reqName = reqWrapper.localName().length() > 0 ? reqWrapper.localName() : reqName; 58 reqNS = reqWrapper.targetNamespace().length() > 0 ? reqWrapper.targetNamespace() : reqNS; 59 } else { 60 reqClassName = model.getPackageName() + ".types." + AnnotationUtil.capitalize(method.getName()); 61 } 62 63 Class reqClass = null; 64 try { 65 reqClass = AnnotationUtil.loadClass(reqClassName, method.getDeclaringClass().getClassLoader()); 66 } catch (Exception e) { 67 68 Message msg = new Message("LOAD_CLASS_ERROR", LOG, reqClassName); 69 throw new ToolException(msg, e); 70 } 71 QName reqQN = new QName (reqNS, reqName); 72 TypeReference typeRef = new TypeReference(reqQN, reqClass, new Annotation [0]); 73 WSDLParameter request = new WSDLParameter(reqName, typeRef, JavaType.Style.IN); 74 request.setTargetNamespace(reqNS); 75 javaMethod.addRequest(request); 76 77 WSDLParameter response = null; 78 if (!isOneWayMethod(method)) { 79 ResponseWrapper resWrapper = method.getAnnotation(ResponseWrapper.class); 81 String resClassName = ""; 82 String resName = method.getName() + "Response"; 84 String resNS = model.getTargetNameSpace(); 85 if (resWrapper != null) { 86 resClassName = resWrapper.className(); 87 resName = resWrapper.localName().length() > 0 ? resWrapper.localName() : resName; 88 resNS = resWrapper.targetNamespace().length() > 0 ? resWrapper.targetNamespace() : resNS; 89 } else { 90 resClassName = model.getPackageName() + ".types." 91 + AnnotationUtil.capitalize(method.getName()) 92 + "Response"; 93 } 94 Class resClass = null; 95 QName resQN = new QName (resNS, resName); 96 try { 97 resClass = AnnotationUtil 98 .loadClass(resClassName, method.getDeclaringClass().getClassLoader()); 99 } catch (Exception e) { 100 Message msg = new Message("LOAD_CLASS_ERROR", LOG, resClassName); 101 throw new ToolException(msg, e); 102 } 103 typeRef = new TypeReference(resQN, resClass, new Annotation [0]); 104 response = new WSDLParameter(resName, typeRef, JavaType.Style.OUT); 105 response.setTargetNamespace(resNS); 106 javaMethod.addResponse(response); 107 WebResult webResult = method.getAnnotation(WebResult.class); 108 JavaParameter returnParameter = getReturnParameter(webResult, method); 109 if (returnParameter != null) { 110 response.addChildren(returnParameter); 111 } 112 } 113 List <JavaParameter> paras = processWebPara(method); 114 for (JavaParameter jp : paras) { 115 if (jp.getStyle() == JavaType.Style.IN) { 116 request.addChildren(jp); 117 } else { 118 request.addChildren(jp); 119 } 120 } 121 122 processExceptions(javaMethod, method); 123 } 124 125 private void setMethodUse(JavaMethod javaMethod, Method method) { 126 SOAPBinding binding = method.getAnnotation(SOAPBinding.class); 127 if (binding != null) { 128 javaMethod.setSoapUse(binding.use()); 129 } else { 130 javaMethod.setSoapUse(this.model.getUse()); 131 } 132 } 133 134 private List <JavaParameter> processWebPara(Method method) { 135 Class <?>[] parameterTypes = method.getParameterTypes(); 137 Type [] parameterGenTypes = method.getGenericParameterTypes(); 138 Annotation [][] paraAnns = AnnotationUtil.getPrivParameterAnnotations(method); 139 List <JavaParameter> paras = new ArrayList <JavaParameter>(); 140 int i = 0; 141 for (Class clazzType : parameterTypes) { 142 String paraName = "arg" + i; 143 String partName; 144 String paraTNS = model.getTargetNameSpace(); 145 Class clazz = clazzType; 146 boolean holder = isHolder(clazzType); 147 if (holder) { 148 clazz = getHoldedClass(clazzType, parameterGenTypes[i]); 149 } 150 for (Annotation anno : paraAnns[i]) { 151 if (anno.annotationType() == WebParam.class) { 152 WebParam webParam = (WebParam)anno; 153 paraName = webParam.name().length() > 0 ? webParam.name() : paraName; 154 partName = webParam.partName().length() > 0 ? webParam.partName() : paraName; 155 paraTNS = webParam.targetNamespace().length() > 0 156 ? webParam.targetNamespace() : paraTNS; 157 158 QName requestQN = new QName (paraTNS, paraName); 159 TypeReference typeref = new TypeReference(requestQN, clazz, paraAnns[i]); 160 JavaParameter jp; 161 if (holder) { 162 if (webParam.mode() == WebParam.Mode.INOUT) { 163 jp = new JavaParameter(typeref.tagName.getLocalPart(), typeref, 164 JavaType.Style.INOUT); 165 } else { 166 jp = new JavaParameter(typeref.tagName.getLocalPart(), typeref, 167 JavaType.Style.OUT); 168 } 169 } else { 170 jp = new JavaParameter(typeref.tagName.getLocalPart(), typeref, JavaType.Style.IN); 171 } 172 jp.setName(paraName); 173 jp.setPartName(partName); 174 jp.setHeader(webParam.header()); 175 jp.setTargetNamespace(paraTNS); 176 paras.add(jp); 177 } 178 } 179 i++; 180 } 181 182 return paras; 183 } 184 185 private void processExceptions(JavaMethod jmethod, Method method) { 186 for (Type exception : method.getGenericExceptionTypes()) { 187 if (RemoteException .class.isAssignableFrom((Class )exception)) { 188 continue; 189 } 190 Annotation [] anns = null; 191 Class <?> exClass = (Class <?>)exception; 192 String exNameSpace = model.getTargetNameSpace(); 193 String exName = exClass.getSimpleName(); 194 Class exReturnType = null; 195 Method faultInfo = null; 196 try { 197 faultInfo = exClass.getMethod("getFaultInfo", new Class [0]); 198 } catch (SecurityException e) { 199 throw new ToolException(e.getMessage(), e); 200 } catch (NoSuchMethodException e) { 201 throw new ToolException(e.getMessage(), e); 202 } 203 204 if (faultInfo != null) { 205 WebFault wf = exClass.getAnnotation(WebFault.class); 206 exReturnType = faultInfo.getReturnType(); 207 anns = faultInfo.getAnnotations(); 208 if (wf.targetNamespace().length() > 0) { 209 exNameSpace = wf.targetNamespace(); 210 } 211 exName = wf.name(); 212 } 213 214 QName exQName = new QName (exNameSpace, exName); 215 TypeReference tf = new TypeReference(exQName, exReturnType, anns); 216 WSDLException wsdlEx = new WSDLException(exClass, tf); 217 218 jmethod.addWSDLException(wsdlEx); 219 220 } 221 } 222 223 private boolean isHolder(Class cType) { 224 return Holder.class.isAssignableFrom(cType); 225 } 227 228 private Class getHoldedClass(Class holderClazz, Type type) { 229 ParameterizedType pt = (ParameterizedType )type; 230 return getClass(pt.getActualTypeArguments()[0]); 231 } 232 233 private Class getClass(Type type) { 234 if (type instanceof Class ) { 235 return (Class )type; 236 } else if (type instanceof GenericArrayType ) { 237 GenericArrayType gt = (GenericArrayType )type; 238 Class compType = getClass(gt.getGenericComponentType()); 239 return java.lang.reflect.Array.newInstance(compType, 0).getClass(); 240 } 241 return Object .class; 242 } 243 244 private boolean isOneWayMethod(Method method) { 245 return method.isAnnotationPresent(Oneway.class); 246 } 247 248 private JavaParameter getReturnParameter(WebResult webResult, Method method) { 249 boolean isHeader = false; 250 String resultName = "Return"; 251 String resultTNS = model.getTargetNameSpace(); 252 JavaParameter jpara = null; 253 QName resultQName = null; 254 if (webResult != null) { 255 resultName = webResult.name().length() > 0 && webResult.partName().length() > 0 ? webResult 256 .partName() : resultName; 257 resultName = webResult.name().length() > 0 ? webResult.name() : resultName; 258 resultName = webResult.partName().length() > 0 ? webResult.partName() : resultName; 259 resultTNS = webResult.targetNamespace().length() > 0 ? webResult.targetNamespace() : resultTNS; 260 isHeader = webResult.header(); 261 } 262 resultQName = new QName (resultTNS, resultName); 263 Class returnType = method.getReturnType(); 264 if (resultQName != null && !isOneWayMethod(method) && (returnType != null) 265 && (!"void".equals(returnType.getName()))) { 266 Annotation [] annotations = new Annotation [0]; 268 if (resultQName.getLocalPart() != null) { 269 TypeReference rTypeReference = new TypeReference(resultQName, returnType, annotations); 270 jpara = new JavaParameter(); 271 jpara.setName(method.getName() + "Response"); 272 jpara.setTypeReference(rTypeReference); 273 jpara.setStyle(JavaType.Style.OUT); 274 jpara.setHeader(isHeader); 275 } 276 } 277 return jpara; 278 } 279 280 } 281 | Popular Tags |