1 package org.objectweb.celtix.tools.processors.wsdl2.internal; 2 3 import java.util.HashMap ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 import java.util.Map ; 7 import java.util.logging.Level ; 8 9 import javax.jws.soap.SOAPBinding; 10 import javax.wsdl.Fault; 11 import javax.wsdl.Message; 12 import javax.wsdl.Operation; 13 import javax.wsdl.OperationType; 14 import javax.wsdl.Part; 15 import javax.xml.namespace.QName ; 16 17 import org.objectweb.celtix.common.util.StringUtils; 18 import org.objectweb.celtix.tools.common.ProcessorEnvironment; 19 import org.objectweb.celtix.tools.common.ToolConstants; 20 import org.objectweb.celtix.tools.common.ToolException; 21 import org.objectweb.celtix.tools.common.model.JavaAnnotation; 22 import org.objectweb.celtix.tools.common.model.JavaInterface; 23 import org.objectweb.celtix.tools.common.model.JavaMethod; 24 import org.objectweb.celtix.tools.common.model.JavaParameter; 25 import org.objectweb.celtix.tools.common.model.JavaReturn; 26 import org.objectweb.celtix.tools.extensions.jaxws.CustomizationParser; 27 import org.objectweb.celtix.tools.extensions.jaxws.JAXWSBinding; 28 import org.objectweb.celtix.tools.utils.ProcessorUtil; 29 import org.objectweb.celtix.tools.utils.SOAPBindingUtil; 30 31 public class OperationProcessor extends AbstractProcessor { 32 33 private JavaParameter wrapperRequest; 34 private JavaParameter wrapperResponse; 35 36 public OperationProcessor(ProcessorEnvironment penv) { 37 super(penv); 38 } 39 40 @SuppressWarnings ("unchecked") 41 public void process(JavaInterface intf, Operation operation) throws ToolException { 42 JavaMethod method = new JavaMethod(intf); 43 method.setName(ProcessorUtil.mangleNameToVariableName(operation.getName())); 44 method.setOperationName(operation.getName()); 45 method.setStyle(operation.getStyle()); 46 if (method.getStyle() == null) { 47 if (operation.getOutput() == null) { 48 method.setStyle(OperationType.ONE_WAY); 49 } else { 50 method.setStyle(OperationType.REQUEST_RESPONSE); 51 } 52 } 53 54 method.setWrapperStyle(isWrapperStyle(operation)); 55 method.setJAXWSBinding(customizing(intf, operation)); 56 processMethod(method, operation); 57 Map <String , Fault> faults = operation.getFaults(); 58 FaultProcessor faultProcessor = new FaultProcessor(env); 59 faultProcessor.process(method, faults); 60 61 intf.addMethod(method); 62 } 63 64 @SuppressWarnings ("unchecked") 65 public void processMethod(JavaMethod method, Operation operation) throws ToolException { 66 List <String > parameterOrder = operation.getParameterOrdering(); 67 Message inputMessage = operation.getInput() == null ? null : operation.getInput().getMessage(); 68 Message outputMessage = operation.getOutput() == null ? null : operation.getOutput().getMessage(); 69 70 if (inputMessage == null) { 71 LOG.log(Level.WARNING, "NO_INPUT_MESSAGE", 72 new Object [] {operation.getName(), operation.getInput().getName()}); 73 } 74 75 ParameterProcessor paramProcessor = new ParameterProcessor(env); 76 method.clear(); 77 paramProcessor.process(method, 78 inputMessage, 79 outputMessage, 80 isRequestResponse(operation), 81 parameterOrder); 82 isWrapperStyle(operation); 83 addWebMethodAnnotation(method); 84 addWrapperAnnotation(method, operation); 85 addWebResultAnnotation(method); 86 addSOAPBindingAnnotation(method); 87 if (!method.isOneWay() && method.getJAXWSBinding().isEnableAsyncMapping()) { 88 addAsyncMethod(method); 89 } 90 } 91 92 private void addSOAPBindingAnnotation(JavaMethod method) { 93 if (method.getSoapStyle() == SOAPBinding.Style.DOCUMENT && !method.isWrapperStyle()) { 94 JavaAnnotation bindingAnnotation = new JavaAnnotation("SOAPBinding"); 95 bindingAnnotation.addArgument("parameterStyle", SOAPBindingUtil.getBindingAnnotation("BARE"), ""); 96 method.addAnnotation("SOAPBinding", bindingAnnotation); 97 } 98 } 99 100 private void addWebMethodAnnotation(JavaMethod method) { 101 addWebMethodAnnotation(method, method.getOperationName()); 102 } 103 104 private void addWebMethodAnnotation(JavaMethod method, String operationName) { 105 JavaAnnotation methodAnnotation = new JavaAnnotation("WebMethod"); 106 methodAnnotation.addArgument("operationName", operationName); 107 if (!StringUtils.isEmpty(method.getSoapAction())) { 108 methodAnnotation.addArgument("action", method.getSoapAction()); 109 } 110 method.addAnnotation("WebMethod", methodAnnotation); 111 method.getInterface().addImport("javax.jws.WebMethod"); 112 } 113 114 private void addWebResultAnnotation(JavaMethod method) { 115 if (method.isOneWay()) { 116 JavaAnnotation oneWayAnnotation = new JavaAnnotation("Oneway"); 117 method.addAnnotation("Oneway", oneWayAnnotation); 118 method.getInterface().addImport("javax.jws.Oneway"); 119 return; 120 } 121 122 if ("void".equals(method.getReturn().getType())) { 123 return; 124 } 125 JavaAnnotation resultAnnotation = new JavaAnnotation("WebResult"); 126 String targetNamespace = method.getReturn().getTargetNamespace(); 127 String name = "return"; 128 129 if (method.getSoapStyle() == SOAPBinding.Style.DOCUMENT 130 && !method.isWrapperStyle()) { 131 name = method.getName() + "Response"; 132 } 133 134 if (method.getSoapStyle() == SOAPBinding.Style.RPC) { 135 name = method.getReturn().getName(); 136 targetNamespace = method.getInterface().getNamespace(); 137 } 138 if (method.getSoapStyle() == SOAPBinding.Style.DOCUMENT) { 139 if (method.getReturn().getQName() != null) { 140 name = method.getReturn().getQName().getLocalPart(); 141 } 142 targetNamespace = method.getReturn().getTargetNamespace(); 143 } 144 145 resultAnnotation.addArgument("name", name); 146 resultAnnotation.addArgument("targetNamespace", targetNamespace); 147 148 149 150 if (method.getSoapStyle() == SOAPBinding.Style.RPC 151 || (method.getSoapStyle() == SOAPBinding.Style.DOCUMENT && !method.isWrapperStyle())) { 152 resultAnnotation.addArgument("partName", method.getReturn().getName()); 153 } 154 155 method.addAnnotation("WebResult", resultAnnotation); 156 method.getInterface().addImport("javax.jws.WebResult"); 157 } 158 159 protected void addWrapperAnnotation(JavaMethod method, Operation operation) { 160 if (!isWrapperStyle(operation)) { 161 return; 162 } 163 164 if (wrapperRequest != null) { 165 JavaAnnotation wrapperRequestAnnotation = new JavaAnnotation("RequestWrapper"); 166 wrapperRequestAnnotation.addArgument("localName", wrapperRequest.getType()); 167 wrapperRequestAnnotation.addArgument("targetNamespace", wrapperRequest.getTargetNamespace()); 168 wrapperRequestAnnotation.addArgument("className", wrapperRequest.getClassName()); 169 method.addAnnotation("RequestWrapper", wrapperRequestAnnotation); 170 method.getInterface().addImport("javax.xml.ws.RequestWrapper"); 171 } 172 if (wrapperResponse != null) { 173 JavaAnnotation wrapperResponseAnnotation = new JavaAnnotation("ResponseWrapper"); 174 wrapperResponseAnnotation.addArgument("localName", wrapperResponse.getType()); 175 wrapperResponseAnnotation.addArgument("targetNamespace", wrapperResponse.getTargetNamespace()); 176 wrapperResponseAnnotation.addArgument("className", wrapperResponse.getClassName()); 177 method.addAnnotation("ResponseWrapper", wrapperResponseAnnotation); 178 method.getInterface().addImport("javax.xml.ws.ResponseWrapper"); 179 } 180 181 } 182 183 @SuppressWarnings ("unchecked") 184 private boolean isWrapperStyle(Operation operation) { 185 186 Message inputMessage = operation.getInput() == null ? null : operation.getInput().getMessage(); 187 Message outputMessage = operation.getOutput() == null ? null : operation.getOutput().getMessage(); 188 189 Map <String , Part> inputParts = new HashMap <String , Part>(); 190 Map <String , Part> outputParts = new HashMap <String , Part>(); 191 192 if (inputMessage != null) { 193 inputParts = inputMessage.getParts(); 194 } 195 if (outputMessage != null) { 196 outputParts = outputMessage.getParts(); 197 } 198 199 if (inputParts.size() > 1 || outputParts.size() > 1) { 204 return false; 205 } 206 207 Part inputPart = null; 213 if (inputParts.size() == 1) { 214 inputPart = inputParts.values().iterator().next(); 215 if (inputPart != null) { 216 QName inputElement = inputPart.getElementName(); 217 if (inputElement == null) { 218 return false; 219 } else if (!operation.getName().equals(inputElement.getLocalPart())) { 220 return false; 221 } 222 } 223 } 224 Part outputPart = null; 229 if (outputParts.size() == 1) { 230 outputPart = outputParts.values().iterator().next(); 231 if (outputPart != null) { 232 QName outputElement = outputPart.getElementName(); 233 if (outputElement == null) { 234 return false; 235 } 236 } 237 } 238 239 if (ProcessorUtil.getBlock(inputPart, env) == null 244 || ProcessorUtil.getBlock(outputPart, env) == null) { 245 return false; 246 } 247 248 if (inputPart != null) { 249 wrapperRequest = new JavaParameter(); 250 wrapperRequest.setName(ProcessorUtil.resolvePartName(inputPart)); 251 wrapperRequest.setType(ProcessorUtil.getPartType(inputPart)); 252 wrapperRequest.setTargetNamespace(ProcessorUtil.resolvePartNamespace(inputPart)); 253 254 wrapperRequest.setClassName(ProcessorUtil.getFullClzName(inputPart, this.env, this.collector)); 255 256 257 } 258 if (outputPart != null) { 259 wrapperResponse = new JavaParameter(); 260 wrapperResponse.setName(ProcessorUtil.resolvePartName(outputPart)); 261 wrapperResponse.setType(ProcessorUtil.getPartType(outputPart)); 262 wrapperResponse.setTargetNamespace(ProcessorUtil.resolvePartNamespace(outputPart)); 263 264 wrapperResponse.setClassName(ProcessorUtil.getFullClzName(outputPart, this.env, this.collector)); 265 266 } 267 268 return true; 269 } 270 271 private boolean isRequestResponse(Operation operation) { 272 if (operation.getStyle() == null) { 273 return operation.getOutput() != null; 274 } 275 return OperationType.REQUEST_RESPONSE.equals(operation.getStyle()); 276 } 277 278 private JAXWSBinding customizing(JavaInterface intf, Operation operation) { 279 JAXWSBinding binding = null; 280 List extElements = operation.getExtensibilityElements(); 281 if (extElements.size() > 0) { 282 Iterator iterator = extElements.iterator(); 283 while (iterator.hasNext()) { 284 Object obj = iterator.next(); 285 if (obj instanceof JAXWSBinding) { 286 binding = (JAXWSBinding)obj; 287 } 288 } 289 } else { 290 String portTypeName = intf.getWebServiceName(); 291 String operationName = operation.getName(); 292 binding = CustomizationParser.getInstance().getPortTypeOperationExtension(portTypeName, 293 operationName); 294 } 295 296 if (binding == null) { 297 binding = new JAXWSBinding(); 298 } 299 if (!binding.isSetAsyncMapping() && (intf.getJavaModel().getJAXWSBinding().isEnableAsyncMapping() 300 || intf.getJAXWSBinding().isEnableAsyncMapping())) { 301 binding.setEnableAsyncMapping(true); 302 } 303 return binding; 304 } 305 306 private void addAsyncMethod(JavaMethod method) throws ToolException { 307 addPollingMethod(method); 308 addCallbackMethod(method); 309 310 method.getInterface().addImport("javax.xml.ws.AsyncHandler"); 311 method.getInterface().addImport("java.util.concurrent.Future"); 312 method.getInterface().addImport("javax.xml.ws.Response"); 313 } 314 315 private void addPollingMethod(JavaMethod method) throws ToolException { 316 JavaMethod pollingMethod = new JavaMethod(method.getInterface()); 317 pollingMethod.setName(method.getName() + ToolConstants.ASYNC_METHOD_SUFFIX); 318 pollingMethod.setStyle(method.getStyle()); 319 pollingMethod.setWrapperStyle(method.isWrapperStyle()); 320 pollingMethod.setSoapAction(method.getSoapAction()); 321 322 JavaReturn future = new JavaReturn(); 323 future.setClassName("Future<?>"); 324 pollingMethod.setReturn(future); 325 326 addWebMethodAnnotation(pollingMethod, method.getOperationName()); 327 pollingMethod.addAnnotation("ResponseWrapper", method.getAnnotationMap().get("ResponseWrapper")); 328 pollingMethod.addAnnotation("RequestWrapper", method.getAnnotationMap().get("RequestWrapper")); 329 pollingMethod.addAnnotation("SOAPBinding", method.getAnnotationMap().get("SOAPBinding")); 330 331 for (Iterator iter = method.getParameters().iterator(); iter.hasNext();) { 332 pollingMethod.addParameter((JavaParameter)iter.next()); 333 } 334 335 JavaParameter asyncHandler = new JavaParameter(); 336 asyncHandler.setName("asyncHandler"); 337 asyncHandler.setClassName(getAsyncClassName(method, "AsyncHandler")); 338 JavaAnnotation asyncHandlerAnnotation = new JavaAnnotation("WebParam"); 339 asyncHandlerAnnotation.addArgument("name", "asyncHandler"); 340 asyncHandlerAnnotation.addArgument("targetNamespace", ""); 341 asyncHandler.setAnnotation(asyncHandlerAnnotation); 342 343 pollingMethod.addParameter(asyncHandler); 344 345 method.getInterface().addMethod(pollingMethod); 346 } 347 348 private void addCallbackMethod(JavaMethod method) throws ToolException { 349 JavaMethod callbackMethod = new JavaMethod(method.getInterface()); 350 callbackMethod.setName(method.getName() + ToolConstants.ASYNC_METHOD_SUFFIX); 351 callbackMethod.setStyle(method.getStyle()); 352 callbackMethod.setWrapperStyle(method.isWrapperStyle()); 353 callbackMethod.setSoapAction(method.getSoapAction()); 354 355 JavaReturn response = new JavaReturn(); 356 response.setClassName(getAsyncClassName(method, "Response")); 357 callbackMethod.setReturn(response); 358 359 addWebMethodAnnotation(callbackMethod, method.getOperationName()); 360 callbackMethod.addAnnotation("RequestWrapper", method.getAnnotationMap().get("RequestWrapper")); 361 callbackMethod.addAnnotation("ResponseWrapper", method.getAnnotationMap().get("ResponseWrapper")); 362 callbackMethod.addAnnotation("SOAPBinding", method.getAnnotationMap().get("SOAPBinding")); 363 364 for (Iterator iter = method.getParameters().iterator(); iter.hasNext();) { 365 callbackMethod.addParameter((JavaParameter)iter.next()); 366 } 367 368 method.getInterface().addMethod(callbackMethod); 369 } 370 371 private String getAsyncClassName(JavaMethod method, String clzName) { 372 String response; 373 if (wrapperResponse != null) { 374 response = wrapperResponse.getClassName(); 375 } else { 376 response = method.getReturn().getClassName(); 377 } 378 379 StringBuffer sb = new StringBuffer (); 380 sb.append(clzName); 381 sb.append("<"); 382 sb.append(response); 383 sb.append(">"); 384 return sb.toString(); 385 } 386 } 387 | Popular Tags |