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

templates.webclient.rpc.rpc.class.vm Maven / Gradle / Ivy

There is a newer version: 3.5.0
Show newest version
#parse("${include}/generic.include.vm")
#set ( $className				="${sourceinfo.interfaceClass.simpleName}WebserviceByRPC")
$codewriter.setCurrentJavaFilename("$pkg", "${className}.java")
package $pkg;
#outputImports()
#include("${include}/webclient.rpc.${sourceinfo.interfaceClass.simpleName}.import.vm")

/**
 *
 * RPC方式实现 {@link #typeName(${sourceinfo.interfaceClass})}的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();
 *  }
 * 
* 参见{@link RPCServiceClient}
* $GENERAED_BY
* @author guyadong * */ @SuppressWarnings("unchecked") public class ${className} extends #typeName($sourceinfo.baseClass){ private static class Port{ final QName portQName; final Class[] returnTypes ; final Set>exceptionTypes=new HashSet>(); private Port(QName portQName, Class returnType,Collection> exceptionTypes) { this.portQName = portQName; this.returnTypes = new Class[]{returnType}; this.exceptionTypes.addAll(exceptionTypes); } private Port(QName portQName, Class returnType,Class...exceptionTypes) { this(portQName, returnType, Arrays.>asList(exceptionTypes)); } } private static final String namespaceURI="$sourceinfo.namespaceURI"; private static final Map portMap=new HashMap(); private final ServiceClientProvider serviceClientProvider; #parse("${include}/webclient.rpc.${sourceinfo.interfaceClass.simpleName}.body.vm") #set( $method2PortMap=$sourceinfo.method2PortMap) static{ #foreach( $port in $method2PortMap.values()) portMap.put("$port.name",new Port(new QName(namespaceURI,"$port.name"),#typeName($port.returnType).class#foreach($exp in $port.exceptionTypes),#typeName($exp).class#end)); #end } #foreach ($method in $TOOL.sortBySignature($methods)) #set( $port = $method2PortMap.get($method)) #set( $parameterClass=$sourceinfo.toStubClass($port)) #set( $hasIOException=false) #set( $isVoid=$TOOL.isVoid($method.returnType)) @Override//$velocityCount public #typeParametersDefine() #typeName($method.genericReturnType) $method.name (#join($method.parameters '#pamameterType($e) $e.name' ',')) #fillThrows($TOOL.sortBy($method.genericExceptionTypes,"simpleName")){ Port port=portMap.get("$port.name"); RPCServiceClient rpcClient=serviceClientProvider.getRPCServiceClient(); try{ Object[] parameters= new Object[]{#join($method.parameters '#if($sourceinfo.isGenericTypeClass($method,$e.type))${e.name}.toString()#else$e.name#end' ',')}; #if($isVoid) rpcClient.invokeRobust(port.portQName, parameters); #else #if($method.returnType.primitive) return (#typeName($TOOL.toObjectType($method.returnType)))rpcClient.invokeBlocking(port.portQName, parameters, port.returnTypes)[0] ; #else return (#typeName($method.returnType))rpcClient.invokeBlocking(port.portQName, parameters, port.returnTypes)[0] ; #end #end } catch (AxisFault f) { OMElement faultElt = f.getDetail(); if (faultElt != null) { Exception exception=createException(f,port.exceptionTypes,rpcClient.getAxisService()); #foreach($exp in $port.genericExceptionTypes) #if($velocityCount==1)if#{else}else if#end(exception instanceof #typeName($exp)) #if($sourceinfo.isServiceRuntime($exp)) throw new RuntimeException((#typeName($exp))exception);//exceptionCont=$velocityCount #else throw (#typeName($exp))exception;//exceptionCont=$velocityCount #end #if($velocityCount==$port.genericExceptionTypes.size()) else throw new RuntimeException(f); #end #end }else throw new RuntimeException(f); } finally { try { rpcClient.cleanupTransport(); }catch (AxisFault e) { throw new RuntimeException(e); } } } #end private static final QName QNAME_TYPE=new QName("http://www.w3.org/2001/XMLSchema-instance", "type", "xsi"); /** * 从{@link OMElement}中获取定义Schema类型的元素 * @param faultElt * @return OMElement */ private final OMElement getTypeElement(OMElement faultElt){ for(Iterator it = faultElt.getChildElements();it.hasNext();){ OMElement ome=(OMElement) it.next(); if(null!=ome.getAttribute(QNAME_TYPE)) return ome; } return null; } /** * 根据Schema类型元素生成异常类
* @param typeElt * @param axisService * @return Class * @throws AxisFault * @throws ClassNotFoundException */ private final Class createExceptionClassFromTypeElement(OMElement typeElt,AxisService axisService) throws AxisFault, ClassNotFoundException{ String type = typeElt.getAttributeValue(QNAME_TYPE); String[] nsinfo = type.split(":"); String ns = (String) axisService.getNamespaceMap().get(nsinfo[0]); if(Judge.isEmpty(ns)) throw new AxisFault(String.format("can't get NamespaceURI for %s from NamespaceMap of service",nsinfo[0])); String exceptionClassName = new StringBuilder(Axis2Utilits.getPackageFromNamespaceURI(ns)).append(".") .append(nsinfo[1]).toString(); return Class.forName(exceptionClassName); } /** * 根据{@link AxisFault}的内容生成对应的异常对象 * @param fault * @param exceptionTypes 允许抛出的对象类型表,如果实际解析的对象不在此列,则抛出{@link AxisFault}异常 * @param axisService * @return Exception */ private final Exception createException(AxisFault fault, Set> exceptionTypes,AxisService axisService){ Exception ex=null; OMElement faultElt = fault.getDetail(); OMElement typeElement=getTypeElement(faultElt); try { if (typeElement != null) { Class exceptionClass = createExceptionClassFromTypeElement(typeElement, axisService); if (!exceptionTypes.contains(exceptionClass)) throw new AxisFault(String.format("%s be not Exception shoulds be throw", exceptionClass.getName())); Constructor constructor = exceptionClass.getConstructor(String.class); ex = (Exception) constructor.newInstance(fault.getMessage()); Object obj[] = BeanUtil.deserialize(faultElt, new Class[] { exceptionClass }, new DefaultObjectSupplier()); org.apache.commons.beanutils.BeanUtils.copyProperties(ex, obj[0]); } else throw new RuntimeException(fault); return ex; } catch (java.lang.ClassCastException e) { throw new RuntimeException(e); } catch (java.lang.ClassNotFoundException e) { throw new RuntimeException(e); } catch (java.lang.NoSuchMethodException e) { throw new RuntimeException(e); } catch (java.lang.reflect.InvocationTargetException e) { throw new RuntimeException(e); } catch (java.lang.IllegalAccessException e) { throw new RuntimeException(e); } catch (java.lang.InstantiationException e) { throw new RuntimeException(e); }catch (AxisFault e) { throw new RuntimeException(e); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy