templates.thrift.service.perservice.service.decorator.class.vm Maven / Gradle / Ivy
#parse("${include}/header.include.vm")
#parse("${include}/generic.include.vm")
#parse("thrift/include/decorator.include.vm")
#set ( $interfaceName = "${sourceinfo.interfaceClass.simpleName}")
#set ( $className ="${interfaceName}ThriftDecorator")
#set ( $sourceinfo.generatePackage = $pkg )
$codewriter.setCurrentJavaFilename("$pkg", "${className}.java")
${sourceinfo.removeDecorateTypesFromImports()}
## 读取源码注释
## 默认#set语句不允许设置null,所以这里要通过#if判断来将$classDoc设置为空
#if($JavadocReader.read(${sourceinfo.interfaceClass}))
#set ($classDoc = $JavadocReader.read(${sourceinfo.interfaceClass}))
#else
#set ($classDoc = "")
#end
package $pkg;
#outputImports()
import com.facebook.swift.service.ThriftException;
import com.facebook.swift.service.ThriftService;
import com.facebook.swift.service.ThriftMethod;
#if ($sourceinfo.needTransformer)
import com.gitee.l0km.xthrift.thrift.TypeTransformer;
#end
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
#if(${classDoc.getClassComment()})
## 替换所有@link,@value注释改为@code避免生成的代码产生javadoc编译警告
${classDoc.getClassComment(false,true,true).replaceAll("\{@link(\s+)","\{@code$1").replaceAll("\{@value(\s+)","\{@code$1")}
* ========================================
#end
* {@link #typeName(${sourceinfo.interfaceClass})}接口的thrift服务实现
* decorator pattern 装饰者模式将{@link #typeName(${sourceinfo.interfaceClass})}实例封装为一个thrift服务
* 转发所有服务请求到{@link ${esc.hash}delegate()}指定的实例
* $GENERAED_BY
*
*/
@ThriftService("${interfaceName}")
#if(!${sourceinfo.annotationOf($sourceinfo.interfaceClass).empty})
${sourceinfo.annoationCodeOf($sourceinfo.interfaceClass,"
")}
#end
public class ${className} {
private static final Logger logger = LoggerFactory.getLogger(${className}.class);
/** 是否输出异常堆栈信息 */
private boolean trace;
private final #typeName(${sourceinfo.interfaceClass}) delegate;
/**
* @param delegate #typeName(${sourceinfo.interfaceClass}) instance
* @param trace output stack trace if true
*/
public ${className}(#typeName(${sourceinfo.interfaceClass}) delegate,boolean trace) {
super();
if(null == delegate){
throw new NullPointerException("delegate is null");
}
this.delegate = delegate;
this.trace = trace;
}
public ${className}(#typeName(${sourceinfo.interfaceClass}) delegate) {
this(delegate, false);
}
/**
* @return 返回被装饰的{@link #typeName(${sourceinfo.interfaceClass})}实例
*/
public #typeName(${sourceinfo.interfaceClass}) delegate() {
return delegate;
}
/**
* @return 是否输出异常堆栈信息
*/
public boolean isTrace() {
return trace;
}
/**
* 设置是否输出异常堆栈信息
* @param trace
* @return 当前对象
*/
public ${className} setTrace(boolean trace) {
this.trace = trace;
return this;
}
private void logErrorIfTrace(Throwable e){
if(trace){
logger.error(e.getMessage(), e);
}
}
#set($methodCount = 0 )
#foreach ($method in $TOOL.sortBySignature($methods))
#set($methodCount= $methodCount + 1 )
#if(${classDoc.getMethodComment($method.delegate())})
## 替换所有@link,@value注释改为@code避免生成的代码产生javadoc编译警告
${classDoc.getMethodComment($method.delegate()).replaceAll("\{@link(\s+)","\{@code$1").replaceAll("\{@value(\s+)","\{@code$1")}##
#else
/**
* delegate of ${method.docSignature}
*/
#end
#set ($declaredExceptions = $TOOL.sortByName($method.genericExceptionTypeSet))
#set ($throwExceptions = $declaredExceptions)
#set ($portName = ${schemainfo.escapeThrift(${sourceinfo.getPortName($method)})})
@ThriftMethod(value = "${portName}${sourceinfo.methodSuffix($method)}" #fillExcOfThriftMethod($throwExceptions))
## 如果有方法注解(Annotation)则复制方法注解
#if(!${sourceinfo.annotationOf($method).empty})
${sourceinfo.annoationCodeOf($method,"
")}
#end
public $sourceinfo.toThriftType($method.genericReturnType) ${method.name}${sourceinfo.methodSuffix($method)}(#join($method.parameters '$sourceinfo.toThriftType($e.genericType) $schemainfo.escapeThrift($e.name)' ',
'))
#fillThriftThrows($throwExceptions){
try{
#RETURN() #returnValue($method.genericReturnType "delegate().${method.name}(#join($method.parameters '#setValue($e.genericType,$schemainfo.escapeThrift($e.name))' ',
'))");
}
#foreach($exp in $throwExceptions)
catch(#typeName($exp) e){
logErrorIfTrace(e);
throw new ${sourceinfo.toThriftType($exp)}(e);
}
#end
catch(#typeName($schemainfo.serviceRuntimeExceptionClass) e){
logErrorIfTrace(e);
throw e;
}
catch(RuntimeException e){
logErrorIfTrace(e);
throw new #typeName($schemainfo.serviceRuntimeExceptionClass)(e);
}
}
#end##foreach
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy