All Downloads are FREE. Search and download functionalities are using the official Maven repository.

templates.stub.ksoap.stub.class.vm Maven / Gradle / Ivy

There is a newer version: 3.5.0
Show newest version
#parse("${include}/generic.include.vm")
#parse("${include}/webclient.ksoap.include.vm")
#set ( $className                ="$stubClassName")
$codewriter.setCurrentJavaFilename("$pkg", "${className}.java")
##生成注释
#macro (comment $method)
    /**
     * $GENERAED_BY
* 参见{@link $method.getDocSignature($imports)}
#foreach( $parameter in $method.parameters) * @param $parameter.name #end * @return #typeName($method.genericReturnType) #foreach( $exception in $method.exceptionTypes) * @throws $exception.simpleName #end * @see $method.getDocSignature($imports) */ #end package $pkg; #outputImports() import org.ksoap2.SoapEnvelope; import org.ksoap2.SoapFault; import org.ksoap2.transport.HttpTransportSE; import org.ksoap2.serialization.PropertyInfo; import java.io.File; import java.lang.reflect.Constructor; import java.net.URI; import java.util.List; /** * 实现{@link #typeName(${sourceinfo.serviceClass})} WebService的本地代理
* 所有自来服务器端的{@link ServiceRuntime}异常都被封装在{@link RuntimeException}中抛出,
* 可以通过调用{@link ServiceRuntime#getServerStackTraceMessage()}获取服务器端异常的堆栈信息,
* 例如: *
 *  try{
 *      double sim = faceapi.detectAndCompare2Face(
 *              TestFaceAPI.class.getResourceAsStream("/images/guyadong-1.jpg")
 *              , null
 *              , TestFaceAPI.class.getResourceAsStream("/images/guyadong-2.jpg")
 *              , null);
 *      System.out.printf("similarity=%f\n",sim);
 *  }catch(RuntimeException e){
 *      if(e.getCause() instanceof ServiceRuntime){
 *          ((ServiceRuntime)e.getCause()).printServerStackTrace();
 *      }else
 *          e.printStackTrace();
 *  }
 * 
* $GENERAED_BY
* @author guyadong * */ public class ${className} implements $I_CONSTANT{ /** * 线程局部变量
* 每个线程独享一个 {@link $C_ENVELOPE}对象 */ private static final ThreadLocal<$C_ENVELOPE> envelope=new ThreadLocal<$C_ENVELOPE>(); /** * 线程局部变量
* 每个线程独享一个 {@link HttpTransportSE}对象 */ private static final ThreadLocal transport=new ThreadLocal(); private final String nameSpace ="http://service.gdface.net"; /** * Webservice目标目标地址 */ private final String endPoint; /** * {@link HttpTransportSE}超时参数(毫秒)默认 */ private long timeoutMills=-1; /** * {@link org.ksoap2.transport.Transport}请求头
* 参见{@link org.ksoap2.transport.Transport#call(String, SoapEnvelope, List, File)} */ private List headers=null; /** * 是否启动GZIP压缩 */ private boolean compress=true; /** * 用于调试的输出xml文件({@link #debug}为{@code true}有效),默认为{@code null}
* 每次webservice调用返回的xml会保存到指定的文件中
*/ private File outputFile=null; /** * {@link org.ksoap2.transport.Transport}调试标志,默认为{@code false}
* 参见 {@link org.ksoap2.transport.Transport#debug} */ private boolean debug=false; /** * ${className}构造方法
* * @param endPoint * Webservice目标目标地址
* 如:http://192.168.1.18:8080/axis2/services/FaceDbService */ $className(String endPoint){ if(endPoint==null) throw new IllegalArgumentException("endpoint must not be null"); this.endPoint=endPoint; } /** * ${className}构造方法
* @param builder */ ${className}(Builder builder){ endPoint=builder.endPoint; timeoutMills = builder.timeoutMills; debug=builder.debug; outputFile=builder.outputFile; headers=builder.headers; compress=builder.compress; } /** * 从 {@link #envelope}中获取 {@link $C_ENVELOPE}对象,如果不存在则创建新对象 * @return $C_ENVELOPE */ public final $C_ENVELOPE getEnvelope(){ $C_ENVELOPE env; if(null==(env = envelope.get())){ env = new $C_ENVELOPE(SoapEnvelope.VER11); for(PropertyInfo info:$V_COMPLEXTYPES){ env.addMapping(info.namespace, info.name,(Class) info.type); } envelope.set(env); } return env; } /** * 从 {@link #transport}中获取 {@link HttpTransportSE}对象,如果不存在则创建新对象 * @return HttpTransportSE */ public HttpTransportSE getHttpTransport(){ HttpTransportSE trans=transport.get(); if (null == trans) { $C_GZIPHTTPTRANSPORT gziptrans; try { if (timeoutMills > 0) { gziptrans = new $C_GZIPHTTPTRANSPORT(endPoint,(int)timeoutMills); } else gziptrans = new $C_GZIPHTTPTRANSPORT(endPoint); } catch (Exception e) { throw new RuntimeException(e); } gziptrans.compress=this.compress; trans=gziptrans; transport.set(trans); } trans.debug=debug; return trans; } /** * 所有port方法的请求(Request)对象{@link java.util.Map}
* 以请求对象类(Class)为key,以{@link ThreadLocal}为value,保存每个线程独享的Request对象
* 在同一个线程反复port方法时只会使用一个Request对象 */ private static final java.util.Map,ThreadLocal> _request=new java.util.HashMap,ThreadLocal>(); /** * 从 {@link #_request}中获取请求对象,如果不存在则创建对象 * @param clazz 请求对象的类 * @return request */ @SuppressWarnings("unchecked") private static final T getRequest(Class clazz){ ThreadLocal threadLocal=_request.get(clazz); if(threadLocal==null) _request.put(clazz,(threadLocal=new ThreadLocal())); Object request=threadLocal.get(); if(request==null){ try{ request=clazz.newInstance(); }catch(Exception e){ throw new RuntimeException(e); } threadLocal.set(request); } return (T)request; } public static class Builder { /** * 目标地址模板
* 用于根据主机名(host)和端口号(host)生成目标地址,如"http://192.168.1.18:8080/axis2/services/$webServiceInfo.serviceName" */ public static final String END_POINT ="http://#host#:#port#/axis2/services/$webServiceInfo.serviceName"; private String endPoint=null; private String host=null; private int port=-1; private Object from =this; public long timeoutMills=-1; private List headers=null; private boolean compress=true; private File outputFile=null; private boolean debug=false; protected Builder() {} /** * @param from */ protected Builder(Object from) { this.from = from; } @SuppressWarnings("unchecked") public T buildReturn(){ return (T) from; } /** * 根据参数创建{@link $stubClassName}对象
* 当设置了{@link #endPoint}时优先使用{@link #endPoint}为参数创建对象 * @see $stubClassName#$stubClassName(String) */ public $stubClassName build(){ if(null==endPoint){ if(null==host) throw new IllegalArgumentException("endPoint must not be null"); //用 {@link #host},{@link #port}为参数以{@link #END_POINT}为模板生成Webservice目标目标地址 if(port<0)port=8080; endPoint=END_POINT.replaceAll("#host#", host).replaceAll("#port#", Integer.toString(port)); } return new $stubClassName(this); } /** * 设置Webservice目标地址
* 参见{@link #END_POINT} * @param endPoint 要设置的Webservice目标地址,如"http://192.168.1.18:8080/axis2/services/$webServiceInfo.serviceName" */ public void setEndPoint(URI endPoint) { this.endPoint = endPoint.toString(); } /** * 参见{@link #END_POINT} * @param host 主机名 */ public Builder setHost(String host) { this.host = host; return this; } /** * @param port 端口号,当<0时,使用默认端口8080 */ public Builder setPort(int port) { this.port = port; return this; } /** * @param timeoutMills 参见 {@link ${className}#timeoutMills} * @return Builder */ public Builder setTimeoutMills(long timeoutMills) { this.timeoutMills = timeoutMills; return this; } /** * @param headers 参见 {@link ${className}#headers} * @return Builder */ public Builder setHeaders(List headers) { this.headers = headers; return this; } /** * @param outputFile 参见 {@link ${className}#outputFile} * @return Builder */ public Builder setOutputFile(File outputFile) { this.outputFile = outputFile; return this; } /** * @param debug 参见 {@link ${className}#debug} * @return Builder */ public Builder setDebug(boolean debug) { this.debug = debug; return this; } /** * 设置发送请求时是否启用GZIP压缩 * @param compress 要设置的 compress * @return Builder */ public Builder setCompress(boolean compress) { this.compress = compress; return this; } } public static final Builder builder(){ return new Builder(); } public static final Builder builder(T from) { return new Builder(from); } #foreach($method in $TOOL.sortByName($webServiceInfo.ports.values())) #set($requestClassName=${TOOL.toClassName($method.name)}) //$velocityCount #comment($method) #set($exps=$TOOL.sortBy($method.genericExceptionTypes,"simpleName")) public final #typeParametersDefine()#typeName($method.genericReturnType) $method.name (#join($method.parameters '#typeName($e.type) $e.name' ',')) #fillThrows($exps){ $C_ENVELOPE envelope=getEnvelope(); envelope.setOutputSoapObject(getRequest(${requestClassName}.class).setParameters(#join($method.parameters '$e.name' ','))); try { getHttpTransport().call(nameSpace + "/$method.name", envelope,headers,outputFile);//for debug } catch (Exception e) { throw new RuntimeException(e); } #if(!$TOOL.isVoid($method.returnType)) if(envelope.bodyIn instanceof ${TOOL.toClassName($method.name)}Response) return ((${TOOL.toClassName($method.name)}Response) envelope.bodyIn).get_return(); #end if(envelope.bodyIn instanceof SoapFault){ SoapFault fault= (SoapFault) envelope.bodyIn; Object exp=${C_UTILITS}.convertToException(fault,envelope); #foreach($exp in $exps) if(exp instanceof #typeName($exp)) throw new #getKname($exp)(fault.getMessage(),(#getKname($exp))exp); #end throw new RuntimeException(fault); }else throw new IllegalArgumentException(String.format("unexpected return type %s ",envelope.bodyIn.getClass().getName())); } #end public File getOutputFile() { return outputFile; } public void setOutputFile(File outputFile) { this.outputFile = outputFile; } public boolean isDebug() { return debug; } public void setDebug(boolean debug) { this.debug = debug; } public List getHeaders() { return headers; } public void setHeaders(List headers) { this.headers = headers; } }