GAAS GFrame项目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.

113 lines
3.1 KiB

  1. // Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.
  2. package container
  3. import (
  4. "errors"
  5. "fmt"
  6. )
  7. // 服务组件信息
  8. // @Implement LAPP_GAAS_GFrame_BACKEND/container/ComponentInformation
  9. type ServiceInformation struct {
  10. // @Inherit LAPP_GAAS_GFrame_BACKEND/container/ElementInformation
  11. ElementInformation
  12. // 方法信息映射: map[方法名]方法信息
  13. methodMapping map[string]*ServiceMethodInformation
  14. }
  15. // 创建服务组件信息
  16. // 参数
  17. // 1.工厂方法
  18. // 返回值:
  19. // 1.服务组件信息
  20. // 2.错误
  21. func NewServiceInformation(factory interface{}) *ServiceInformation {
  22. valueOfFactory, interfaceType := parseFactory(factory)
  23. return &ServiceInformation{ElementInformation{valueOfFactory, interfaceType}, make(map[string]*ServiceMethodInformation, 10)}
  24. }
  25. // @Reference LAPP_GAAS_GFrame_BACKEND/container/ComponentInformation.BuildHandler
  26. // @Override LAPP_GAAS_GFrame_BACKEND/container/ElementInformation.BuildHandler
  27. func (info *ServiceInformation) BuildHandler(sessionContext *SessionContext) (ComponentHandler, error) {
  28. if sessionContext == nil {
  29. return nil, errors.New(fmt.Sprintf("会话上下文不能为空!"))
  30. }
  31. instance, err := info.factory.Create(sessionContext)
  32. if err != nil {
  33. return nil, err
  34. }
  35. methodHandlers := make(map[string]*ServiceMethodHandler, len(info.methodMapping))
  36. for methodName, methodInformation := range info.methodMapping {
  37. methodHandler, err := methodInformation.BuildHandler(sessionContext, instance)
  38. if err != nil {
  39. return nil, err
  40. }
  41. methodHandlers[methodName] = methodHandler
  42. }
  43. serviceHandler, err := NewServiceHandler(instance, methodHandlers)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return serviceHandler, nil
  48. }
  49. // 注册服务方法
  50. // 参数
  51. // 1.方法
  52. // 返回值:
  53. // 1.注册的服务方法信息
  54. // 2.错误
  55. // 异常
  56. // 1.方法名不能为空
  57. // 2.方法已经注册
  58. // 3.找不到方法
  59. // 4.方法类型不能为空
  60. // 5.返回值数量错误
  61. // 6.最后一个返回值类型错误
  62. func (info *ServiceInformation) RegisterMethod(methodName string) *ServiceMethodInformation {
  63. if methodName == "" {
  64. panic(fmt.Sprintf("方法名不能为空!"))
  65. }
  66. if _, ok := info.methodMapping[methodName]; ok {
  67. panic(fmt.Sprintf("方法%s已经注册!", methodName))
  68. }
  69. method, ok := info.interfaceType.MethodByName(methodName)
  70. if !ok {
  71. panic(fmt.Sprintf("找不到方法%s!", methodName))
  72. }
  73. methodInformation := NewServiceMethodInformation(&method)
  74. info.methodMapping[methodName] = methodInformation
  75. return methodInformation
  76. }
  77. // 获取指定名称的服务方法信息
  78. // 参数:
  79. // 1.方法名
  80. // 返回值:
  81. // 1.方法信息
  82. func (info *ServiceInformation) Method(methodName string) *ServiceMethodInformation {
  83. method, ok := info.methodMapping[methodName]
  84. if ok {
  85. return method
  86. }
  87. return nil
  88. }
  89. // 获取注册的服务方法信息列表
  90. // 返回值:
  91. // 1.服务方法信息列表
  92. func (info *ServiceInformation) Methods() []*ServiceMethodInformation {
  93. methods := make([]*ServiceMethodInformation, len(info.methodMapping))
  94. index := 0
  95. for _, method := range info.methodMapping {
  96. methods[index] = method
  97. index++
  98. }
  99. return methods
  100. }