Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.internal.ws.processor.generator; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.codemodel.internal.ClassType; |
aoqi@0 | 29 | import com.sun.codemodel.internal.JAnnotationUse; |
aoqi@0 | 30 | import com.sun.codemodel.internal.JBlock; |
aoqi@0 | 31 | import com.sun.codemodel.internal.JCatchBlock; |
aoqi@0 | 32 | import com.sun.codemodel.internal.JClass; |
aoqi@0 | 33 | import com.sun.codemodel.internal.JClassAlreadyExistsException; |
aoqi@0 | 34 | import com.sun.codemodel.internal.JCommentPart; |
aoqi@0 | 35 | import com.sun.codemodel.internal.JConditional; |
aoqi@0 | 36 | import com.sun.codemodel.internal.JDefinedClass; |
aoqi@0 | 37 | import com.sun.codemodel.internal.JDocComment; |
aoqi@0 | 38 | import com.sun.codemodel.internal.JExpr; |
aoqi@0 | 39 | import com.sun.codemodel.internal.JFieldVar; |
aoqi@0 | 40 | import com.sun.codemodel.internal.JInvocation; |
aoqi@0 | 41 | import com.sun.codemodel.internal.JMethod; |
aoqi@0 | 42 | import com.sun.codemodel.internal.JMod; |
aoqi@0 | 43 | import com.sun.codemodel.internal.JTryBlock; |
aoqi@0 | 44 | import com.sun.codemodel.internal.JType; |
aoqi@0 | 45 | import com.sun.codemodel.internal.JVar; |
aoqi@0 | 46 | import com.sun.tools.internal.ws.processor.model.Model; |
aoqi@0 | 47 | import com.sun.tools.internal.ws.processor.model.ModelProperties; |
aoqi@0 | 48 | import com.sun.tools.internal.ws.processor.model.Port; |
aoqi@0 | 49 | import com.sun.tools.internal.ws.processor.model.Service; |
aoqi@0 | 50 | import com.sun.tools.internal.ws.processor.model.java.JavaInterface; |
aoqi@0 | 51 | import com.sun.tools.internal.ws.resources.GeneratorMessages; |
aoqi@0 | 52 | import com.sun.tools.internal.ws.wscompile.ErrorReceiver; |
aoqi@0 | 53 | import com.sun.tools.internal.ws.wscompile.Options; |
aoqi@0 | 54 | import com.sun.tools.internal.ws.wscompile.WsimportOptions; |
aoqi@0 | 55 | import com.sun.tools.internal.ws.wsdl.document.PortType; |
aoqi@0 | 56 | import com.sun.xml.internal.ws.spi.db.BindingHelper; |
aoqi@0 | 57 | |
aoqi@0 | 58 | import org.xml.sax.Locator; |
aoqi@0 | 59 | |
aoqi@0 | 60 | import javax.xml.namespace.QName; |
aoqi@0 | 61 | import javax.xml.ws.WebEndpoint; |
aoqi@0 | 62 | import javax.xml.ws.WebServiceClient; |
aoqi@0 | 63 | import javax.xml.ws.WebServiceFeature; |
aoqi@0 | 64 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 65 | import java.net.MalformedURLException; |
aoqi@0 | 66 | import java.net.URL; |
aoqi@0 | 67 | |
aoqi@0 | 68 | import com.sun.xml.internal.ws.util.ServiceFinder; |
aoqi@0 | 69 | import java.util.Locale; |
aoqi@0 | 70 | |
aoqi@0 | 71 | /** |
aoqi@0 | 72 | * @author WS Development Team |
aoqi@0 | 73 | * @author Jitendra Kotamraju |
aoqi@0 | 74 | */ |
aoqi@0 | 75 | public class ServiceGenerator extends GeneratorBase { |
aoqi@0 | 76 | |
aoqi@0 | 77 | public static void generate(Model model, WsimportOptions options, ErrorReceiver receiver) { |
aoqi@0 | 78 | ServiceGenerator serviceGenerator = new ServiceGenerator(model, options, receiver); |
aoqi@0 | 79 | serviceGenerator.doGeneration(); |
aoqi@0 | 80 | } |
aoqi@0 | 81 | |
aoqi@0 | 82 | private ServiceGenerator(Model model, WsimportOptions options, ErrorReceiver receiver) { |
aoqi@0 | 83 | init(model, options, receiver); |
aoqi@0 | 84 | } |
aoqi@0 | 85 | |
aoqi@0 | 86 | @Override |
aoqi@0 | 87 | public void visit(Service service) { |
aoqi@0 | 88 | JavaInterface intf = service.getJavaInterface(); |
aoqi@0 | 89 | String className = Names.customJavaTypeClassName(intf); |
aoqi@0 | 90 | if (donotOverride && GeneratorUtil.classExists(options, className)) { |
aoqi@0 | 91 | log("Class " + className + " exists. Not overriding."); |
aoqi@0 | 92 | return; |
aoqi@0 | 93 | } |
aoqi@0 | 94 | |
aoqi@0 | 95 | JDefinedClass cls; |
aoqi@0 | 96 | try { |
aoqi@0 | 97 | cls = getClass(className, ClassType.CLASS); |
aoqi@0 | 98 | } catch (JClassAlreadyExistsException e) { |
aoqi@0 | 99 | receiver.error(service.getLocator(), GeneratorMessages.GENERATOR_SERVICE_CLASS_ALREADY_EXIST(className, service.getName())); |
aoqi@0 | 100 | return; |
aoqi@0 | 101 | } |
aoqi@0 | 102 | |
aoqi@0 | 103 | cls._extends(javax.xml.ws.Service.class); |
aoqi@0 | 104 | String serviceFieldName = BindingHelper.mangleNameToClassName(service.getName().getLocalPart()).toUpperCase(Locale.ENGLISH); |
aoqi@0 | 105 | String wsdlLocationName = serviceFieldName + "_WSDL_LOCATION"; |
aoqi@0 | 106 | JFieldVar urlField = cls.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, URL.class, wsdlLocationName); |
aoqi@0 | 107 | |
aoqi@0 | 108 | JFieldVar exField = cls.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, WebServiceException.class, serviceFieldName+"_EXCEPTION"); |
aoqi@0 | 109 | |
aoqi@0 | 110 | |
aoqi@0 | 111 | String serviceName = serviceFieldName + "_QNAME"; |
aoqi@0 | 112 | cls.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, QName.class, serviceName, |
aoqi@0 | 113 | JExpr._new(cm.ref(QName.class)).arg(service.getName().getNamespaceURI()).arg(service.getName().getLocalPart())); |
aoqi@0 | 114 | |
aoqi@0 | 115 | JClass qNameCls = cm.ref(QName.class); |
aoqi@0 | 116 | JInvocation inv; |
aoqi@0 | 117 | inv = JExpr._new(qNameCls); |
aoqi@0 | 118 | inv.arg("namespace"); |
aoqi@0 | 119 | inv.arg("localpart"); |
aoqi@0 | 120 | |
aoqi@0 | 121 | if (options.useBaseResourceAndURLToLoadWSDL) { |
aoqi@0 | 122 | writeClassLoaderBaseResourceWSDLLocation(className, cls, urlField, exField); |
aoqi@0 | 123 | } else if (wsdlLocation.startsWith("http://") || wsdlLocation.startsWith("https://") || wsdlLocation.startsWith("file:/")) { |
aoqi@0 | 124 | writeAbsWSDLLocation(cls, urlField, exField); |
aoqi@0 | 125 | } else if (wsdlLocation.startsWith("META-INF/")) { |
aoqi@0 | 126 | writeClassLoaderResourceWSDLLocation(className, cls, urlField, exField); |
aoqi@0 | 127 | } else { |
aoqi@0 | 128 | writeResourceWSDLLocation(className, cls, urlField, exField); |
aoqi@0 | 129 | } |
aoqi@0 | 130 | |
aoqi@0 | 131 | //write class comment - JAXWS warning |
aoqi@0 | 132 | JDocComment comment = cls.javadoc(); |
aoqi@0 | 133 | |
aoqi@0 | 134 | if (service.getJavaDoc() != null) { |
aoqi@0 | 135 | comment.add(service.getJavaDoc()); |
aoqi@0 | 136 | comment.add("\n\n"); |
aoqi@0 | 137 | } |
aoqi@0 | 138 | |
aoqi@0 | 139 | for (String doc : getJAXWSClassComment()) { |
aoqi@0 | 140 | comment.add(doc); |
aoqi@0 | 141 | } |
aoqi@0 | 142 | |
aoqi@0 | 143 | // Generating constructor |
aoqi@0 | 144 | // for e.g: public ExampleService() |
aoqi@0 | 145 | JMethod constructor1 = cls.constructor(JMod.PUBLIC); |
aoqi@0 | 146 | String constructor1Str = String.format("super(__getWsdlLocation(), %s);", serviceName); |
aoqi@0 | 147 | constructor1.body().directStatement(constructor1Str); |
aoqi@0 | 148 | |
aoqi@0 | 149 | // Generating constructor |
aoqi@0 | 150 | // for e.g: public ExampleService(WebServiceFeature ... features) |
aoqi@0 | 151 | if (options.target.isLaterThan(Options.Target.V2_2)) { |
aoqi@0 | 152 | JMethod constructor2 = cls.constructor(JMod.PUBLIC); |
aoqi@0 | 153 | constructor2.varParam(WebServiceFeature.class, "features"); |
aoqi@0 | 154 | String constructor2Str = String.format("super(__getWsdlLocation(), %s, features);", serviceName); |
aoqi@0 | 155 | constructor2.body().directStatement(constructor2Str); |
aoqi@0 | 156 | } |
aoqi@0 | 157 | |
aoqi@0 | 158 | // Generating constructor |
aoqi@0 | 159 | // for e.g: public ExampleService(URL wsdlLocation) |
aoqi@0 | 160 | if (options.target.isLaterThan(Options.Target.V2_2)) { |
aoqi@0 | 161 | JMethod constructor3 = cls.constructor(JMod.PUBLIC); |
aoqi@0 | 162 | constructor3.param(URL.class, "wsdlLocation"); |
aoqi@0 | 163 | String constructor3Str = String.format("super(wsdlLocation, %s);", serviceName); |
aoqi@0 | 164 | constructor3.body().directStatement(constructor3Str); |
aoqi@0 | 165 | } |
aoqi@0 | 166 | |
aoqi@0 | 167 | // Generating constructor |
aoqi@0 | 168 | // for e.g: public ExampleService(URL wsdlLocation, WebServiceFeature ... features) |
aoqi@0 | 169 | if (options.target.isLaterThan(Options.Target.V2_2)) { |
aoqi@0 | 170 | JMethod constructor4 = cls.constructor(JMod.PUBLIC); |
aoqi@0 | 171 | constructor4.param(URL.class, "wsdlLocation"); |
aoqi@0 | 172 | constructor4.varParam(WebServiceFeature.class, "features"); |
aoqi@0 | 173 | String constructor4Str = String.format("super(wsdlLocation, %s, features);", serviceName); |
aoqi@0 | 174 | constructor4.body().directStatement(constructor4Str); |
aoqi@0 | 175 | } |
aoqi@0 | 176 | |
aoqi@0 | 177 | // Generating constructor |
aoqi@0 | 178 | // for e.g: public ExampleService(URL wsdlLocation, QName serviceName) |
aoqi@0 | 179 | JMethod constructor5 = cls.constructor(JMod.PUBLIC); |
aoqi@0 | 180 | constructor5.param(URL.class, "wsdlLocation"); |
aoqi@0 | 181 | constructor5.param(QName.class, "serviceName"); |
aoqi@0 | 182 | constructor5.body().directStatement("super(wsdlLocation, serviceName);"); |
aoqi@0 | 183 | |
aoqi@0 | 184 | // Generating constructor |
aoqi@0 | 185 | // for e.g: public ExampleService(URL, QName, WebServiceFeature ...) |
aoqi@0 | 186 | if (options.target.isLaterThan(Options.Target.V2_2)) { |
aoqi@0 | 187 | JMethod constructor6 = cls.constructor(JMod.PUBLIC); |
aoqi@0 | 188 | constructor6.param(URL.class, "wsdlLocation"); |
aoqi@0 | 189 | constructor6.param(QName.class, "serviceName"); |
aoqi@0 | 190 | constructor6.varParam(WebServiceFeature.class, "features"); |
aoqi@0 | 191 | constructor6.body().directStatement("super(wsdlLocation, serviceName, features);"); |
aoqi@0 | 192 | } |
aoqi@0 | 193 | |
aoqi@0 | 194 | //@WebService |
aoqi@0 | 195 | JAnnotationUse webServiceClientAnn = cls.annotate(cm.ref(WebServiceClient.class)); |
aoqi@0 | 196 | writeWebServiceClientAnnotation(service, webServiceClientAnn); |
aoqi@0 | 197 | |
aoqi@0 | 198 | // additional annotations |
aoqi@0 | 199 | for (GeneratorExtension f:ServiceFinder.find(GeneratorExtension.class)) { |
aoqi@0 | 200 | f.writeWebServiceClientAnnotation(options, cm, cls); |
aoqi@0 | 201 | } |
aoqi@0 | 202 | |
aoqi@0 | 203 | |
aoqi@0 | 204 | //@HandlerChain |
aoqi@0 | 205 | writeHandlerConfig(Names.customJavaTypeClassName(service.getJavaInterface()), cls, options); |
aoqi@0 | 206 | |
aoqi@0 | 207 | for (Port port : service.getPorts()) { |
aoqi@0 | 208 | if (port.isProvider()) { |
aoqi@0 | 209 | continue; // No getXYZPort() for porvider based endpoint |
aoqi@0 | 210 | } |
aoqi@0 | 211 | |
aoqi@0 | 212 | //Get the SEI class |
aoqi@0 | 213 | JType retType; |
aoqi@0 | 214 | try { |
aoqi@0 | 215 | retType = getClass(port.getJavaInterface().getName(), ClassType.INTERFACE); |
aoqi@0 | 216 | } catch (JClassAlreadyExistsException e) { |
aoqi@0 | 217 | QName portTypeName = |
aoqi@0 | 218 | (QName) port.getProperty( |
aoqi@0 | 219 | ModelProperties.PROPERTY_WSDL_PORT_TYPE_NAME); |
aoqi@0 | 220 | Locator loc = null; |
aoqi@0 | 221 | if (portTypeName != null) { |
aoqi@0 | 222 | PortType pt = port.portTypes.get(portTypeName); |
aoqi@0 | 223 | if (pt != null) { |
aoqi@0 | 224 | loc = pt.getLocator(); |
aoqi@0 | 225 | } |
aoqi@0 | 226 | } |
aoqi@0 | 227 | receiver.error(loc, GeneratorMessages.GENERATOR_SEI_CLASS_ALREADY_EXIST(port.getJavaInterface().getName(), portTypeName)); |
aoqi@0 | 228 | return; |
aoqi@0 | 229 | } |
aoqi@0 | 230 | |
aoqi@0 | 231 | //write getXyzPort() |
aoqi@0 | 232 | writeDefaultGetPort(port, retType, cls); |
aoqi@0 | 233 | |
aoqi@0 | 234 | //write getXyzPort(WebServicesFeature...) |
aoqi@0 | 235 | if (options.target.isLaterThan(Options.Target.V2_1)) { |
aoqi@0 | 236 | writeGetPort(port, retType, cls); |
aoqi@0 | 237 | } |
aoqi@0 | 238 | } |
aoqi@0 | 239 | |
aoqi@0 | 240 | writeGetWsdlLocation(cm.ref(URL.class), cls, urlField, exField); |
aoqi@0 | 241 | } |
aoqi@0 | 242 | |
aoqi@0 | 243 | private void writeGetPort(Port port, JType retType, JDefinedClass cls) { |
aoqi@0 | 244 | JMethod m = cls.method(JMod.PUBLIC, retType, port.getPortGetter()); |
aoqi@0 | 245 | JDocComment methodDoc = m.javadoc(); |
aoqi@0 | 246 | if (port.getJavaDoc() != null) { |
aoqi@0 | 247 | methodDoc.add(port.getJavaDoc()); |
aoqi@0 | 248 | } |
aoqi@0 | 249 | JCommentPart ret = methodDoc.addReturn(); |
aoqi@0 | 250 | JCommentPart paramDoc = methodDoc.addParam("features"); |
aoqi@0 | 251 | paramDoc.append("A list of "); |
aoqi@0 | 252 | paramDoc.append("{@link " + WebServiceFeature.class.getName() + "}"); |
aoqi@0 | 253 | paramDoc.append("to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values."); |
aoqi@0 | 254 | ret.add("returns " + retType.name()); |
aoqi@0 | 255 | m.varParam(WebServiceFeature.class, "features"); |
aoqi@0 | 256 | JBlock body = m.body(); |
aoqi@0 | 257 | StringBuilder statement = new StringBuilder("return "); |
aoqi@0 | 258 | statement.append("super.getPort(new QName(\"").append(port.getName().getNamespaceURI()).append("\", \"").append(port.getName().getLocalPart()).append("\"), "); |
aoqi@0 | 259 | statement.append(retType.name()); |
aoqi@0 | 260 | statement.append(".class, features);"); |
aoqi@0 | 261 | body.directStatement(statement.toString()); |
aoqi@0 | 262 | writeWebEndpoint(port, m); |
aoqi@0 | 263 | } |
aoqi@0 | 264 | |
aoqi@0 | 265 | |
aoqi@0 | 266 | /* |
aoqi@0 | 267 | Generates the code to create URL for absolute WSDL location |
aoqi@0 | 268 | |
aoqi@0 | 269 | for e.g.: |
aoqi@0 | 270 | static { |
aoqi@0 | 271 | URL url = null; |
aoqi@0 | 272 | WebServiceException e = null; |
aoqi@0 | 273 | try { |
aoqi@0 | 274 | url = new URL("http://ExampleService.wsdl"); |
aoqi@0 | 275 | } catch (MalformedURLException ex) { |
aoqi@0 | 276 | e = new WebServiceException(ex); |
aoqi@0 | 277 | } |
aoqi@0 | 278 | EXAMPLESERVICE_WSDL_LOCATION = url; |
aoqi@0 | 279 | EXAMPLESERVICE_EXCEPTION = e; |
aoqi@0 | 280 | } |
aoqi@0 | 281 | */ |
aoqi@0 | 282 | private void writeAbsWSDLLocation(JDefinedClass cls, JFieldVar urlField, JFieldVar exField) { |
aoqi@0 | 283 | JBlock staticBlock = cls.init(); |
aoqi@0 | 284 | JVar urlVar = staticBlock.decl(cm.ref(URL.class), "url", JExpr._null()); |
aoqi@0 | 285 | JVar exVar = staticBlock.decl(cm.ref(WebServiceException.class), "e", JExpr._null()); |
aoqi@0 | 286 | |
aoqi@0 | 287 | JTryBlock tryBlock = staticBlock._try(); |
aoqi@0 | 288 | tryBlock.body().assign(urlVar, JExpr._new(cm.ref(URL.class)).arg(wsdlLocation)); |
aoqi@0 | 289 | JCatchBlock catchBlock = tryBlock._catch(cm.ref(MalformedURLException.class)); |
aoqi@0 | 290 | catchBlock.param("ex"); |
aoqi@0 | 291 | catchBlock.body().assign(exVar, JExpr._new(cm.ref(WebServiceException.class)).arg(JExpr.ref("ex"))); |
aoqi@0 | 292 | |
aoqi@0 | 293 | staticBlock.assign(urlField, urlVar); |
aoqi@0 | 294 | staticBlock.assign(exField, exVar); |
aoqi@0 | 295 | } |
aoqi@0 | 296 | |
aoqi@0 | 297 | /* |
aoqi@0 | 298 | Generates the code to create URL for WSDL location as resource |
aoqi@0 | 299 | |
aoqi@0 | 300 | for e.g.: |
aoqi@0 | 301 | static { |
aoqi@0 | 302 | EXAMPLESERVICE_WSDL_LOCATION = ExampleService.class.getResource(...); |
aoqi@0 | 303 | Exception e = null; |
aoqi@0 | 304 | if (EXAMPLESERVICE_WSDL_LOCATION == null) { |
aoqi@0 | 305 | e = new WebServiceException("..."); |
aoqi@0 | 306 | } |
aoqi@0 | 307 | EXAMPLESERVICE_EXCEPTION = e; |
aoqi@0 | 308 | } |
aoqi@0 | 309 | */ |
aoqi@0 | 310 | private void writeResourceWSDLLocation(String className, JDefinedClass cls, JFieldVar urlField, JFieldVar exField) { |
aoqi@0 | 311 | JBlock staticBlock = cls.init(); |
aoqi@0 | 312 | staticBlock.assign(urlField, JExpr.dotclass(cm.ref(className)).invoke("getResource").arg(wsdlLocation)); |
aoqi@0 | 313 | JVar exVar = staticBlock.decl(cm.ref(WebServiceException.class), "e", JExpr._null()); |
aoqi@0 | 314 | JConditional ifBlock = staticBlock._if(urlField.eq(JExpr._null())); |
aoqi@0 | 315 | ifBlock._then().assign(exVar, JExpr._new(cm.ref(WebServiceException.class)).arg( |
aoqi@0 | 316 | "Cannot find "+JExpr.quotify('\'', wsdlLocation)+" wsdl. Place the resource correctly in the classpath.")); |
aoqi@0 | 317 | staticBlock.assign(exField, exVar); |
aoqi@0 | 318 | } |
aoqi@0 | 319 | |
aoqi@0 | 320 | /* |
aoqi@0 | 321 | Generates the code to create URL for WSDL location as classloader resource |
aoqi@0 | 322 | |
aoqi@0 | 323 | for e.g.: |
aoqi@0 | 324 | static { |
aoqi@0 | 325 | EXAMPLESERVICE_WSDL_LOCATION = ExampleService.class.getClassLoader().getResource(...); |
aoqi@0 | 326 | Exception e = null; |
aoqi@0 | 327 | if (EXAMPLESERVICE_WSDL_LOCATION == null) { |
aoqi@0 | 328 | e = new WebServiceException("..."); |
aoqi@0 | 329 | } |
aoqi@0 | 330 | EXAMPLESERVICE_EXCEPTION = e; |
aoqi@0 | 331 | } |
aoqi@0 | 332 | */ |
aoqi@0 | 333 | private void writeClassLoaderResourceWSDLLocation(String className, JDefinedClass cls, JFieldVar urlField, JFieldVar exField) { |
aoqi@0 | 334 | JBlock staticBlock = cls.init(); |
aoqi@0 | 335 | staticBlock.assign(urlField, JExpr.dotclass(cm.ref(className)).invoke("getClassLoader").invoke("getResource").arg(wsdlLocation)); |
aoqi@0 | 336 | JVar exVar = staticBlock.decl(cm.ref(WebServiceException.class), "e", JExpr._null()); |
aoqi@0 | 337 | JConditional ifBlock = staticBlock._if(urlField.eq(JExpr._null())); |
aoqi@0 | 338 | ifBlock._then().assign(exVar, JExpr._new(cm.ref(WebServiceException.class)).arg( |
aoqi@0 | 339 | "Cannot find "+JExpr.quotify('\'', wsdlLocation)+" wsdl. Place the resource correctly in the classpath.")); |
aoqi@0 | 340 | staticBlock.assign(exField, exVar); |
aoqi@0 | 341 | } |
aoqi@0 | 342 | |
aoqi@0 | 343 | /* |
aoqi@0 | 344 | Generates the code to create URL for WSDL location from classloader base resource |
aoqi@0 | 345 | |
aoqi@0 | 346 | for e.g.: |
aoqi@0 | 347 | static { |
aoqi@0 | 348 | Exception e = null; |
aoqi@0 | 349 | URL url = null; |
aoqi@0 | 350 | try { |
aoqi@0 | 351 | url = new URL(ExampleService.class.getClassLoader().getResource("."), ...); |
aoqi@0 | 352 | } catch (MalformedURLException murl) { |
aoqi@0 | 353 | e = new WebServiceException(murl); |
aoqi@0 | 354 | } |
aoqi@0 | 355 | EXAMPLESERVICE_WSDL_LOCATION = url; |
aoqi@0 | 356 | EXAMPLESERVICE_EXCEPTION = e; |
aoqi@0 | 357 | } |
aoqi@0 | 358 | */ |
aoqi@0 | 359 | private void writeClassLoaderBaseResourceWSDLLocation(String className, JDefinedClass cls, JFieldVar urlField, JFieldVar exField) { |
aoqi@0 | 360 | JBlock staticBlock = cls.init(); |
aoqi@0 | 361 | JVar exVar = staticBlock.decl(cm.ref(WebServiceException.class), "e", JExpr._null()); |
aoqi@0 | 362 | JVar urlVar = staticBlock.decl(cm.ref(URL.class), "url", JExpr._null()); |
aoqi@0 | 363 | JTryBlock tryBlock = staticBlock._try(); |
aoqi@0 | 364 | tryBlock.body().assign(urlVar, JExpr._new(cm.ref(URL.class)).arg(JExpr.dotclass(cm.ref(className)).invoke("getResource").arg(".")).arg(wsdlLocation)); |
aoqi@0 | 365 | JCatchBlock catchBlock = tryBlock._catch(cm.ref(MalformedURLException.class)); |
aoqi@0 | 366 | JVar murlVar = catchBlock.param("murl"); |
aoqi@0 | 367 | catchBlock.body().assign(exVar, JExpr._new(cm.ref(WebServiceException.class)).arg(murlVar)); |
aoqi@0 | 368 | staticBlock.assign(urlField, urlVar); |
aoqi@0 | 369 | staticBlock.assign(exField, exVar); |
aoqi@0 | 370 | } |
aoqi@0 | 371 | |
aoqi@0 | 372 | /* |
aoqi@0 | 373 | Generates code that gives wsdl URL. If there is an exception in |
aoqi@0 | 374 | creating the URL, it throws an exception. |
aoqi@0 | 375 | |
aoqi@0 | 376 | for example: |
aoqi@0 | 377 | |
aoqi@0 | 378 | private URL __getWsdlLocation() { |
aoqi@0 | 379 | if (EXAMPLESERVICE_EXCEPTION != null) { |
aoqi@0 | 380 | throw EXAMPLESERVICE_EXCEPTION; |
aoqi@0 | 381 | } |
aoqi@0 | 382 | return EXAMPLESERVICE_WSDL_LOCATION; |
aoqi@0 | 383 | } |
aoqi@0 | 384 | */ |
aoqi@0 | 385 | private void writeGetWsdlLocation(JType retType, JDefinedClass cls, JFieldVar urlField, JFieldVar exField) { |
aoqi@0 | 386 | JMethod m = cls.method(JMod.PRIVATE|JMod.STATIC , retType, "__getWsdlLocation"); |
aoqi@0 | 387 | JConditional ifBlock = m.body()._if(exField.ne(JExpr._null())); |
aoqi@0 | 388 | ifBlock._then()._throw(exField); |
aoqi@0 | 389 | m.body()._return(urlField); |
aoqi@0 | 390 | } |
aoqi@0 | 391 | |
aoqi@0 | 392 | private void writeDefaultGetPort(Port port, JType retType, JDefinedClass cls) { |
aoqi@0 | 393 | String portGetter = port.getPortGetter(); |
aoqi@0 | 394 | JMethod m = cls.method(JMod.PUBLIC, retType, portGetter); |
aoqi@0 | 395 | JDocComment methodDoc = m.javadoc(); |
aoqi@0 | 396 | if (port.getJavaDoc() != null) { |
aoqi@0 | 397 | methodDoc.add(port.getJavaDoc()); |
aoqi@0 | 398 | } |
aoqi@0 | 399 | JCommentPart ret = methodDoc.addReturn(); |
aoqi@0 | 400 | ret.add("returns " + retType.name()); |
aoqi@0 | 401 | JBlock body = m.body(); |
aoqi@0 | 402 | StringBuilder statement = new StringBuilder("return "); |
aoqi@0 | 403 | statement.append("super.getPort(new QName(\"").append(port.getName().getNamespaceURI()).append("\", \"").append(port.getName().getLocalPart()).append("\"), "); |
aoqi@0 | 404 | statement.append(retType.name()); |
aoqi@0 | 405 | statement.append(".class);"); |
aoqi@0 | 406 | body.directStatement(statement.toString()); |
aoqi@0 | 407 | writeWebEndpoint(port, m); |
aoqi@0 | 408 | } |
aoqi@0 | 409 | |
aoqi@0 | 410 | private void writeWebServiceClientAnnotation(Service service, JAnnotationUse wsa) { |
aoqi@0 | 411 | String serviceName = service.getName().getLocalPart(); |
aoqi@0 | 412 | String serviceNS = service.getName().getNamespaceURI(); |
aoqi@0 | 413 | wsa.param("name", serviceName); |
aoqi@0 | 414 | wsa.param("targetNamespace", serviceNS); |
aoqi@0 | 415 | wsa.param("wsdlLocation", wsdlLocation); |
aoqi@0 | 416 | } |
aoqi@0 | 417 | |
aoqi@0 | 418 | private void writeWebEndpoint(Port port, JMethod m) { |
aoqi@0 | 419 | JAnnotationUse webEndpointAnn = m.annotate(cm.ref(WebEndpoint.class)); |
aoqi@0 | 420 | webEndpointAnn.param("name", port.getName().getLocalPart()); |
aoqi@0 | 421 | } |
aoqi@0 | 422 | } |