苏州瑞玛APS项目web后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.3 KiB

  1. // Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.
  2. package container
  3. import (
  4. "errors"
  5. "fmt"
  6. "reflect"
  7. )
  8. // 服务方法信息
  9. type ServiceMethodInformation struct {
  10. // 方法名
  11. methodName string
  12. // 方法类型
  13. methodType Method
  14. // 调用器代理创建器列表
  15. callerBrokerBuilders []CallerBrokerBuilder
  16. // 路由创建器
  17. routerBuilder RouterBuilder
  18. }
  19. // 创建服务方法信息
  20. // 参数
  21. // 1.方法元数据
  22. // 返回值:
  23. // 1.服务方法信息
  24. // 异常
  25. // 1.方法类型不能为空
  26. // 2.方法名不能为空
  27. // 3.返回值数量错误
  28. // 4.最后一个返回值类型错误
  29. func NewServiceMethodInformation(method *reflect.Method) *ServiceMethodInformation {
  30. if method == nil {
  31. panic(fmt.Sprintf("方法类型不能为空!"))
  32. }
  33. methodName := method.Name
  34. methodType := method.Type
  35. if methodName == "" {
  36. panic(fmt.Sprintf("方法名不能为空!"))
  37. }
  38. if methodType.NumOut() < 1 || 2 < methodType.NumOut() {
  39. panic(fmt.Sprintf("返回值数量错误! 返回值数量: %d", methodType.NumOut()))
  40. }
  41. if methodType.Out(methodType.NumOut()-1) != errorType {
  42. panic(fmt.Sprintf("最后一个返回值类型错误! 类型:%s", methodType.Out(methodType.NumOut()-1)))
  43. }
  44. logBroker := NewLogBrokerBuilder("", methodName, "")
  45. err := logBroker.Check(methodType)
  46. if err != nil {
  47. panic(err.Error())
  48. }
  49. return &ServiceMethodInformation{methodName, methodType, []CallerBrokerBuilder{logBroker}, nil}
  50. }
  51. // 获取方法名
  52. // 返回值:
  53. // 1.方法名
  54. func (info *ServiceMethodInformation) MethodName() string {
  55. return info.methodName
  56. }
  57. // 获取方法类型
  58. // 返回值:
  59. // 1.方法类型
  60. func (info *ServiceMethodInformation) MethodType() Method {
  61. return info.methodType
  62. }
  63. // 添加调用器代理创建器
  64. // 参数
  65. // 1.调用器代理创建器
  66. // 异常
  67. // 1.调用器代理创建器不能设置为空
  68. func (info *ServiceMethodInformation) AppendBrokerBuilder(callerBrokerBuilder CallerBrokerBuilder) {
  69. if callerBrokerBuilder == nil {
  70. panic(fmt.Sprintf("调用器代理创建器不能设置为空!"))
  71. }
  72. if err := callerBrokerBuilder.Check(info.methodType); err != nil {
  73. panic(err.Error())
  74. }
  75. info.callerBrokerBuilders = append(info.callerBrokerBuilders, callerBrokerBuilder)
  76. }
  77. // 获取路由创建器
  78. // 返回值:
  79. // 1.路由创建器
  80. func (info *ServiceMethodInformation) RouterBuilder() RouterBuilder {
  81. return info.routerBuilder
  82. }
  83. // 设置路由创建器
  84. // 参数
  85. // 1.路由创建器
  86. // 异常
  87. // 1.路由创建器不能设置为空
  88. // 2.已经设置路由创建器
  89. func (info *ServiceMethodInformation) SetRouterBuilder(value RouterBuilder) {
  90. if value == nil {
  91. panic(fmt.Sprintf("路由创建器不能设置为空!"))
  92. }
  93. if info.routerBuilder != nil {
  94. panic(fmt.Sprintf("已经设置路由创建器!"))
  95. }
  96. info.routerBuilder = value
  97. }
  98. // 设置调用器代理创建器和路由创建器
  99. // 参数
  100. // 1.创建器集合
  101. // 返回值:
  102. // 1.错误
  103. // 异常
  104. // 1.路由创建器不能设置为空
  105. // 2.已经设置路由创建器
  106. // 3.调用器代理创建器不能设置为空
  107. func (info *ServiceMethodInformation) SetBuilders(builders BuilderSet) {
  108. info.SetRouterBuilder(builders.RouterBuilder)
  109. if builders.CallerBrokerBuilders != nil {
  110. for _, brokerBuilder := range builders.CallerBrokerBuilders {
  111. info.AppendBrokerBuilder(brokerBuilder)
  112. }
  113. }
  114. }
  115. // 创建方法句柄
  116. // 参数
  117. // 1.当前会话上下文
  118. // 2.服务实例
  119. // 返回值:
  120. // 1.方法句柄
  121. // 2.错误
  122. func (info *ServiceMethodInformation) BuildHandler(sessionContext *SessionContext, instance Instance) (*ServiceMethodHandler, error) {
  123. if sessionContext == nil {
  124. return nil, errors.New(fmt.Sprintf("会话上下文不能为空!"))
  125. }
  126. caller, err := instance.GetCaller(info.methodName)
  127. if err != nil {
  128. return nil, err
  129. }
  130. if caller == nil {
  131. return nil, errors.New(fmt.Sprintf("原始调用器不能为空!"))
  132. }
  133. for i := len(info.callerBrokerBuilders) - 1; i >= 0; i-- {
  134. brokerBuilder := info.callerBrokerBuilders[i]
  135. caller, err = brokerBuilder.Build(sessionContext, caller)
  136. if err != nil {
  137. return nil, err
  138. }
  139. if caller == nil {
  140. return nil, errors.New(fmt.Sprintf("第%d个调用器不能为空!", i+1))
  141. }
  142. }
  143. serviceMethodHandler, err := NewServiceMethodHandler(caller)
  144. if err != nil {
  145. return nil, err
  146. }
  147. return serviceMethodHandler, err
  148. }