|
// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.
|
|
|
|
package container
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
// 默认组件信息管理器
|
|
// @Implement LAPP_GAAS_GFrame_BACKEND/container/InformationManager
|
|
type DefaultInformationManager struct {
|
|
// 组件信息映射: map[组件接口]组件信息
|
|
informationMapping map[Interface]ComponentInformation
|
|
}
|
|
|
|
// 创建默认组件管理器
|
|
// 返回值:
|
|
// 1.默认组件信息管理器
|
|
func NewDefaultInformationManager() *DefaultInformationManager {
|
|
return &DefaultInformationManager{make(map[Interface]ComponentInformation, 100)}
|
|
}
|
|
|
|
// @Reference LAPP_GAAS_GFrame_BACKEND/container/InformationManager.RegisterElement
|
|
func (manager *DefaultInformationManager) RegisterElement(factory interface{}) error {
|
|
|
|
elementInformation, err := NewElementInformation(factory)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
elementType := elementInformation.Interface()
|
|
componentName := elementType.Name()
|
|
_, ok := manager.informationMapping[elementType]
|
|
if ok {
|
|
return errors.New(fmt.Sprintf("组件%s接口已经注册!", componentName))
|
|
}
|
|
manager.informationMapping[elementType] = elementInformation
|
|
return nil
|
|
}
|
|
|
|
// @Reference LAPP_GAAS_GFrame_BACKEND/container/InformationManager.RegisterService
|
|
func (manager *DefaultInformationManager) RegisterService(factory interface{}) (*ServiceInformation, error) {
|
|
serviceInformation, err := NewServiceInformation(factory)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
serviceType := serviceInformation.Interface()
|
|
componentName := serviceType.Name()
|
|
_, ok := manager.informationMapping[serviceType]
|
|
if ok {
|
|
return nil, errors.New(fmt.Sprintf("组件%s接口已经注册!", componentName))
|
|
}
|
|
manager.informationMapping[serviceType] = serviceInformation
|
|
return serviceInformation, nil
|
|
}
|
|
|
|
// @Reference LAPP_GAAS_GFrame_BACKEND/container/InformationManager.Item
|
|
func (manager *DefaultInformationManager) Item(interfaceType Interface) ComponentInformation {
|
|
|
|
if interfaceType == nil {
|
|
return nil
|
|
}
|
|
item, ok := manager.informationMapping[interfaceType]
|
|
if ok {
|
|
return item
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// @Reference LAPP_GAAS_GFrame_BACKEND/container/InformationManager.Items
|
|
func (manager *DefaultInformationManager) Items() []ComponentInformation {
|
|
|
|
items := make([]ComponentInformation, len(manager.informationMapping))
|
|
index := 0
|
|
for _, item := range manager.informationMapping {
|
|
items[index] = item
|
|
index++
|
|
}
|
|
return items
|
|
}
|