-
Notifications
You must be signed in to change notification settings - Fork 53
/
manager.go
76 lines (55 loc) · 1.43 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package resource
import (
"errors"
"strings"
"github.com/project-flogo/core/support"
"github.com/project-flogo/core/support/log"
)
const UriScheme = "res://"
type Loader interface {
LoadResource(config *Config) (*Resource, error)
}
var loaders = make(map[string]Loader)
// RegisterManager registers a resource manager for the specified type
func RegisterLoader(resourceType string, loader Loader) error {
_, exists := loaders[resourceType]
if exists {
return errors.New("Resource Loader already registered for type: " + resourceType)
}
loaders[resourceType] = loader
return nil
}
func GetLoader(resourceType string) Loader {
return loaders[resourceType]
}
type Manager struct {
resources map[string]*Resource
}
func NewManager(resources map[string]*Resource) *Manager {
return &Manager{resources: resources}
}
func (m *Manager) GetResource(id string) *Resource {
resId := id
if strings.HasPrefix(id, UriScheme) {
//is uri
resId = id[6:]
}
return m.resources[resId]
}
func (m *Manager) CleanupResources() {
for id, res := range m.resources {
if needsCleanup, ok := res.resObj.(support.NeedsCleanup); ok {
err := needsCleanup.Cleanup()
if err != nil {
log.RootLogger().Errorf("Error cleaning up resource '%s' : ", id, err)
}
}
}
}
func GetTypeFromID(id string) (string, error) {
idx := strings.Index(id, ":")
if idx < 0 {
return "", errors.New("Invalid resource id: " + id)
}
return id[:idx], nil
}